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

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

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

网上很多加密方式,做为开发人员,只要懂得怎么运用就行。

第一次写文章、优美的语句等有经验了再献丑,咱们直入正题。

先贴加密、解密的源码:

 1 /// <summary>       
 2  
 3 /// 加密数据       
 4  
 5 /// </summary>       
 6  
 7 /// <param name="Text"></param>       
 8  
 9 /// <param name="sKey"></param>       
10  
11 /// <returns></returns>       
12  
13 public static string Encrypt(string Text, string sKey)         {           
14  
15 DESCryptoServiceProvider des = new DESCryptoServiceProvider();           
16  
17 byte[] inputByteArray;           
18  
19 inputByteArray = Encoding.Default.GetBytes(Text);           
20  
21 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));           
22  
23 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));           
24  
25 System.IO.MemoryStream ms = new System.IO.MemoryStream();           
26  
27 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);           
28  
29 cs.Write(inputByteArray, 0, inputByteArray.Length);           
30  
31 cs.FlushFinalBlock();           
32  
33 StringBuilder ret = new StringBuilder();           
34  
35 foreach (byte b in ms.ToArray())             {               
36  
37 ret.AppendFormat("{0:X2}", b);           
38  
39 }           
40  
41 return ret.ToString();       
42  
43 }
44  
45         #endregion
46  
47 /// <summary>       
48  
49 /// 解密数据       
50  
51 /// </summary>       
52  
53 /// <param name="Text"></param>       
54  
55 /// <param name="sKey"></param>       
56  
57 /// <returns></returns>       
58  
59 public static string Decrypt(string Text, string sKey)         {           
60  
61 DESCryptoServiceProvider des = new DESCryptoServiceProvider();           
62  
63 int len;           
64  
65 len = Text.Length / 2;           
66  
67 byte[] inputByteArray = new byte[len];           
68  
69 int x, i;           
70  
71 for (x = 0; x < len; x++)             {               
72  
73 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);               
74  
75 inputByteArray[x] = (byte)i;             }           
76  
77 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));           
78  
79 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));           
80  
81 System.IO.MemoryStream ms = new System.IO.MemoryStream();           
82  
83 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);           
84  
85 cs.Write(inputByteArray, 0, inputByteArray.Length);           
86  
87 cs.FlushFinalBlock();           
88  
89 return Encoding.Default.GetString(ms.ToArray());         }
90  
91         #endregion
View Code


 

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

1:俩个参数

Text 是要加密的内容

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

2:des对象的key值

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

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

几个必要的对象:

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

MemoryStream    存储在内存的流对象

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

最后转换成String、

就这么搞定了、我也有好多不懂的、欢迎朋友们一起讨论、大大们多多指教。

原文地址:https://www.cnblogs.com/tainshi/p/3501258.html