标题: des加密与解密C#源码分享 [打印本页]

作者: wqdqdq    时间: 2019-1-30 11:42
标题: des加密与解密C#源码分享
c#des加密与解密的相关介绍:
DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1977年被美国家标准局确定为联邦资料处理标准(FIPS),并授权在非密级通信中使用,随后该算法在国际上广泛流传开来。需要注意的是,在某些文献中,作为算法的DES称为数据加密算法(Data Encryption Algorithm,DEA),已与作为标准的DES区分开来。




c#源码:
  1. public static string 加密(string input, string sKey)//来自wqdqdq
  2.         {
  3.             byte[] data = Encoding.UTF8.GetBytes(input);
  4.             using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
  5.             {
  6.                 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  7.                 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  8.                 ICryptoTransform desencrypt = des.CreateEncryptor();
  9.                 byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
  10.                 return BitConverter.ToString(result);
  11.             }
  12.         }
  13.         public static string 解密(string input, string sKey)
  14.         {
  15.             try
  16.             {
  17.                 string[] sInput = input.Split("-".ToCharArray());
  18.                 byte[] data = new byte[sInput.Length];
  19.                 for (int i = 0; i < sInput.Length; i++)
  20.                 {
  21.                     data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
  22.                 }
  23.                 using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
  24.                 {
  25.                     des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  26.                     des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  27.                     ICryptoTransform desencrypt = des.CreateDecryptor();
  28.                     byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
  29.                     Console.Write(result); return Encoding.UTF8.GetString(result);
  30.                 }
  31.             }
  32.             catch { byte[] apk = System.Text.Encoding.Default.GetBytes("错误"); return Encoding.UTF8.GetString(apk); }
复制代码

全部资料51hei下载地址:
des加密.zip (55.52 KB, 下载次数: 10)






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1