Jan*_*ann 5 c# encryption aes-gcm
我刚刚注意到 .NET Standard 2.1/.NET Core 3.0 终于添加了一个用于 AES-GCM 加密的类。
然而,它的 API 似乎与通常的 .NET 加密类略有不同:它的Encrypt
功能要求为密文和标签预先分配字节数组,而不是自己提供它们。不幸的是,文档中没有显示正确使用该类的示例。
我知道如何在理论上计算 AES 加密的预期密文大小,但我想知道这是否真的是“猜测”密文缓冲区大小的预期方法。通常加密库提供处理这些计算的函数。
有人有关于如何使用正确加密字节数组的示例AesGcm
吗?
Jan*_*ann 13
我现在想通了。
我忘了在 GCM 中,密文与明文的长度相同;与 CBC 等其他加密模式相反,不需要填充。随机数和标签长度分别由NonceByteSizes
和 的TagByteSizes
属性决定AesGcm
。
使用它,可以通过以下方式进行加密:
public string Encrypt(string plain)
{
// Get bytes of plaintext string
byte[] plainBytes = Encoding.UTF8.GetBytes(plain);
// Get parameter sizes
int nonceSize = AesGcm.NonceByteSizes.MaxSize;
int tagSize = AesGcm.TagByteSizes.MaxSize;
int cipherSize = plainBytes.Length;
// We write everything into one big array for easier encoding
int encryptedDataLength = 4 + nonceSize + 4 + tagSize + cipherSize;
Span<byte> encryptedData = encryptedDataLength < 1024
? stackalloc byte[encryptedDataLength]
: new byte[encryptedDataLength].AsSpan();
// Copy parameters
BinaryPrimitives.WriteInt32LittleEndian(encryptedData.Slice(0, 4), nonceSize);
BinaryPrimitives.WriteInt32LittleEndian(encryptedData.Slice(4 + nonceSize, 4), tagSize);
var nonce = encryptedData.Slice(4, nonceSize);
var tag = encryptedData.Slice(4 + nonceSize + 4, tagSize);
var cipherBytes = encryptedData.Slice(4 + nonceSize + 4 + tagSize, cipherSize);
// Generate secure nonce
RandomNumberGenerator.Fill(nonce);
// Encrypt
using var aes = new AesGcm(_key);
aes.Encrypt(nonce, plainBytes.AsSpan(), cipherBytes, tag);
// Encode for transmission
return Convert.ToBase64String(encryptedData);
}
Run Code Online (Sandbox Code Playgroud)
相应地,解密过程如下:
public string Decrypt(string cipher)
{
// Decode
Span<byte> encryptedData = Convert.FromBase64String(cipher).AsSpan();
// Extract parameter sizes
int nonceSize = BinaryPrimitives.ReadInt32LittleEndian(encryptedData.Slice(0, 4));
int tagSize = BinaryPrimitives.ReadInt32LittleEndian(encryptedData.Slice(4 + nonceSize, 4));
int cipherSize = encryptedData.Length - 4 - nonceSize - 4 - tagSize;
// Extract parameters
var nonce = encryptedData.Slice(4, nonceSize);
var tag = encryptedData.Slice(4 + nonceSize + 4, tagSize);
var cipherBytes = encryptedData.Slice(4 + nonceSize + 4 + tagSize, cipherSize);
// Decrypt
Span<byte> plainBytes = cipherSize < 1024
? stackalloc byte[cipherSize]
: new byte[cipherSize];
using var aes = new AesGcm(_key);
aes.Decrypt(nonce, cipherBytes, tag, plainBytes);
// Convert plain bytes back into string
return Encoding.UTF8.GetString(plainBytes);
}
Run Code Online (Sandbox Code Playgroud)
有关完整实现和示例,请参阅dotnetfiddle。
请注意,我是为网络传输而编写的,因此所有内容都被编码为一个大的 base-64 字符串;或者,您可以返回nonce
,tag
并cipherBytes
通过out
参数单独返回。
网络设置也是我发送 nonce 和标签大小的原因:该类可能由具有不同运行时环境的不同应用程序使用,这些应用程序可能具有不同的支持参数大小。