最近工作中用到了des解密,之前没接触过,花了点时间去研究了一下,des算是比较基础的加密方式了,这边主要讲的是ECB模式,ECB模式作为一种基本工作模式,具有操作简单,易于实现的特点。同时由于其分组的独立性,利于实现并行处理,并且能很好地防止误差传播。个人觉得ECB比较适用于密钥保护。代码如下:
按 Ctrl+C 复制代码
public class DesHelper
{
private static String _ConstValue = "12345678";
public static String Decipher(String Str, bool IncludeLocalInfo)
{
if (null == Str || 0 == Str.Length) return "";
try
{
SymmetricAlgorithm sa = new DESCryptoServiceProvider();
String _Key = _ConstValue;
sa.Key = Encoding.UTF8.GetBytes(_Key);
sa.IV = Encoding.UTF8.GetBytes(_ConstValue);
sa.Mode = System.Security.Cryptography.CipherMode.ECB;
sa.Padding = PaddingMode.PKCS7;
ICryptoTransform ct = sa.CreateDecryptor();
byte[] byt = Convert.FromBase64String(Str);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
catch(Exception e) {
}
return "";
}
}
按 Ctrl+C 复制代码
没错,代码就是如此简单