C#中的node.js“ aes-256-gcm”

时间:2018-08-03 14:22:44

标签: c# node.js bouncycastle

我在js中有以下代码:

public static decrypt(inData: Buffer, inKey: Buffer, inIv: Buffer, inAuthenticationTag: Buffer = null): Buffer
{
    const decipher: Decipher = createDecipheriv("aes-256-gcm", inKey, inIv);
    decipher.setAuthTag(inAuthenticationTag);
    return Buffer.concat([decipher.update(inData), decipher.final()]);
}

此代码使用密钥以及给定的iv和authTag解密缓冲区。

我尝试使用BouncyCastle在C#中解密相同的数据,并始终收到错误“在gcm中进行mac检查失败”

这是我的C#代码:

    public static string Decrypt(byte[] cipherText, byte[] key, byte[] iv, byte[] authTag)
    {
        var keyParameter = new KeyParameter(key);
        var gcmParameters = new AeadParameters(
            keyParameter,
            128,
            iv);

        var gcmMode = new GcmBlockCipher(new AesFastEngine());
        gcmMode.Init(false, gcmParameters);

        var cipherBuffer = cipherText.Concat(authTag).ToArray();
        var plainBytes = new byte[gcmMode.GetOutputSize(cipherBuffer.Length)];
        var res = gcmMode.ProcessBytes(cipherBuffer, 0, cipherBuffer.Length, plainBytes, 0);
        gcmMode.DoFinal(plainBytes, res); // When executing this line i get the Exception

        var plain = Encoding.UTF8.GetString(plainBytes, 0, plainBytes.Length);
        return plain;
    }

有人可以看到我在C#中犯的错误吗?

0 个答案:

没有答案