des算法的C#实现

DES是Data Encryption Standard(数据加密标准)的缩写。它是一种用56位密钥来加密64位数据的方法。它的原理和算法就不在这里介绍了,网上这方面的资料很多。下面是我参照别人的代码修改的一个DES加密和解密的类。供大家参考:

 

ExpandedBlockStart.gif 代码
 1  public   class  EncryptUtility
 2  {
 3           #region  DES
 4           ///   <summary>
 5           ///  DES加密
 6           ///   </summary>
 7           ///   <param name="code"> 加密字符串 </param>
 8           ///   <param name="key"> 密钥 </param>
 9           ///   <returns></returns>
10           public   static   string  DesEncrypt( string  code,  string  key)
11          {
12               string  iv  =  StringUtility.Reverse(key);
13               return  DesEncrypt(code, key, iv);
14          }
15 
16           ///   <summary>
17           ///  DES加密
18           ///   </summary>
19           ///   <param name="code"> 加密字符串 </param>
20           ///   <param name="key"> 密钥 </param>
21           ///   <param name="iv"> 初始化向量 </param>
22           ///   <returns></returns>
23           public   static   string  DesEncrypt( string  code,  string  key,  string  iv)
24          {
25              DESCryptoServiceProvider des  =   new  DESCryptoServiceProvider();
26               byte [] inputByteArray  =  Encoding.Default.GetBytes(code);
27              des.Key  =  ASCIIEncoding.ASCII.GetBytes(key);
28              des.IV  =  ASCIIEncoding.ASCII.GetBytes(iv);
29              MemoryStream ms  =   new  MemoryStream();
30              CryptoStream cs  =   new  CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
31              cs.Write(inputByteArray,  0 , inputByteArray.Length);
32              cs.FlushFinalBlock();
33              StringBuilder ret  =   new  StringBuilder();
34               foreach  ( byte  b  in  ms.ToArray())
35              {
36                  ret.AppendFormat( " {0:X2} " , b);
37              }
38              ms.Dispose();
39              cs.Dispose();
40               // ret.ToString();
41               return  ret.ToString();
42          }
43 
44 
45           ///   <summary>
46           ///  DES解密
47           ///   </summary>
48           ///   <param name="code"> 解密字符串 </param>
49           ///   <param name="key"> 密钥 </param>
50           ///   <param name="key"></param>
51           ///   <returns></returns>
52           public   static   string  DesDecrypt( string  code,  string  key)
53          {
54               string  iv  =  StringUtility.Reverse(key);
55               return  DesDecrypt(code, key, iv);
56          }
57 
58 
59           ///   <summary>
60           ///  DES解密
61           ///   </summary>
62           ///   <param name="code"> 解密字符串 </param>
63           ///   <param name="key"> 密钥 </param>
64           ///   <param name="iv"> 初始化向量 </param>
65           ///   <returns></returns>
66           public   static   string  DesDecrypt( string  code,  string  key,  string  iv)
67          {
68              DESCryptoServiceProvider des  =   new  DESCryptoServiceProvider();
69               byte [] inputByteArray  =   new   byte [code.Length  /   2 ];
70               for  ( int  x  =   0 ; x  <  code.Length  /   2 ; x ++ )
71              {
72                   int  i  =  (Convert.ToInt32(code.Substring(x  *   2 2 ),  16 ));
73                  inputByteArray[x]  =  ( byte )i;
74              }
75              des.Key  =  ASCIIEncoding.ASCII.GetBytes(key);
76              des.IV  =  ASCIIEncoding.ASCII.GetBytes(iv);
77              MemoryStream ms  =   new  MemoryStream();
78              CryptoStream cs  =   new  CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
79              cs.Write(inputByteArray,  0 , inputByteArray.Length);
80              cs.FlushFinalBlock();
81              cs.Dispose();
82              StringBuilder ret  =   new  StringBuilder();
83               return  System.Text.Encoding.Default.GetString(ms.ToArray());
84          }
85           #endregion
86  }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值