DES加密解密算法: public string EncryptString(string sInputString, string sKey) { byte[] data = Encoding.UTF8.GetBytes(sInputString); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); //创建其支持存储区为内存的流。 CryptoStream cs = new CryptoStream(ms,DES.CreateEncryptor(),CryptoStreamMode.Write);//将数据流连接到加密转换流 cs.Write(data,0,data.Length); cs.FlushFinalBlock(); //用缓冲区的当前状态更新基础数据源或储存库,随后清除缓 StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat( "{0:X2} ",b); } return ret.ToString(); } // DES解密字符串 public string DecryptString(string sInputString, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //Put the input string into the byte array byte[] inputByteArray = new byte[sInputString.Length/2]; for (int x = 0; x < sInputString.Length; x+=2) { int i = Convert.ToInt32(sInputString.Substring(x,2), 16); inputByteArray[x/2] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); //Flush the data through the crypto stream into the memory stream cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); //Get the decrypted data back from the memory stream //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象 StringBuilder ret = new StringBuilder(); return System.Text.Encoding.UTF8.GetString(ms.ToArray()); }
md5、AES加密、DES加密等不行吧,我要求的是加密前与加密后都是8个字符!
这个是我存储登陆数据库连接字符串的加解密方法,很简单就与或一下。 public string read() {
//读出连接字符串 byte key = 126;//密钥 string pi; FileStream fsMyfile = new FileStream(Application.StartupPath+"\\config.dll", FileMode.Open, FileAccess.Read); int count2 = (int)fsMyfile.Length; byte[] buffer2 = new byte[count2]; fsMyfile.Read(buffer2, 0, count2); for (int i = 0; i < count2; i++) { buffer2[i] = (byte)(buffer2[i] ^ key);//解密 } fsMyfile.Close(); pi = Encoding.Unicode.GetString(buffer2); return pi; }
我用的是网上加密MD5
DES加密解密算法:
public string EncryptString(string sInputString, string sKey)
{
byte[] data = Encoding.UTF8.GetBytes(sInputString);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream(); //创建其支持存储区为内存的流。
CryptoStream cs = new CryptoStream(ms,DES.CreateEncryptor(),CryptoStreamMode.Write);//将数据流连接到加密转换流
cs.Write(data,0,data.Length);
cs.FlushFinalBlock(); //用缓冲区的当前状态更新基础数据源或储存库,随后清除缓
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat( "{0:X2} ",b);
}
return ret.ToString();
}
// DES解密字符串
public string DecryptString(string sInputString, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //Put the input string into the byte array
byte[] inputByteArray = new byte[sInputString.Length/2];
for (int x = 0; x < sInputString.Length; x+=2)
{
int i = Convert.ToInt32(sInputString.Substring(x,2), 16);
inputByteArray[x/2] = (byte)i;
} des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock(); //Get the decrypted data back from the memory stream
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder(); return System.Text.Encoding.UTF8.GetString(ms.ToArray()); }
public string read()
{
//读出连接字符串
byte key = 126;//密钥
string pi;
FileStream fsMyfile = new FileStream(Application.StartupPath+"\\config.dll", FileMode.Open, FileAccess.Read);
int count2 = (int)fsMyfile.Length;
byte[] buffer2 = new byte[count2];
fsMyfile.Read(buffer2, 0, count2);
for (int i = 0; i < count2; i++)
{
buffer2[i] = (byte)(buffer2[i] ^ key);//解密
}
fsMyfile.Close();
pi = Encoding.Unicode.GetString(buffer2);
return pi;
}