2016-01-13 103 views
0

我想在我的C++代码中使用AES-256加密/解密来做我使用cryptoPP库,我实现了加密和解密。但我得到了Error : StreamTransformationFilter: invalid PKCS #7 block padding found错误。StreamTransformationFilter:在AES解密中找到无效的PKCS#7块填充

我测试的塞纳里奥是:

首先,我再加密解密myplaintext它 - >没有错误在这里。

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

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

这里我设置四:

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

加密:

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; 
} 

解密:

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; 
} 

结果:

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 

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

IV:

for (int i = 0; i < AES::BLOCKSIZE; 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 < 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; 
} 

解密:

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; 
} 

结果:

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 
+0

@jww,你能帮助我吗? – user3806649

+0

1.您正在使用32字节的iv,大概您期望'memcpy'转换十六进制字符串,但不是。 2.对于256位密钥,您使用的是6字节密钥,请勿使用正确的长度密钥。 3.修复结果中的最后一行,将密钥与错误消息分开,这是令人困惑的。 – zaph

+0

在加密和后加密之前,你是十六进制编码,这是不必要的,不应该完成。使用64字节进行加密并使用PKCS#7填充时,加密输出应该是80字节,而不是160字节的两倍。您需要花时间学习CryptoAES。 – zaph

回答

1

最后,我要解决的问题。

第一:

string cipherstring strkey具有\0填充。

第二:

iv[16]值是两种方法之间的份额被改变!所以我将其设置在每种方法中。