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

Pearl ·
更新时间:2024-09-20
· 875 次阅读

公司协议安全需求、需要对传输内容做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。

您可能感兴趣的文章:C#的3DES加密解密算法实例代码C#编写DES加密、解密类php实现和c#一致的DES加密解密实例C#使用DES和AES实现加密解密功能示例C# 3DES加密详解c#通过DES加密算法加密大文件的方法C# DES加密算法中向量的作用详细解析C#实现对AES加密和解密的方法基于C#对用户密码使用MD5加密与解密asp实现的sha1加密解密代码(和C#兼容)C#编写的Base64加密和解密类C#实现简单的3DES加密解密功能示例



des加密 C# des 源码 key

需要 登录 后方可回复, 如果你还没有账号请 注册新账号