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 = stackalloc byte[0]; // Workaround for https://github.com/dotnet/roslyn/issues/25118
if(encryptedDataLength <= 1024)
encryptedData = stackalloc byte[encryptedDataLength];
else
encryptedData = new byte[encryptedDataLength].AsSpan();
// Copy parameters
MemoryMarshal.Write(encryptedData.Slice(0, 4), ref nonceSize);
MemoryMarshal.Write(encryptedData.Slice(4 + nonceSize, 4), ref 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
new AesGcm(_key).Encrypt(nonce, plainBytes.AsSpan(), cipherBytes, tag);
// Encode for transmission
return Convert.ToBase64String(encryptedData);
}
public string Decrypt(string cipher)
{
// Decode
Span<byte> encryptedData = Convert.FromBase64String(cipher).AsSpan();
// Extract parameter sizes
int nonceSize = MemoryMarshal.Read<int>(encryptedData.Slice(0, 4));
int tagSize = MemoryMarshal.Read<int>(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 = stackalloc byte[0];
if(cipherSize <= 1024)
plainBytes = stackalloc byte[cipherSize];
else
plainBytes = new byte[cipherSize];
new AesGcm(_key).Decrypt(nonce, cipherBytes, tag, plainBytes);
// Convert plain bytes back into string
return Encoding.UTF8.GetString(plainBytes);
}
See dotnetfiddle for the full implementation and an example.