解密AES / OFB / NoPadding

时间:2019-06-19 08:53:32

标签: c# asp.net-core encryption aes bouncycastle

我需要解密使用C#中的"error": { "statusCode": 500, "name": "Error", "message": "Error trying invoke business network with transaction id aca902e4be57b739aec74b832cf55a7bab3911daf66adf55a62552708c31b767. Error: No valid responses from any peers.\nResponse from attempted peer comms was an error: Error: transaction returned with failure: Error: Something broke!", "stack": "Error: Error trying invoke business network with transaction id aca902e4be57b739aec74b832cf55a7bab3911daf66adf55a62552708c31b767. Error: No valid responses from any peers.\nResponse from attempted peer comms was an error: Error: transaction returned with failure: Error: Something broke!\n at HLFConnection.invokeChainCode (/home/rohit/.nvm/versions/node/v8.9.4/lib/node_modules/composer-rest-server/node_modules/composer-connector-hlfv1/lib/hlfconnection.js:1117:30)\n at <anonymous>" } 组合编码的内容。似乎本机不支持此功能:以下代码不会这样做

AES/OFB/NoPadding

产生:

  

System.Security.Cryptography.CryptographicException:
       指定的密码模式对该算法无效。

在SO上我能找到的最接近的问题是this one,它使用BouncyCastle进行加密:

var aes = new AesManaged
{
   Padding = PaddingMode.None,
   Mode = CipherMode.OFB,
   KeySize = 128,
   BlockSize = 128
};

其他问题(例如this one)包含更多信息,但也包含许多自定义代码-我宁愿使用BouncyCastle。有人可以帮助我进行解密,或者为我提供一些有用的文档吗?

1 个答案:

答案 0 :(得分:0)

根据实际问题中的代码,触发解密而非加密所需的唯一步骤是更改cipher.Init调用中的布尔参数:

cipher.Init(False, aesIVKeyParam) // False == decryption, True == encryption

C#的最后一个片段是

private static byte[] DoDecryption(byte[] keyArray, byte[] ivArray, byte[] encoded)
{
    var aesKeyParam = ParameterUtilities.CreateKeyParameter("AES", keyArray);
    var aesIvKeyParam = new ParametersWithIV(aesKeyParam, ivArray);
    var cipher = CipherUtilities.GetCipher("AES/OFB/NOPADDING");
    cipher.Init(false, aesIvKeyParam);
    var decrypted = cipher.DoFinal(encoded);
    return decrypted;
}