我使用AES256 GCM算法使用BouncyCastle加密C#中的数据。为此,我使用implementation provided by James Tuley。以下是此代码的摘录:
public byte[] SimpleEncrypt(byte[] secretMessage, byte[] key, byte[] nonSecretPayload = null)
{
if (key == null || key.Length != KeyBitSize / 8)
throw new ArgumentException($"Key needs to be {KeyBitSize} bit!", nameof(key));
if (secretMessage == null || secretMessage.Length == 0)
throw new ArgumentException("Secret Message Required!", nameof(secretMessage));
nonSecretPayload = nonSecretPayload ?? new byte[] { };
byte[] nonce = _csprng.RandomBytes(NonceBitSize / 8);
var cipher = new GcmBlockCipher(new AesFastEngine());
var parameters = new AeadParameters(new KeyParameter(key), MacBitSize, nonce, nonSecretPayload);
cipher.Init(true, parameters);
var cipherText = new byte[cipher.GetOutputSize(secretMessage.Length)];
int len = cipher.ProcessBytes(secretMessage, 0, secretMessage.Length, cipherText, 0);
cipher.DoFinal(cipherText, len);
using (var combinedStream = new MemoryStream())
{
using (var binaryWriter = new BinaryWriter(combinedStream))
{
binaryWriter.Write(nonSecretPayload);
binaryWriter.Write(nonce);
binaryWriter.Write(cipherText);
}
return combinedStream.ToArray();
}
}
我需要获取身份验证标记(在RFC 5084中提到)。它提到身份验证标记是输出的一部分:
AES-GCM生成两个输出:密文和消息验证代码(也称为验证标记)。
我不明白如何从此代码中获取身份验证标记?任何人都可以帮助我吗?
答案 0 :(得分:1)
调用GetMac()
对象的cipher
函数以获取身份验证标记:
...
cipher.DoFinal(cipherText, len);
var auth_tag = cipher.GetMac();
...
来源: http://www.bouncycastle.org/docs/docs1.5on/org/bouncycastle/crypto/modes/GCMBlockCipher.html "返回与上次处理的流相关联的MAC的值" MAC ="消息验证码"
DoFinal()
函数的文档说明"完成操作要么在数据末尾追加或验证MAC",这似乎证实了先前的假设{{1}也将包含MAC。使用cipherText
,您应该能够确定其距GetMacSize()
末尾的偏移量。