加密和解密是一门高深和复杂的学科。在程序中有时需要用一些简单的加密和解密,以保证一些关键字符串、值等地安全性。这里就要用到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位,否则加密解密都不成功。
D.E.S 是分块加密的,将明文分割成 64 BITS 的块, 然后他们一个个接起来 。他使用56位密钥对64位的数据块进行加密,并对64bits的数据块进行16轮编码。和每轮编码时,一个48bits的“每轮”密钥值由56bits的完整密钥得出来。DES用软件进行解码需要用非常长时间,而用硬件解码速度非常快,1977年,人们估计要耗资两千万美元才能建成一个专门计算机用于DES的解密,而且需要12个小时的破解才能得到结果。所以,当时DES被认为是一种十分强壮的加密方法。但今天, 只需 二十万美圆就能制造一台破译DES的特别的计算机,所以目前 DES 对需求“强壮”加密的场合已不再适用了。