项目中token在传输过程中采用了AES加密, 网上找到的两篇博文都有写问题,在这里记录一下.Net Core 2.2代码中AES加解密的使用:
//AES加密 传入,要加密的串和, 解密key public static string AESEncrypt(string input, string key = "dataplatform2019") { var encryptKey = Encoding.UTF8.GetBytes(key); var iv = Encoding.UTF8.GetBytes("1012132405963708"); //偏移量,最小为16 using (var aesAlg = Aes.Create()) {using (var encryptor = aesAlg.CreateEncryptor(encryptKey, iv)) { using (var msEncrypt = new MemoryStream()) { using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) using (var swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(input); } var decryptedContent = msEncrypt.ToArray(); return Convert.ToBase64String(decryptedContent); } } } }
解密过程:
public static string DecryptString(string cipherText) { var fullCipher = Convert.FromBase64String(cipherText); var iv = Encoding.UTF8.GetBytes("1012132405963708"); var key = Encoding.UTF8.GetBytes("dataplatform2019"); using (var aesAlg = Aes.Create()) { using (var decryptor = aesAlg.CreateDecryptor(key, iv)) { string result; using (var msDecrypt = new MemoryStream(fullCipher)) { using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (var srDecrypt = new StreamReader(csDecrypt)) { result = srDecrypt.ReadToEnd(); } } } return result; } } }