.net7 AesCcm加密算法测试
今天测试了.net core AesCcm加密算法,并做了封装,测试过程做个记录。
首先上封装成果:
/// <summary> /// AES加密操作类 /// </summary> public static class AESOperation { /// <summary> /// AES 加密 /// </summary> /// <param name="value">明文(待加密)</param> /// <param name="key">密钥,长度16、24 或 32 字节</param> /// <param name="nonce">7、8、9、10、11、12 或 13 个字节</param> /// <returns></returns> public static (string, string) AesEncrypt(string value, string key, string nonce) { byte[] nonceByte = Encoding.UTF8.GetBytes(nonce);//对于具有相同密钥的每个操作,它应该是唯一值 byte[] plaintext = Encoding.UTF8.GetBytes(value);//要加密的内容。 byte[] ciphertext = new byte[plaintext.Length];//接收加密内容的字节数组。 byte[] tag = new byte[16];//接收生成的身份验证标记的字节数组。4、6、8、10、12、14 或 16 个字节 byte[]? associatedData = default;//与此消息关联的额外数据。该数据还必须在解密过程中提供。 using (AesCcm ccm = new(Encoding.UTF8.GetBytes(key))) { ccm.Encrypt(nonceByte, plaintext, ciphertext, tag, associatedData); } return (Convert.ToBase64String(ciphertext, 0, ciphertext.Length), Convert.ToBase64String(tag)); } /// <summary> /// AES 解密 /// </summary> /// <param name="value">密文(待解密)</param> /// <param name="key">密钥,长度16、24 或 32 字节</param> /// <param name="nonce">7、8、9、10、11、12 或 13 个字节</param> /// <returns></returns> public static string AesDecrypt(string value, string key, string nonce,string tagValue) { byte[] nonceByte = Encoding.UTF8.GetBytes(nonce);//与此消息关联的 nonce。它必须与加密过程中提供的值相匹配 byte[] ciphertext = Convert.FromBase64String(value);//要解密的加密内容。 byte[] plaintext = new byte[ciphertext.Length];//接收解密内容的字节数组。 byte[] tag = Convert.FromBase64String(tagValue);//加密过程中为此消息生成的身份验证标记。4、6、8、10、12、14 或 16 个字节 byte[]? associatedData = default;//与此消息关联的额外数据。该数据必须与加密过程中提供的值相匹配。 using (AesCcm ccm = new(Encoding.UTF8.GetBytes(key))) { ccm.Decrypt(nonceByte, ciphertext,tag,plaintext,associatedData); } return Encoding.UTF8.GetString(plaintext); } }
测试结果:
string value1 = AESOperation.AesEncrypt("祖国强盛", "qqqq7897qqqq7897", "r1amr1a").Item1; string value2 = AESOperation.AesEncrypt("12", "qqqq7897qqqq7897", "r1amr1a").Item1; string value3 = AESOperation.AesEncrypt("123", "qqqq7897qqqq7897", "r1amr1a").Item1; string value4 = AESOperation.AesEncrypt("1234", "qqqq7897qqqq7897", "r1amr1aq").Item1; string value5 = AESOperation.AesEncrypt("12347896", "qqqq7897qqqq7897", "r1amr1aq").Item1; string value6 = AESOperation.AesEncrypt("12347896XXXX", "qqqq7897qqqq7897", "0r1amr1aqa").Item1; string value7 = AESOperation.AesEncrypt("12347896XXXXOOOO", "qqqq7897qqqq7897", "0r1amr1aqa").Item1; string tag1 = AESOperation.AesEncrypt("祖国强盛", "qqqq7897qqqq7897", "r1amr1a").Item2; string tag2 = AESOperation.AesEncrypt("12", "qqqq7897qqqq7897", "r1amr1a").Item2; string tag3 = AESOperation.AesEncrypt("123", "qqqq7897qqqq7897", "r1amr1a").Item2; string tag4 = AESOperation.AesEncrypt("1234", "qqqq7897qqqq7897", "r1amr1aq").Item2; string tag5 = AESOperation.AesEncrypt("12347896", "qqqq7897qqqq7897", "r1amr1aq").Item2; string tag6 = AESOperation.AesEncrypt("12347896XXXX", "qqqq7897qqqq7897", "0r1amr1aqa").Item2; string tag7 = AESOperation.AesEncrypt("12347896XXXXOOOO", "qqqq7897qqqq7897", "0r1amr1aqa").Item2; Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); Console.WriteLine(value4); Console.WriteLine(value5); Console.WriteLine(value6); Console.WriteLine(value7); Console.WriteLine("----tag"); Console.WriteLine(tag1); Console.WriteLine(tag2); Console.WriteLine(tag3); Console.WriteLine(tag4); Console.WriteLine(tag5); Console.WriteLine(tag6); Console.WriteLine(tag7); Console.WriteLine("----解密"); string value8 = AESOperation.AesDecrypt(value1, "qqqq7897qqqq7897", "r1amr1a", tag1); string value9 = AESOperation.AesDecrypt(value2, "qqqq7897qqqq7897", "r1amr1a", tag2); string value10 = AESOperation.AesDecrypt(value3, "qqqq7897qqqq7897", "r1amr1a", tag3); string value11 = AESOperation.AesDecrypt(value4, "qqqq7897qqqq7897", "r1amr1aq", tag4); string value12 = AESOperation.AesDecrypt(value5, "qqqq7897qqqq7897", "r1amr1aq", tag5); string value13 = AESOperation.AesDecrypt(value6, "qqqq7897qqqq7897", "0r1amr1aqa", tag6); string value14 = AESOperation.AesDecrypt(value7, "qqqq7897qqqq7897", "0r1amr1aqa", tag7); Console.WriteLine(value8); Console.WriteLine(value9); Console.WriteLine(value10); Console.WriteLine(value11); Console.WriteLine(value12); Console.WriteLine(value13); Console.WriteLine(value14); Console.Read();
控制台输出: