首页 > 编程技术 > csharp

des加密解密源码 C# key值问题分析

发布时间:2020-6-25 11:38

公司协议安全需求、需要对传输内容做des、md5加密。

因为是新人、刚交给我这个任务的时候有点眩晕。就开始在网上找各种des加密的内容。因为不懂以为需要把原理也搞明白,最后误了时间、把自己也搞糊涂了。当然,逻辑能力强、有兴趣的朋友可以试着去搞搞。

先贴加密、解密的源码:

复制代码 代码如下:

/// <summary>      

/// 加密数据      

/// </summary>      

/// <param name="Text"></param>      

/// <param name="sKey"></param>      

/// <returns></returns>      

public static string Encrypt(string Text, string sKey)         {          

DESCryptoServiceProvider des = new DESCryptoServiceProvider();          

byte[] inputByteArray;          

inputByteArray = Encoding.Default.GetBytes(Text);          

des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));          

des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));          

System.IO.MemoryStream ms = new System.IO.MemoryStream();          

CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);          

cs.Write(inputByteArray, 0, inputByteArray.Length);          

cs.FlushFinalBlock();          

StringBuilder ret = new StringBuilder();          

foreach (byte b in ms.ToArray())             {              

ret.AppendFormat("{0:X2}", b);          

}          

return ret.ToString();      

}

        #endregion

/// <summary>      

/// 解密数据      

/// </summary>      

/// <param name="Text"></param>      

/// <param name="sKey"></param>      

/// <returns></returns>      

public static string Decrypt(string Text, string sKey)         {          

DESCryptoServiceProvider des = new DESCryptoServiceProvider();          

int len;          

len = Text.Length / 2;          

byte[] inputByteArray = new byte[len];          

int x, i;          

for (x = 0; x < len; x++)             {              

i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);              

inputByteArray[x] = (byte)i;             }          

des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));          

des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));          

System.IO.MemoryStream ms = new System.IO.MemoryStream();          

CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);          

cs.Write(inputByteArray, 0, inputByteArray.Length);          

cs.FlushFinalBlock();          

return Encoding.Default.GetString(ms.ToArray());         }

#endregion

因为是第一次接触des并且公司协议文档的需求、让我对这段代码里面迷糊的有:

1:俩个参数

Text 是要加密的内容

sKey是作为加密内容的密钥。当然加密、解密时候的sKey值,是要保持一致的。

2:des对象的key值

这个key值和IV值是固定的8位长度,一定要牢记。因为咱们的参数sKey是不定长度的、所以采取了一个方式就是对其进行MD5加密、然后再截取他的前8位。这是为了在解密的时候保证key一致。不然会解密出错。

最后,我说一下做为新人,我感觉牢记的几个地方,或许是大大们眼中写des必需的几点~~别喷我啊、

几个必要的对象:

DESCryptoServiceProvider 没有它你想怎么des呢、嘿嘿

MemoryStream    存储在内存的流对象

CryptoStream    定义将数据流链接到加密转换流。通过它写入MemoryStream对象当中

最后转换成String。

标签:[!--infotagslink--]

您可能感兴趣的文章: