Skip to content

Can I assume that AesGcm.Encrypt/Decrypt is safe for in-place operation? #95552

Answered by bartonjs
GF-Huang asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, it's a thing we explicitly test for.

public static void InplaceEncryptDecrypt()
{
byte[] key = "d5a194ed90cfe08abecd4691997ceb2c".HexToByteArray();
byte[] nonce = new byte[12];
byte[] originalPlaintext = new byte[] { 1, 2, 8, 12, 16, 99, 0 };
byte[] data = (byte[])originalPlaintext.Clone();
byte[] tag = new byte[16];
RandomNumberGenerator.Fill(nonce);
using (var aesGcm = new AesGcm(key, tag.Length))
{
aesGcm.Encrypt(nonce, data, data, tag);
Assert.NotEqual(originalPlaintext, data);
aesGcm.Decrypt(nonce, data

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@GF-Huang
Comment options

@danmoseley
Comment options

@GF-Huang
Comment options

Answer selected by GF-Huang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
3 participants