代码改变世界

数据加密之DES加密

2015-05-06 14:30  糯米粥  阅读(473)  评论(0编辑  收藏  举报

DES加密即使用DESCryptoServiceProvider加密。DESCryptoServiceProvider在命名空间下:System.Security.Cryptography;

对称加密算法的优点在于加解密的高速度和使用长密钥时的难破解性。假设两个用户需要使用对称加密方法加密然后交换数据,则用户最少需要2个密钥并交换使 用,如果企业内用户有n个,则整个企业共需要n×(n-1) 个密钥,密钥的生成和分发将成为企业信息部门的恶梦。对称加密算法的安全性取决于加密密钥的保存情况,但要求企业中每一个持有密钥的人都保守秘密是不可能 的,他们通常会有意无意的把密钥泄漏出去——如果一个用户使用的密钥被入侵者所获得,入侵者便可以读取该用户密钥加密的所有文档,如果整个企业共用一个加 密密钥,那整个企业文档的保密性便无从谈起

 

DES加密是可以解密的

 1  /// <summary> 
 2         /// DES解密数据 
 3         /// </summary> 
 4         /// <param name="Text"></param> 
 5         /// <param name="sKey"></param> 
 6         /// <returns></returns> 
 7         public static string Decrypt(string Text, string sKey)
 8         {
 9             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
10             int len;
11             len = Text.Length / 2;
12             byte[] inputByteArray = new byte[len];
13             int x, i;
14             for (x = 0; x < len; x++)
15             {
16                 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
17                 inputByteArray[x] = (byte)i;
18             }
19             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
20             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
21             System.IO.MemoryStream ms = new System.IO.MemoryStream();
22             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
23             cs.Write(inputByteArray, 0, inputByteArray.Length);
24             cs.FlushFinalBlock();
25             return Encoding.Default.GetString(ms.ToArray());
26         }
27 
28         #endregion
29    /// <summary> 
30         /// DES加密数据 
31         /// </summary> 
32         /// <param name="Text"></param> 
33         /// <param name="sKey"></param> 
34         /// <returns></returns> 
35         public static string Encrypt(string Text, string sKey)
36         {
37             //DESCryptoServiceProvider命名空间: System.Security.Cryptography;
38             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
39             byte[] inputByteArray;
40             inputByteArray = Encoding.Default.GetBytes(Text);
41             //FormsAuthentication命名空间:System.Web.Security;
42             des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
43             des.IV = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
44             System.IO.MemoryStream ms = new System.IO.MemoryStream();
45             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
46             cs.Write(inputByteArray, 0, inputByteArray.Length);
47             cs.FlushFinalBlock();
48             StringBuilder ret = new StringBuilder();
49             foreach (byte b in ms.ToArray())
50             {
51                 ret.AppendFormat("{0:X2}", b);
52             }
53             return ret.ToString();
54         }

 

这样调用即可

 1   /// <summary>
 2         /// 加密
 3         /// </summary>
 4         /// <param name="Text"></param>
 5         /// <returns></returns>
 6         public static string Encrypt(string Text)
 7         {
 8             return Encrypt(Text, "unity3d");
 9         }
10  /// <summary>
11         /// 解密
12         /// </summary>
13         /// <param name="Text"></param>
14         /// <returns></returns>
15         public static string Decrypt(string Text)
16         {
17             return Decrypt(Text, "unity3d");
18         }