Model

1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Security.Cryptography; 6 using System.Text; 7 using System.Threading.Tasks; 8 using System.Web; 9 10 namespace VerificationCode.Code 11 { 12 public class VerificationCodeAESHelp 13 { 14 /// <summary> 15 /// Key123Ace#321Key 16 /// </summary> 17 private static readonly string _AESKEY = "qwertyuiopasdfghjklzxcvbnm123456"; 18 19 /// <summary> 20 /// slide 21 /// </summary> 22 public const string _SlideCode = "slidecode."; 23 24 /// <summary> 25 ///验证码cookie 26 /// </summary> 27 public const string _YZM = "_YZM."; 28 29 30 private HttpContextBase _httpContextAccessor; 31 32 public VerificationCodeAESHelp(HttpContextBase httpContextAccessor) 33 { 34 this._httpContextAccessor = httpContextAccessor; 35 } 36 37 38 /// <summary> 39 /// AES加密返回base64字符串 40 /// </summary> 41 public string AES_Encrypt_Return_Base64String(string str) 42 { 43 string base64Str = AESEncrypt(str, _AESKEY); 44 45 return base64Str; 46 } 47 48 49 /// <summary> 50 /// AES解密返回string 51 /// </summary> 52 public string AES_Decrypt_Return_String(string str) 53 { 54 return AESDecrypt(str, _AESKEY); 55 } 56 57 58 59 #region AES 60 61 private static string SubString(string sourceStr, int startIndex, int length) 62 { 63 string str; 64 if (string.IsNullOrEmpty(sourceStr)) 65 { 66 str = ""; 67 } 68 else 69 { 70 str = (sourceStr.Length < startIndex + length ? sourceStr.Substring(startIndex) : sourceStr.Substring(startIndex, length)); 71 } 72 return str; 73 } 74 75 76 private static byte[] _aeskeys = new byte[] { 18, 52, 86, 120, 144, 171, 205, 239, 18, 52, 86, 120, 144, 171, 205, 239 }; 77 /// <summary> 78 /// 加密 79 /// </summary> 80 /// <param name="input"></param> 81 /// <param name="key"></param> 82 /// <returns></returns> 83 private static string AESEncrypt(string encryptStr, string encryptKey) 84 { 85 86 string base64String; 87 if (!string.IsNullOrWhiteSpace(encryptStr)) 88 { 89 encryptKey = SubString(encryptKey, 0, 32); 90 encryptKey = encryptKey.PadRight(32, ' '); 91 SymmetricAlgorithm bytes = Rijndael.Create(); 92 byte[] numArray = Encoding.UTF8.GetBytes(encryptStr); 93 bytes.Key = Encoding.UTF8.GetBytes(encryptKey); 94 bytes.IV = _aeskeys; 95 byte[] array = null; 96 MemoryStream memoryStream = new MemoryStream(); 97 try 98 { 99 CryptoStream cryptoStream = new CryptoStream(memoryStream, bytes.CreateEncryptor(), CryptoStreamMode.Write); 100 try 101 { 102 cryptoStream.Write(numArray, 0, numArray.Length); 103 cryptoStream.FlushFinalBlock(); 104 array = memoryStream.ToArray(); 105 cryptoStream.Close(); 106 memoryStream.Close(); 107 } 108 finally 109 { 110 if (cryptoStream != null) 111 { 112 ((IDisposable)cryptoStream).Dispose(); 113 } 114 } 115 } 116 finally 117 { 118 if (memoryStream != null) 119 { 120 ((IDisposable)memoryStream).Dispose(); 121 } 122 } 123 base64String = Convert.ToBase64String(array); 124 } 125 else 126 { 127 base64String = string.Empty; 128 } 129 return base64String; 130 131 } 132 133 134 135 /// <summary> 136 /// 解密 137 /// </summary> 138 /// <param name="input"></param> 139 /// <param name="key"></param> 140 /// <returns></returns> 141 private static string AESDecrypt(string decryptStr, string decryptKey) 142 { 143 144 string empty; 145 if (!string.IsNullOrWhiteSpace(decryptStr)) 146 { 147 decryptKey = SubString(decryptKey, 0, 32); 148 decryptKey = decryptKey.PadRight(32, ' '); 149 byte[] numArray = Convert.FromBase64String(decryptStr); 150 SymmetricAlgorithm bytes = Rijndael.Create(); 151 bytes.Key = Encoding.UTF8.GetBytes(decryptKey); 152 bytes.IV = _aeskeys; 153 byte[] numArray1 = new byte[numArray.Length]; 154 MemoryStream memoryStream = new MemoryStream(numArray); 155 try 156 { 157 CryptoStream cryptoStream = new CryptoStream(memoryStream, bytes.CreateDecryptor(), CryptoStreamMode.Read); 158 try 159 { 160 cryptoStream.Read(numArray1, 0, numArray1.Length); 161 cryptoStream.Close(); 162 memoryStream.Close(); 163 } 164 finally 165 { 166 if (cryptoStream != null) 167 { 168 ((IDisposable)cryptoStream).Dispose(); 169 } 170 } 171 } 172 finally 173 { 174 if (memoryStream != null) 175 { 176 ((IDisposable)memoryStream).Dispose(); 177 } 178 } 179 empty = Encoding.UTF8.GetString(numArray1).Replace("