架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 23373|回复: 0

[资料] c#之AES加密

[复制链接]
发表于 2016-3-30 15:42:45 | 显示全部楼层 |阅读模式
  1. public static class AESCryptoProvider
  2.     {
  3.         public const string guid = "46d2e413-a154-4521-87de-e45066216bfe";
  4.         /// <summary>
  5.         /// AES加密
  6.         /// </summary>
  7.         /// <param name="Data">被加密的明文</param>
  8.         /// <param name="Key">密钥</param>
  9.         /// <param name="Vector">向量</param>
  10.         /// <returns>密文</returns>
  11.         public static string Encrypt(string Data, String Key = guid, String Vector = guid)
  12.         {
  13.             var _data = Encoding.Unicode.GetBytes(Data);
  14.             Byte[] bKey = new Byte[32];
  15.             Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
  16.             Byte[] bVector = new Byte[16];
  17.             Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);

  18.             Byte[] Cryptograph = null; // 加密后的密文

  19.             Rijndael Aes = Rijndael.Create();
  20.             try
  21.             {
  22.                 // 开辟一块内存流
  23.                 using (MemoryStream Memory = new MemoryStream())
  24.                 {
  25.                     // 把内存流对象包装成加密流对象
  26.                     using (CryptoStream Encryptor = new CryptoStream(Memory,
  27.                      Aes.CreateEncryptor(bKey, bVector),
  28.                      CryptoStreamMode.Write))
  29.                     {
  30.                         // 明文数据写入加密流
  31.                         Encryptor.Write(_data, 0, _data.Length);
  32.                         Encryptor.FlushFinalBlock();

  33.                         Cryptograph = Memory.ToArray();
  34.                     }
  35.                 }
  36.             }
  37.             catch
  38.             {
  39.                 Cryptograph = null;
  40.             }

  41.             return Convert.ToBase64String(Cryptograph);
  42.         }

  43.         /// <summary>
  44.         /// AES解密
  45.         /// </summary>
  46.         /// <param name="Data">被解密的密文</param>
  47.         /// <param name="Key">密钥</param>
  48.         /// <param name="Vector">向量</param>
  49.         /// <returns>明文</returns>
  50.         public static string Decrypt(string Data, String Key = guid, String Vector = guid)
  51.         {
  52.             Byte[] bKey = new Byte[32];
  53.             Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
  54.             Byte[] bVector = new Byte[16];
  55.             Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);

  56.             UnicodeEncoding ByteConverter = new UnicodeEncoding();

  57.             Byte[] original = null; // 解密后的明文

  58.             Rijndael Aes = Rijndael.Create();
  59.             try
  60.             {
  61.                 // 开辟一块内存流,存储密文
  62.                 using (MemoryStream Memory = new MemoryStream(Convert.FromBase64String(Data)))
  63.                 {
  64.                     // 把内存流对象包装成加密流对象
  65.                     using (CryptoStream Decryptor = new CryptoStream(Memory,
  66.                     Aes.CreateDecryptor(bKey, bVector),
  67.                     CryptoStreamMode.Read))
  68.                     {
  69.                         // 明文存储区
  70.                         using (MemoryStream originalMemory = new MemoryStream())
  71.                         {
  72.                             Byte[] Buffer = new Byte[1024];
  73.                             Int32 readBytes = 0;
  74.                             while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
  75.                             {
  76.                                 originalMemory.Write(Buffer, 0, readBytes);
  77.                             }

  78.                             original = originalMemory.ToArray();
  79.                         }
  80.                     }
  81.                 }
  82.             }
  83.             catch
  84.             {
  85.                 original = null;
  86.             }

  87.             return ByteConverter.GetString(original);
  88.         }

  89.         /// <summary>
  90.         /// AES解密
  91.         /// </summary>
  92.         /// <param name="Data">被解密的密文</param>
  93.         /// <param name="Key">密钥</param>
  94.         /// <param name="Vector">向量</param>
  95.         /// <returns>明文</returns>
  96.         public static string Decrypt_js(string toDecrypt, string key, string iv)
  97.         {
  98.             byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
  99.             byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
  100.             byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
  101.             RijndaelManaged rDel = new RijndaelManaged();
  102.             rDel.Key = keyArray;
  103.             rDel.IV = ivArray;
  104.             rDel.Mode = CipherMode.CBC;
  105.             rDel.Padding = PaddingMode.Zeros;
  106.             ICryptoTransform cTransform = rDel.CreateDecryptor();
  107.             byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  108.             return UTF8Encoding.UTF8.GetString(resultArray);
  109.         }

  110.         /// <summary>
  111.         /// AES加密
  112.         /// </summary>
  113.         /// <param name="Data">被加密的明文</param>
  114.         /// <param name="Key">密钥</param>
  115.         /// <param name="Vector">向量</param>
  116.         /// <returns>密文</returns>
  117.         public static string Encrypt_js(string toEncrypt, string key, string iv)
  118.         {
  119.             byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
  120.             byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
  121.             byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
  122.             RijndaelManaged rDel = new RijndaelManaged();
  123.             rDel.Key = keyArray;
  124.             rDel.IV = ivArray;
  125.             rDel.Mode = CipherMode.CBC;
  126.             rDel.Padding = PaddingMode.Zeros;
  127.             ICryptoTransform cTransform = rDel.CreateEncryptor();
  128.             byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  129.             return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  130.         }

  131.     }
复制代码






上一篇:c# Windows 消息列表,枚举
下一篇:vshost32.exe 已停止工作 的解决办法
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-4-6 12:33

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表