gpt4 book ai didi

c# - AES GCM 手动 byte[] IV 和标记与加密信息的串联

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:04 28 4
gpt4 key购买 nike

我在 GCM 模式下使用 AES 256 加密,使用来自此站点的名为 AuthenticatedAesCng 的类:CLR security

通过加密流写入明文后,我手动连接 IV、TAG 和加密数据,然后返回该值。

cs 是密码流,ms 是内存流

// Write through and retrieve encrypted data.
cs.Write(message, 0, message.Length);
cs.FlushFinalBlock();
byte[] cipherText = ms.ToArray();

// Retrieve tag and create array to hold encrypted data.
byte[] authenticationTag = encryptor.GetTag();
byte[] encrypted = new byte[cipherText.Length + aes.IV.Length + authenticationTag.Length];

// Set needed data in byte array.
aes.IV.CopyTo(encrypted, 0);
authenticationTag.CopyTo(encrypted, IV_LENGTH);
cipherText.CopyTo(encrypted, IV_LENGTH + TAG_LENGTH);

// Store encrypted value in base 64.
return Convert.ToBase64String(encrypted);

这是在 GCM 模式下使用 AES 密码的正确方式吗?我应该手动将所有这些值放在一起,还是自动完成而我只是错过了?

最佳答案

密文只是数据,但没有标签就不能拥有 GCM 密文:它会破坏 GCM 的全部目的。标签通常附加到密文。

AAD 数据是可选的,它的全部目的是让它以明文形式发送。

IV 实际上是一个随机数,所以它可以在两边计算。如果您使用随机 NONCE 或无法预先计算它,那么通常将它作为密文的前缀(但您必须在两侧明确编码)。

关于c# - AES GCM 手动 byte[] IV 和标记与加密信息的串联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11533105/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com