1 /** 2 *┌──────────────────────────────────────────────────────────────┐ 3 *│ 描 述:AES加解密帮助类 4 *│ 作 者:yilezhu 5 *│ 版 本:1.0 6 *│ 创建时间:2019/1/1 21:11:02 7 *└──────────────────────────────────────────────────────────────┘ 8 *┌──────────────────────────────────────────────────────────────┐ 9 *│ 命名空间: Helper 10 *│ 类 名: DESEncryptHelper 11 *└──────────────────────────────────────────────────────────────┘ 12 */ 13 using System; 14 using System.IO; 15 using System.Text; 16 using System.Security.Cryptography; 17 18 namespace Czar.Cms.Core.Helper 19 { 20 /// <summary> 21 /// AES加密解密 22 /// </summary> 23 public class AESEncryptHelper 24 { 25 //默认密钥向量 26 private static byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F }; 27 28 #region ========加密======== 29 public static string Encode(string encryptString, string encryptKey) 30 { 31 encryptKey = GetSubString(encryptKey, 0, 32, ""); 32 encryptKey = encryptKey.PadRight(32, ‘ ‘); 33 34 RijndaelManaged rijndaelProvider = new RijndaelManaged(); 35 rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32)); 36 rijndaelProvider.IV = Keys; 37 ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor(); 38 39 byte[] inputData = Encoding.UTF8.GetBytes(encryptString); 40 byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length); 41 42 return Convert.ToBase64String(encryptedData); 43 } 44 #endregion 45 46 #region ========解密======== 47 public static string Decode(string decryptString, string decryptKey) 48 { 49 try 50 { 51 decryptKey = GetSubString(decryptKey, 0, 32, ""); 52 decryptKey = decryptKey.PadRight(32, ‘ ‘); 53 54 RijndaelManaged rijndaelProvider = new RijndaelManaged(); 55 rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey); 56 rijndaelProvider.IV = Keys; 57 ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor(); 58 59 byte[] inputData = Convert.FromBase64String(decryptString); 60 byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length); 61 62 return Encoding.UTF8.GetString(decryptedData); 63 } 64 catch 65 { 66 return ""; 67 } 68 69 } 70 #endregion 71 72 public static string GetSubString(string p_SrcString, int p_StartIndex, int p_Length, string p_TailString) 73 { 74 string myResult = p_SrcString; 75 76 Byte[] bComments = Encoding.UTF8.GetBytes(p_SrcString); 77 foreach (char c in Encoding.UTF8.GetChars(bComments)) 78 { //当是日文或韩文时(注:中文的范围:\u4e00 - \u9fa5, 日文在\u0800 - \u4e00, 韩文为\xAC00-\xD7A3) 79 if ((c > ‘\u0800‘ && c < ‘\u4e00‘) || (c > ‘\xAC00‘ && c < ‘\xD7A3‘)) 80 { 81 //if (System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\u0800-\u4e00]+") || System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\xAC00-\xD7A3]+")) 82 //当截取的起始位置超出字段串长度时 83 if (p_StartIndex >= p_SrcString.Length) 84 return ""; 85 else 86 return p_SrcString.Substring(p_StartIndex, 87 ((p_Length + p_StartIndex) > p_SrcString.Length) ? (p_SrcString.Length - p_StartIndex) : p_Length); 88 } 89 } 90 91 if (p_Length >= 0) 92 { 93 byte[] bsSrcString = Encoding.Default.GetBytes(p_SrcString); 94 95 //当字符串长度大于起始位置 96 if (bsSrcString.Length > p_StartIndex) 97 { 98 int p_EndIndex = bsSrcString.Length; 99 100 //当要截取的长度在字符串的有效长度范围内 101 if (bsSrcString.Length > (p_StartIndex + p_Length)) 102 { 103 p_EndIndex = p_Length + p_StartIndex; 104 } 105 else 106 { //当不在有效范围内时,只取到字符串的结尾 107 108 p_Length = bsSrcString.Length - p_StartIndex; 109 p_TailString = ""; 110 } 111 112 int nRealLength = p_Length; 113 int[] anResultFlag = new int[p_Length]; 114 byte[] bsResult = null; 115 116 int nFlag = 0; 117 for (int i = p_StartIndex; i < p_EndIndex; i++) 118 { 119 if (bsSrcString[i] > 127) 120 { 121 nFlag++; 122 if (nFlag == 3) 123 nFlag = 1; 124 } 125 else 126 nFlag = 0; 127 128 anResultFlag[i] = nFlag; 129 } 130 131 if ((bsSrcString[p_EndIndex - 1] > 127) && (anResultFlag[p_Length - 1] == 1)) 132 nRealLength = p_Length + 1; 133 134 bsResult = new byte[nRealLength]; 135 136 Array.Copy(bsSrcString, p_StartIndex, bsResult, 0, nRealLength); 137 138 myResult = Encoding.Default.GetString(bsResult); 139 myResult = myResult + p_TailString; 140 } 141 } 142 143 return myResult; 144 } 145 146 } 147 }
引自:依乐祝CMS项目——https://www.cnblogs.com/yilezhu/p/9977862.html