【问题标题】:Wrong 16 bytes in decryption using AES使用 AES 解密时错误的 16 个字节
【发布时间】:2015-01-11 17:27:49
【问题描述】:

我正在使用 AES 来解密密文。在所有情况下,我的初始 16 个字节都是错误的。我正在使用 C++。我不知道代码有什么问题。我错了 16 个字节,然后部分解密的消息就在那里。我已经在 SO 上完成了许多解决方案,但没有为任何人工作。请帮助解决问题。谢谢。

请在下面找到我的代码:

bytearray *DecryptResponse::decrypt(bytearray cleartext, bytearray iv,
        bytearray hmac) {


bytearray encryptionKey = mykeyBundle[0];

String TRANSFORMATION = "AES/CBC/PKCS5Padding";
String KEY_ALGORITHM_SPEC = "AES";
bytearray* outputMessage;
try {
    Cipher* cipher = Cipher::getInstance("AES/CBC/PKCS5Padding");
    SecretKeySpec secKey = *(new SecretKeySpec(encryptionKey,
            KEY_ALGORITHM_SPEC));
    IvParameterSpec _iv = *(new IvParameterSpec(iv));
    cipher->init(Cipher::DECRYPT_MODE, secKey, _iv);
    outputMessage = cipher->doFinal(cleartext);
} catch (Exception &e) {
    cout << "Exception getCause" << e.getCause() << endl;
    cout << "Exception getMessage" << e.getMessage() << endl;
    cout << "Check for the Encryption Key. It might be wrong input!"
            << endl;
}

cout<<"sizes: "<< cleartext.size()<<" "<<((*(outputMessage)).size());
return outputMessage;

现在,输出是这样的:

\��6����_)�stdllections":{},"default":["fT1ICn+rqet0S8/AAXDiuto7FTRqIi3SzA=","oQq7EokSPqAoGHgj974G0tFsw6c="],"collection":"crypto"}

【问题讨论】:

  • 如何生成 IV ?您需要使用与加密文本的人相同的 IV。
  • @kulatamicuda 当我从服务器收到消息时,我有密文、IV、hmac。然后,我使用 AES 使用库 API 对其进行解密。我没有生成 IV。它来自服务器端。而且,我只使用该 IV 来解密我的数据。
  • @hellodear 而且那个IV在密文的开头也没有重复?
  • @owlstead 不,我不知道。你可以告诉如果如何检查。我会看到的。告诉我这两种情况会发生什么:什么时候重复,什么时候不重复。
  • 请尝试添加一个(示例)密钥和十六进制的 IV,以及十六进制的前 16 个字节的明文。

标签: c++ encryption cryptography byte aes


【解决方案1】:

实际上,密文的前16个字节是:实际明文和IV密钥的异或。我只需要在解密后用 IV 异或。这就是解决方案。感谢@owlstead 您的宝贵意见。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 2016-02-07
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    相关资源
    最近更新 更多