Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用AES解密时出现错误的16字节_C++_Encryption_Cryptography_Byte_Aes - Fatal编程技术网

C++ 使用AES解密时出现错误的16字节

C++ 使用AES解密时出现错误的16字节,c++,encryption,cryptography,byte,aes,C++,Encryption,Cryptography,Byte,Aes,我用AES解密密文。在所有情况下,我的初始16字节都是错误的。我用的是C++。我不知道代码出了什么问题。我得到了16个字节的错误,然后部分解密的消息存在。我已经做了很多解决方案,但没有任何人。请帮助解决可能出现的问题。谢谢 请在下面找到我的代码: bytearray *DecryptResponse::decrypt(bytearray cleartext, bytearray iv, bytearray hmac) { bytearray encryptionKey = m

我用AES解密密文。在所有情况下,我的初始16字节都是错误的。我用的是C++。我不知道代码出了什么问题。我得到了16个字节的错误,然后部分解密的消息存在。我已经做了很多解决方案,但没有任何人。请帮助解决可能出现的问题。谢谢

请在下面找到我的代码:

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;

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

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