加密和解密是一门高深和复杂的学科。在程序中有时需要用一些简单的加密和解密,以保证一些关键字符串、值等地安全性。这里就要用到DES加密解密。当然我们不用深究这些加密的原理了,因为如果深究的话估计都得转行研究数学。DES加密解密C#中需要用到这几个类,DESCryptoServiceProvider,CryptoStream。
看下面的两组加密解密代码。
1、
(1)、对字符串进行DES加密
public static string Encrypt(string sourceString, string key, string iv)
{
try
{
byte[] btKey = Encoding.UTF8.GetBytes(key);
byte[] btIV = Encoding.UTF8.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Encoding.UTF8.GetBytes(sourceString);
try
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}
return Convert.ToBase64String(ms.ToArray());
}
catch
{
return sourceString;
}
}
}
catch { }
return "DES加密出错";
}
调用:
string recordString = Encrypt("金胖子死了", "20111219", "12345678");
结果:recordString= "xQ969nexy964SXhkTuekUQ=="
(2)、 对DES加密后的字符串进行解密
public static string Decrypt(string encryptedString, string key, string iv)
{
byte[] btKey = Encoding.UTF8.GetBytes(key);
byte[] btIV = Encoding.UTF8.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Convert.FromBase64String(encryptedString);
try
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}
return Encoding.UTF8.GetString(ms.ToArray());
}
catch
{
return encryptedString;
}
}
}
调用:string recordString = DESOperation.Decrypt("xQ969nexy964SXhkTuekUQ==", "20111219", "12345678");
结果:recordString = “金胖子死了”;
2、
(1)、加密字符串
public static string EncryptString(string sInputString, string sKey, string sIV)
{
try
{
byte[] data = Encoding.UTF8.GetBytes(sInputString);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sIV);
ICryptoTransform desencrypt = DES.CreateEncryptor();
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return BitConverter.ToString(result);
}
catch { }
return "转换出错!";
}
调用:string recordString = DESOperation.EncryptString("金胖子死了", "20111219", "12345678");
结果:recordString = "C5-0F-7A-F6-77-B1-CB-DE-B8-49-78-64-4E-E7-A4-51";
(2)、解密字符串
public static string DecryptString(string sInputString, string sKey, string sIV)
{
try
{
string[] sInput = sInputString.Split("-".ToCharArray());
byte[] data = new byte[sInput.Length];
for (int i = 0; i < sInput.Length; i++)
{
data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
}
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sIV);
ICryptoTransform desencrypt = DES.CreateDecryptor();
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return Encoding.UTF8.GetString(result);
}
catch { }
return "解密出错!";
}
调用:string recordString = DESOperation.DecryptString("C5-0F-7A-F6-77-B1-CB-DE-B8-49-78-64-4E-E7-A4-51", "20111219", "12345678");
结果:recordString =” 金胖子死了”;
注意:密钥和向量必须为8位,否则加密解密都不成功。