大米软件

大米专注电子商务解决方案,及其大型网站构架

 

DES 的加密实现

引用命名空间
using System.IO;
using System.Security.Cryptography;

使用类的介绍
System.Security.Cryptography.DES 的抽象类
表示所有 System.Security.Cryptography.DES 实现都必须从中派生的数据加密标准 (DES) 算法的基类。

System.Security.Cryptography.DESCryptoServiceProvider : DES
定义访问数据加密标准 (System.Security.Cryptography.DES) 算法的加密服务提供程序 (CSP) 版本的包装对象。无法继承此类。

我们可以用这个DES提供程序加解密byte数组

1.  DES提供程序的入口参数:
     Key   -- 加密key,注意了,是8个字符(即是byte[8]数组),64位,中文也是1个字符
     IV     --  对称算法的初始化向量IV,下面的位数目不小于8个字符(即是byte[8]数组),64位,大于8个的长度不限制,但是也只有前8位有效的
     Data -- 要加密的byte数组 

2.  加密方法的代码如下:

    public static string Encode(string data)
  {
   DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

   #region 得到key 和 IV
   string mykey = KEY_64;
   string myiv = IV_64;

   //下面代码说明,因为key必须是8位的字符串,这里检测并生成真正的key

   int KeyLength = cryptoProvider.Key.Length;

   if (KEY_64.Length > KeyLength)
    mykey = KEY_64.Substring(0, KeyLength);
   else if (KEY_64.Length < KeyLength)
    mykey = KEY_64.PadRight(KeyLength, ' ');

   //其实 大于8个的长度可以不用截取,但是小于8个的是必须处理的

   int IVLength = cryptoProvider.IV.Length;

   if (IV_64.Length > IVLength)
    myiv = IV_64.Substring(0, IVLength);
   else if (IV_64.Length < IVLength)
    myiv = IV_64.PadRight(IVLength, ' ');

   byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(mykey);
   byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(myiv);
   #endregion

   MemoryStream ms = new MemoryStream();//内存流
   CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
   //定义将内存数据流链接到加密转换的流,模式为加密

   #region 方法一

   //   StreamWriter sw = new StreamWriter(cst);//在加密转换的流上定义一个写对象
   //   sw.Write(data);//写出加密后的数据
   //   sw.Flush();
   //   cst.FlushFinalBlock();//清除缓存,才将缓存中的数据写到内存流中。
   
   #endregion

   #region 方法二
   cst.Write(System.Text.UTF8Encoding.UTF8.GetBytes(data), 0, System.Text.UTF8Encoding.UTF8.GetBytes(data).Length);
   cst.FlushFinalBlock();//清除缓存,才将缓存中的数据写到内存流中。
   #endregion

   ms.Close();
   byte[] bytOut = ms.ToArray();
   return Convert.ToBase64String(bytOut);
   //下面的这种有没有关闭内存流的缺点,也可以
   //return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);//把加密后的数据从内存流中读出来

  }

3.  解密方法的代码

  public static string Decode(string data)
  {
   DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

   #region 得到 key 和 IV

   string mykey = KEY_64;
   string myiv = IV_64;
   int KeyLength = cryptoProvider.Key.Length;

   if (KEY_64.Length > KeyLength)
    mykey = KEY_64.Substring(0, KeyLength);
   else if (KEY_64.Length < KeyLength)
    mykey = KEY_64.PadRight(KeyLength, ' ');

   int IVLength = cryptoProvider.IV.Length;

   if (IV_64.Length > IVLength)
    myiv = IV_64.Substring(0, IVLength);
   else if (IV_64.Length < IVLength)
    myiv = IV_64.PadRight(IVLength, ' ');

   byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(mykey);
   byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(myiv);

   #endregion

   byte[] byEnc;
   try
   {
    byEnc = Convert.FromBase64String(data);//将加密字符串转换为byte数组
   }
   catch
   {
    return null;
   }

   MemoryStream ms = new MemoryStream(byEnc);//将byte数组加载到内存流中
   CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
   //定义将内存数据流链接到加密转换的流,模式为解密

   StreamReader sr = new StreamReader(cst);//在加密转换的流上定义一个读对象
   return sr.ReadToEnd();
  }
 }
}


代码下载

posted on 2007-04-30 16:31  大米  阅读(715)  评论(0编辑  收藏  举报

导航