AES解密中找到的无效的PKCS#7块填充

AES解密中找到的无效的PKCS#7块填充

本文介绍了StreamTransformationFilter:AES解密中找到的无效的PKCS#7块填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的C ++代码中使用AES-256加密/解密,我使用cryptoPP库,我实施加密和解密。但我得到错误:StreamTransformationFilter:无效PKCS#7块填充发现错误。



我测试的senario是:



首先加密myplaintext然后解密 - >没有错误在这里。



第二我解密上面创建的密码 - >这里我得到的错误。



知道这里有同样的问题,但我不能解决我的问题:(



这里我设置iv:

 字节IV [16]; 
串strIv =162169848599E7C792BF58BFA53D88E6;
的memcpy(四,strIv.data(),strIv.length());

加密:

 字符串CryptoAES ::加密(字符串平原,串strkey){
COUT<<\\\
plain文字:<<平原;
COUT<<\\ \
密钥来加密:&所述;&下; strkey;
字节密钥[AES :: MAX_KEYLENGTH];
的memcpy(标号,strkey.data(),strkey.length());
串encodedKey,decodedKey,ciphertextEncode,密文;
encodedKey.clear();
decodedKey.clear();
ciphertextEncode.clear();
ciphertext.clear();
StringSource(key,sizeof(key),true,new HexEncoder(new StringSink(encodedKey)));
StringSource ssk(encodedKey,true,new HexDecoder(new StringSink(decodedKey)));
memcpy(key,decodedKey.data(),decodedKey.length());
CryptoPP :: AES :: Encryption aesEncryption(key,AES :: MAX_KEYLENGTH);
CryptoPP :: CBC_Mode_ExternalCipher :: Encryption cbcEncryption(aesEncryption,iv);

CryptoPP :: StreamTransformationFilter stfEncryptor(cbcEncryption,new CryptoPP :: StringSink(ciphertext));
stfEncryptor.Put(reinterpret_cast< const unsigned char *>(plain.c_str()),plain.length()+ 1);
stfEncryptor.MessageEnd();
StringSource ss(ciphertext,true,new HexEncoder(new StringSink(ciphertextEncode)));
cout<< \\\
encrypted text:<< ciphertextEncode;
return ciphertextEncode;
}

解密:

  string CryptoAES :: Decrypt(string cipher,string strkey){
cout<<\\\
cipher text:<密码
cout<< \\\
key to decrypt:<<< strkey;
byte key [AES :: MAX_KEYLENGTH];
memcpy(key,strkey.data(),strkey.length());
string encodedKey,decodedKey,ciphertextDecode,decryptptedtext;
encodedKey.clear();
decodedKey.clear();
ciphertextDecode.clear();
decryptptedtext.clear();
StringSource(key,sizeof(key),true,new HexEncoder(new StringSink(encodedKey)));
StringSource ssk(encodedKey,true,new HexDecoder(new StringSink(decodedKey)));
memcpy(key,decodedKey.data(),decodedKey.length());
StringSource ss(cipher,true,new HexDecoder(new StringSink(ciphertextDecode)));
CryptoPP :: AES :: Decryption aesDecryption(key,AES :: MAX_KEYLENGTH);
CryptoPP :: CBC_Mode_ExternalCipher :: Decryption cbcDecryption(aesDecryption,iv);

CryptoPP :: StreamTransformationFilter stfDecryptor(cbcDecryption,新CryptoPP :: StringSink(decryptedtext));
stfDecryptor.Put(reinterpret_cast的< const的无符号字符*>(ciphertextDecode.c_str()),ciphertextDecode.size());
stfDecryptor.MessageEnd();

cout<<\\\
decrypted text:<<
return decryptedtext;
}

结果:

 纯文本:F7ACA191B43AFAF85277DD196FE0441CB7C0901BEC33CB9F38FFAA54CBF219B9 
键加密:123456
加密后的文本:6FC1BF3108B0590367E6449B6E615CACFDF4DE16EDD05742C873EE4E8A16BA9EC0B8EFAD800F466EE9A6F75202C8800CF4CBDD2620956020D5B0A6A9A8DCEA9EDB5C470527423ACBEEDD0A9C59916C8B
密文:6FC1BF3108B0590367E6449B6E615CACFDF4DE16EDD05742C873EE4E8A16BA9EC0B8EFAD800F466EE9A6F75202C8800CF4CBDD2620956020D5B0A6A9A8DCEA9EDB5C470527423ACBEEDD0A9C59916C8B
密钥来解密:123456
解密文本:F7ACA191B43AFAF85277DD196FE0441CB7C0901BEC33CB9F38FFAA54CBF219B9
密文:6FC1BF3108B0590367E6449B6E615CACFDF4DE16EDD05742C873EE4E8A16BA9EC0B8EFAD800F466EE9A6F75202C8800CF4CBDD2620956020D5B0A6A9A8DCEA9EDB5C470527423ACBEEDD0A9C59916C8B
密钥来解密:123456
例外代理服务器! (
Error:StreamTransformationFilter:invalid PKCS#7 block padding

编辑#
这里是新版本的方法:



IV:

  for(int i = 0; i  iv [i] = 0; 

加密:

  string CryptoAES :: Encrypt(string plain,string strkey){ 

byte key [AES :: MAX_KEYLENGTH];
byte * k =(byte *)strkey.c_str();

for(int i = 0; i if(i key [i] = k [i];
else
key [i] = 0;
串ciphertextEncode,密文;
COUT<<\\\
plain文字:<<平原;
COUT<<\\\
密钥来加密:<<钥匙;
ciphertextEncode.clear();
ciphertext.clear();
CryptoPP :: AES加密:: aesEncryption(键,AES :: MAX_KEYLENGTH);
CryptoPP :: CBC_Mode_ExternalCipher :: Encryption cbcEncryption(aesEncryption,iv);

CryptoPP :: StreamTransformationFilter stfEncryptor(cbcEncryption,new CryptoPP :: StringSink(ciphertext));
stfEncryptor.Put(reinterpret_cast< const unsigned char *>(plain.c_str()),plain.length()+ 1);
stfEncryptor.MessageEnd();
cout<< \\\
encrypted text:<< ciphertext;
StringSource ss(ciphertext,true,new HexEncoder(new StringSink(ciphertextEncode)));
cout<< \\\
encoded encrypted text:<< ciphertextEncode;
return ciphertextEncode;
}

解密:

  string CryptoAES :: Decrypt(string cipher,string strkey){

byte key [AES :: MAX_KEYLENGTH];
byte * k =(byte *)strkey.c_str();

的for(int i = 0; I< AES :: MAX_KEYLENGTH;我++)
如果(I<的sizeof(K))
键[i] = K [我];
else
key [i] = 0;
string ciphertextDecode,decryptptedtext;
cout<<\\\
cipher text:<<密码
cout<< \\\
key to decrypt:<< key;
ciphertextDecode.clear();
decryptptedtext.clear();

StringSource ss(cipher,true,new HexDecoder(new StringSink(ciphertextDecode)));
cout<< \\\
cipher decoded:<密文解码;
CryptoPP :: AES :: Decryption aesDecryption(key,AES :: MAX_KEYLENGTH);
CryptoPP :: CBC_Mode_ExternalCipher :: Decryption cbcDecryption(aesDecryption,iv);

CryptoPP :: StreamTransformationFilter stfDecryptor(cbcDecryption,新CryptoPP :: StringSink(decryptedtext));
stfDecryptor.Put(reinterpret_cast的< const的无符号字符*>(ciphertextDecode.c_str()),ciphertextDecode.size());
stfDecryptor.MessageEnd();

cout<<\\\
decrypted text:<<
return decryptedtext;
}

结果:

 纯文本:0C469BEA09DFCAC0A555E74175F1A614F471A5205FEB13A72C2DFFE8C4B52AA4 
键加密:123456
加密后的文本:z_d0kߨ ^G o zL Z fc L X ,< X *-R_ x ? U$
编码的加密文字:D8F68E8E8B7AADB094A5BD5FC117BFBA648130E46BDFA8B6DD0EA35E17478F6FA01AA38E0417B4087A4CAEBB5AB8C466639C4C84F35895C07FB2172C3C582A2D525FFDDC78A9F83FEFA50913E55524C1
密文:D8F68E8E8B7AADB094A5BD5FC117BFBA648130E46BDFA8B6DD0EA35E17478F6FA01AA38E0417B4087A4CAEBB5AB8C466639C4C84F35895C07FB2172C3C582A2D525FFDDC78A9F83FEFA50913E55524C1
密钥来解密:123456
密码解码:z_d0kߨ^ GozLZfcLX,< X * -R_x U $
解密文本:0C469BEA09DFCAC0A555E74175F1A614F471A5205FEB13A72C2DFFE8C4B52AA4
密文:D8F68E8E8B7AADB094A5BD5FC117BFBA648130E46BDFA8B6DD0EA35E17478F6FA01AA38E0417B4087A4CAEBB5AB8C466639C4C84F35895C07FB2172C3C582A2D525FFDDC78A9F83FEFA50913E55524C1
密钥来解密:123456
密码解码:z_d0kߨ^围棋 zL Z fc L X ,< X *-R_ x ? U$
代理服务器中的异常! :(
StreamTransformationFilter:无效的PKCS#7块填充发现


解决方案



code>字符串密码和字符串strkey \0 填充。





iv [16] / code>这两个方法之间的共享值,改变了!所以我在每个方法中设置它。


I want to use AES-256 encryption/decryption in my C++ code to do that I use cryptoPP library, I have implement encryption and decryption. but I get Error : StreamTransformationFilter: invalid PKCS #7 block padding found Error.

The senario which I test is :

First I encrypt myplaintext then decrypt it --> there is no error here.

Second I decrypt the cipher created above --> here I got the error.

I know that there are same question here but I couldn't solve my problem :(

Here I set iv:

byte iv[16];
string strIv = "162169848599E7C792BF58BFA53D88E6";
memcpy(iv, strIv.data(), strIv.length());

Encryption:

string CryptoAES::Encrypt(string plain, string strkey) {
    cout << "\nplain text :" << plain;
    cout << "\n key to encrypt: " <<strkey;
    byte key[AES::MAX_KEYLENGTH];
    memcpy(key, strkey.data(), strkey.length());
    string encodedKey, decodedKey,ciphertextEncode,ciphertext;
    encodedKey.clear();
    decodedKey.clear();
    ciphertextEncode.clear();
    ciphertext.clear();
    StringSource(key, sizeof (key), true, new HexEncoder(new StringSink(encodedKey)));
    StringSource ssk(encodedKey, true, new HexDecoder(new StringSink(decodedKey)));
    memcpy(key, decodedKey.data(), decodedKey.length());
    CryptoPP::AES::Encryption aesEncryption(key, AES::MAX_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);

    CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(ciphertext));
    stfEncryptor.Put(reinterpret_cast<const unsigned char*> (plain.c_str()), plain.length() + 1);
    stfEncryptor.MessageEnd();
    StringSource ss(ciphertext, true, new HexEncoder(new StringSink(ciphertextEncode)));
    cout << "\nencrypted text: " <<ciphertextEncode;
    return ciphertextEncode;
}

Decryption:

string CryptoAES::Decrypt(string cipher, string strkey) {
    cout <<"\ncipher text : "<< cipher;
    cout << "\n key to decrypt: " <<strkey;
    byte key[AES::MAX_KEYLENGTH];
    memcpy(key, strkey.data(), strkey.length());
    string encodedKey, decodedKey,ciphertextDecode,decryptedtext;
    encodedKey.clear();
    decodedKey.clear();
    ciphertextDecode.clear();
    decryptedtext.clear();
    StringSource(key, sizeof (key), true, new HexEncoder(new StringSink(encodedKey)));
    StringSource ssk(encodedKey, true, new HexDecoder(new StringSink(decodedKey)));
    memcpy(key, decodedKey.data(), decodedKey.length());
    StringSource ss(cipher, true, new HexDecoder(new StringSink(ciphertextDecode)));
    CryptoPP::AES::Decryption aesDecryption(key, AES::MAX_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);

    CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext));
    stfDecryptor.Put(reinterpret_cast<const unsigned char*> (ciphertextDecode.c_str()), ciphertextDecode.size());
    stfDecryptor.MessageEnd();

    cout <<"\ndecrypted text: "<<decryptedtext;
    return decryptedtext;
}

result :

plain text :F7ACA191B43AFAF85277DD196FE0441CB7C0901BEC33CB9F38FFAA54CBF219B9
 key to encrypt: 123456
encrypted text: 6FC1BF3108B0590367E6449B6E615CACFDF4DE16EDD05742C873EE4E8A16BA9EC0B8EFAD800F466EE9A6F75202C8800CF4CBDD2620956020D5B0A6A9A8DCEA9EDB5C470527423ACBEEDD0A9C59916C8B
cipher text : 6FC1BF3108B0590367E6449B6E615CACFDF4DE16EDD05742C873EE4E8A16BA9EC0B8EFAD800F466EE9A6F75202C8800CF4CBDD2620956020D5B0A6A9A8DCEA9EDB5C470527423ACBEEDD0A9C59916C8B
 key to decrypt: 123456
decrypted text: F7ACA191B43AFAF85277DD196FE0441CB7C0901BEC33CB9F38FFAA54CBF219B9
cipher text : 6FC1BF3108B0590367E6449B6E615CACFDF4DE16EDD05742C873EE4E8A16BA9EC0B8EFAD800F466EE9A6F75202C8800CF4CBDD2620956020D5B0A6A9A8DCEA9EDB5C470527423ACBEEDD0A9C59916C8B
 key to decrypt: 123456
exception in proxy server! :(
    Error : StreamTransformationFilter: invalid PKCS #7 block padding found

EDIT #1:here is the new version of the methods:

IV:

for (int i = 0; i < AES::BLOCKSIZE; i++)
        iv[i] = 0;

encryption:

string CryptoAES::Encrypt(string plain, string strkey) {

    byte key[AES::MAX_KEYLENGTH];
    byte* k = (byte*) strkey.c_str();

    for (int i = 0; i < AES::MAX_KEYLENGTH; i++)
        if (i<sizeof (k))
            key[i] = k[i];
        else
            key[i] = 0;
    string ciphertextEncode,ciphertext;
    cout << "\nplain text :" << plain;
    cout << "\n key to encrypt: " <<key;
    ciphertextEncode.clear();
    ciphertext.clear();
    CryptoPP::AES::Encryption aesEncryption(key, AES::MAX_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);

    CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(ciphertext));
    stfEncryptor.Put(reinterpret_cast<const unsigned char*> (plain.c_str()), plain.length() + 1);
    stfEncryptor.MessageEnd();
    cout << "\nencrypted text: " <<ciphertext;
    StringSource ss(ciphertext, true, new HexEncoder(new StringSink(ciphertextEncode)));
    cout << "\nencoded encrypted text: " <<ciphertextEncode;
    return ciphertextEncode;
}

Decryption:

string CryptoAES::Decrypt(string cipher, string strkey) {

    byte key[AES::MAX_KEYLENGTH];
    byte* k = (byte*) strkey.c_str();

    for (int i = 0; i < AES::MAX_KEYLENGTH; i++)
        if (i<sizeof (k))
            key[i] = k[i];
        else
            key[i] = 0;
    string ciphertextDecode,decryptedtext;
    cout <<"\ncipher text : "<< cipher;
    cout << "\n key to decrypt: " <<key;
    ciphertextDecode.clear();
    decryptedtext.clear();

    StringSource ss(cipher, true, new HexDecoder(new StringSink(ciphertextDecode)));
    cout << "\n cipher decoded: " << ciphertextDecode;
    CryptoPP::AES::Decryption aesDecryption(key, AES::MAX_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);

    CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext));
    stfDecryptor.Put(reinterpret_cast<const unsigned char*> (ciphertextDecode.c_str()), ciphertextDecode.size());
    stfDecryptor.MessageEnd();

    cout <<"\ndecrypted text: "<<decryptedtext;
    return decryptedtext;
}

result:

plain text :0C469BEA09DFCAC0A555E74175F1A614F471A5205FEB13A72C2DFFE8C4B52AA4
 key to encrypt: 123456
encrypted text: �����z�����_���d�0�kߨ���^G�o���zL��Z��fc�L��X���,<X*-R_��x��?�  �U$�
encoded encrypted text: D8F68E8E8B7AADB094A5BD5FC117BFBA648130E46BDFA8B6DD0EA35E17478F6FA01AA38E0417B4087A4CAEBB5AB8C466639C4C84F35895C07FB2172C3C582A2D525FFDDC78A9F83FEFA50913E55524C1
cipher text : D8F68E8E8B7AADB094A5BD5FC117BFBA648130E46BDFA8B6DD0EA35E17478F6FA01AA38E0417B4087A4CAEBB5AB8C466639C4C84F35895C07FB2172C3C582A2D525FFDDC78A9F83FEFA50913E55524C1
 key to decrypt: 123456
 cipher decoded: �����z�����_���d�0�kߨ���^G�o���zL��Z��fc�L��X���,<X*-R_��x��?� �U$�
decrypted text: 0C469BEA09DFCAC0A555E74175F1A614F471A5205FEB13A72C2DFFE8C4B52AA4
cipher text : D8F68E8E8B7AADB094A5BD5FC117BFBA648130E46BDFA8B6DD0EA35E17478F6FA01AA38E0417B4087A4CAEBB5AB8C466639C4C84F35895C07FB2172C3C582A2D525FFDDC78A9F83FEFA50913E55524C1
 key to decrypt: 123456
 cipher decoded: �����z�����_���d�0�kߨ���^G�o���zL��Z��fc�L��X���,<X*-R_��x��?� �U$�
exception in proxy server! :(
    StreamTransformationFilter: invalid PKCS #7 block padding found
解决方案

Finally I solved the problems.

first:

string cipher and string strkey have \0 padding.

second:

The iv[16] value which was share between two methods, is changed! so I set it in each methods.

这篇关于StreamTransformationFilter:AES解密中找到的无效的PKCS#7块填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 20:17