.net7 AesCng 加密解密封装,仅支持Windows平台
/// <summary> /// AesCng 加密 /// 仅支持Windows平台 /// </summary> /// <param name="value">明文(待加密)</param> /// <param name="keyValue">密钥大小为 128 位、192 位和 256 位</param> /// <returns></returns> public static (string, string) AesCngEncrypt(string value, string keyValue) { if (OperatingSystem.IsWindows()) { using (AesCng aesCng = new()) { aesCng.Key = Encoding.UTF8.GetBytes(keyValue); aesCng.GenerateIV(); using (MemoryStream memoryStream = new()) { using (CryptoStream cryptoStream = new(memoryStream, aesCng.CreateEncryptor(), CryptoStreamMode.Write)) { byte[] plainText = Encoding.UTF8.GetBytes(value); cryptoStream.Write(plainText, 0, plainText.Length); cryptoStream.FlushFinalBlock(); return (Convert.ToBase64String(memoryStream.ToArray()), Convert.ToBase64String(aesCng.IV)); } } } } else { return (string.Empty, string.Empty); } } /// <summary> /// AesCng 解密 /// 仅支持Windows平台 /// </summary> /// <param name="value">密文(待解密)</param> /// <param name="keyValue">密钥大小为 128 位、192 位和 256 位</param> /// <param name="iv">加密时的随机初始化向量 </param> /// <returns></returns> public static string AesCngDecrypt(string value, string keyValue, string iv) { if (OperatingSystem.IsWindows()) { using (AesCng aesCng = new()) { aesCng.Key = Encoding.UTF8.GetBytes(keyValue); aesCng.IV = Convert.FromBase64String(iv); using (MemoryStream memoryStream = new(Convert.FromBase64String(value))) { using (CryptoStream cryptoStream = new(memoryStream, aesCng.CreateDecryptor(), CryptoStreamMode.Read)) { using (MemoryStream plainStream = new()) { cryptoStream.CopyTo(plainStream); return Encoding.UTF8.GetString(plainStream.ToArray()); } } } } } else { return string.Empty; } }
测试截图:
测试代码:
Console.WriteLine("----AesCngEncrypt加密"); (value1, tag1) = AESOperation.AesCngEncrypt("祖国强盛", "qqqq7897qqqq7897"); (value2, tag2) = AESOperation.AesCngEncrypt("12", "qqqq7897qqqq7897"); (value3, tag3) = AESOperation.AesCngEncrypt("123", "qqqq7897qqqq7897"); (value4, tag4) = AESOperation.AesCngEncrypt("1234", "qqqq7897qqqq7897"); (value5, tag5) = AESOperation.AesCngEncrypt("12347896", "qqqq7897qqqq7897"); (value6, tag6) = AESOperation.AesCngEncrypt("12347896XXXX", "qqqq7897qqqq7897"); (value7, tag7) = AESOperation.AesCngEncrypt("12347896XXXXOOOO", "qqqq7897qqqq7897"); Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); Console.WriteLine(value4); Console.WriteLine(value5); Console.WriteLine(value6); Console.WriteLine(value7); Console.WriteLine("----iv"); Console.WriteLine(tag1); Console.WriteLine(tag2); Console.WriteLine(tag3); Console.WriteLine(tag4); Console.WriteLine(tag5); Console.WriteLine(tag6); Console.WriteLine(tag7); Console.WriteLine("----AesCngEncrypt解密"); value8 = AESOperation.AesCngDecrypt(value1, "qqqq7897qqqq7897", tag1); value9 = AESOperation.AesCngDecrypt(value2, "qqqq7897qqqq7897", tag2); value10 = AESOperation.AesCngDecrypt(value3, "qqqq7897qqqq7897", tag3); value11 = AESOperation.AesCngDecrypt(value4, "qqqq7897qqqq7897", tag4); value12 = AESOperation.AesCngDecrypt(value5, "qqqq7897qqqq7897", tag5); value13 = AESOperation.AesCngDecrypt(value6, "qqqq7897qqqq7897", tag6); value14 = AESOperation.AesCngDecrypt(value7, "qqqq7897qqqq7897", tag7); Console.WriteLine(value8); Console.WriteLine(value9); Console.WriteLine(value10); Console.WriteLine(value11); Console.WriteLine(value12); Console.WriteLine(value13); Console.WriteLine(value14); Console.Read();