您的当前位置:首页正文

DES解密,ECB模式,PKCS7填充标准

2024-08-01 来源:一二三四网
DES解密,ECB模式,PKCS7填充标准

最近⼯作中⽤到了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 复制代码

没错,代码就是如此简单

因篇幅问题不能全部显示,请点此查看更多更全内容