使用AES解密时错误的16个字节

时间:2014-11-14 10:35:03

标签: c++ encryption cryptography byte aes

我使用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"}

1 个答案:

答案 0 :(得分:0)

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