AES加密16进制

项目中用到,此处记下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.IO;
  6 using System.Security.Cryptography;
  7 using System.Text;
  8 
  9 namespace CrossSiteLogin.Common
 10 {
 11     public class AESEncryptHelper
 12     {
 13         /// <summary>
 14         /// 加密
 15         /// </summary>
 16         /// <param name="toEncryptText">原文</param>
 17         /// <param name="sKey">密钥</param>
 18         /// <param name="cipherText">密文</param>
 19         /// <returns></returns>
 20         public static bool Encrypt(string toEncryptText, string sKey, ref string cipherText)
 21         {
 22             try
 23             {
 24                 while (sKey.Length < 16)
 25                 {
 26                     sKey += sKey;
 27                 }
 28                 if (sKey.Length > 16) sKey = sKey.Substring(0, 16);
 29 
 30                 byte[] toEncryptData = UTF8Encoding.UTF8.GetBytes(toEncryptText);
 31                 byte[] bKeyData = UTF8Encoding.UTF8.GetBytes(sKey);
 32 
 33                 cipherText = ByteToHexStr(AESEncrypt(toEncryptData, bKeyData, bKeyData));
 34                 return true;
 35             }
 36             catch (Exception ex)
 37             {
 38                 return false;
 39             }
 40 
 41         }
 42         /// <summary>
 43         /// 解密
 44         /// </summary>
 45         /// <param name="toDecryptText">密文</param>
 46         /// <param name="sKey">密钥</param>
 47         /// <param name="originalText">原文</param>
 48         /// <returns></returns>
 49         public static bool Decrypt(string toDecryptText, string sKey, ref string originalText)
 50         {
 51             try
 52             {
 53                 while (sKey.Length < 16)
 54                 {
 55                     sKey += sKey;
 56                 }
 57                 if (sKey.Length > 16) sKey = sKey.Substring(0, 16);
 58 
 59                 byte[] toEncryptData = HexStrToByte(toDecryptText);
 60                 byte[] bKeyData = UTF8Encoding.UTF8.GetBytes(sKey);
 61 
 62                 originalText = UTF8Encoding.UTF8.GetString(AESDecrypt(toEncryptData, bKeyData, bKeyData));
 63                 return true;
 64             }
 65             catch (Exception ex)
 66             {
 67                 return false;
 68             }
 69         }
 70 
 71         /// <summary>
 72         /// 字符串转16进制字节数组
 73         /// </summary>
 74         /// <param name="hexString"></param>
 75         /// <returns></returns>
 76         private static byte[] HexStrToByte(string hexString)
 77         {
 78             hexString = hexString.Replace(" ", "");
 79             if ((hexString.Length % 2) != 0)
 80                 hexString += " ";
 81             byte[] returnBytes = new byte[hexString.Length / 2];
 82             for (int i = 0; i < returnBytes.Length; i++)
 83                 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
 84             return returnBytes;
 85         }
 86         /// <summary>
 87         /// 字节数组转16进制字符串
 88         /// </summary>
 89         /// <param name="bytes"></param>
 90         /// <returns></returns>
 91         private static string ByteToHexStr(byte[] bytes)
 92         {
 93             string returnStr = "";
 94             if (bytes != null)
 95             {
 96                 for (int i = 0; i < bytes.Length; i++)
 97                 {
 98                     //returnStr += bytes[i].ToString("X2");
 99                     returnStr += bytes[i].ToString("x2");
100                 }
101             }
102             return returnStr;
103         }
104         /// <summary>
105         /// AES加密
106         /// </summary>
107         /// <param name="Data">被加密的明文</param>
108         /// <param name="Key">密钥</param>
109         /// <param name="Vector">向量</param>
110         /// <returns>密文</returns>
111         private static Byte[] AESEncrypt(Byte[] bData, Byte[] bKey, Byte[] bVector)
112         {
113 
114             Byte[] Cryptograph = null; // 加密后的密文
115             Rijndael Aes = Rijndael.Create();
116             try
117             {
118                 // 开辟一块内存流
119                 using (MemoryStream Memory = new MemoryStream())
120                 {
121                     // 把内存流对象包装成加密流对象
122                     using (CryptoStream Encryptor = new CryptoStream(Memory,
123                     Aes.CreateEncryptor(bKey, bVector),
124                     CryptoStreamMode.Write))
125                     {
126                         // 明文数据写入加密流
127                         Encryptor.Write(bData, 0, bData.Length);
128                         Encryptor.FlushFinalBlock();
129 
130                         Cryptograph = Memory.ToArray();
131                     }
132                 }
133             }
134             catch
135             {
136                 Cryptograph = null;
137             }
138 
139             return Cryptograph;
140         }
141         /// <summary>
142         /// AES解密
143         /// </summary>
144         /// <param name="Data">被解密的密文</param>
145         /// <param name="Key">密钥</param>
146         /// <param name="Vector">向量</param>
147         /// <returns>明文</returns>
148         private static Byte[] AESDecrypt(Byte[] bData, Byte[] bKey, Byte[] bVector)
149         {
150 
151             Byte[] original = null; // 解密后的明文
152 
153             Rijndael Aes = Rijndael.Create();
154             try
155             {
156                 // 开辟一块内存流,存储密文
157                 using (MemoryStream Memory = new MemoryStream(bData))
158                 {
159                     // 把内存流对象包装成加密流对象
160                     using (CryptoStream Decryptor = new CryptoStream(Memory,
161                     Aes.CreateDecryptor(bKey, bVector),
162                     CryptoStreamMode.Read))
163                     {
164                         // 明文存储区
165                         using (MemoryStream originalMemory = new MemoryStream())
166                         {
167                             Byte[] Buffer = new Byte[1024];
168                             Int32 readBytes = 0;
169                             while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
170                             {
171                                 originalMemory.Write(Buffer, 0, readBytes);
172                             }
173 
174                             original = originalMemory.ToArray();
175                         }
176                     }
177                 }
178             }
179             catch
180             {
181                 original = null;
182             }
183 
184             return original;
185         }
186     }
187 }
原文地址:https://www.cnblogs.com/ryhan/p/2660545.html