Remplissage de bloc PKCS # 7 non valide lors de l’utilisation de Crypto ++

Nous utilisons la bibliothèque cryptopp. Nous utilisons le codage ci-dessous. Le chiffrement fonctionne sans problème et nous pouvons obtenir le texte chiffré. Mais obtenir une erreur lors du déchiffrement en tant que “Bloquer le remplissage trouvé”. Quel pourrait être le problème …?

#include  #include  using namespace std; #include "cryptlib.h" #include "filters.h" #include "files.h" #include "modes.h" #include "hex.h" #include "aes.h" #include "osrng.h" using namespace CryptoPP; using CryptoPP::AutoSeededRandomPool; class cspl_crypto{ public: cspl_crypto(); byte* generate_block(int size); char* encrypt_rijndael(byte[], byte[], int, char*, int); char* decrypt_rijndael(byte[], byte[], int, char*, int); ssortingng readFile(); void writeFile(ssortingng); }; cspl_crypto::cspl_crypto() { } int main(int argc, char* argv[]) { vector plain; cspl_crypto ccrypto; AutoSeededRandomPool prng; byte key[AES::DEFAULT_KEYLENGTH]; prng.GenerateBlock(key, sizeof(key)); byte iv[AES::BLOCKSIZE]; prng.GenerateBlock(iv, sizeof(iv)); 

Conversion de chaîne en char *

  ssortingng str("testing"); //ccrypto.readFile() char plainArray[str.size()]; strcpy(plainArray, str.c_str()); char* cipher = ccrypto.encrypt_rijndael(key, iv, sizeof(key), plainArray, sizeof(plainArray)); //char cipherCharArray[cipherText.size()]; // strcpy(cipherCharArray, cipherText.c_str()); char* recover = ccrypto.decrypt_rijndael(key, iv, sizeof(key), cipher, sizeof(cipher)); // cout << "Recovered text: " << recoverText << endl; return 0; } 

Bloc de cryptage:

 char* cspl_crypto::encrypt_rijndael(byte key[], byte iv[], int keysize, char plainText[], int plainTextSize){ vector cipher; std::vector plain(plainText, plainText + plainTextSize); CBC_Mode::Encryption enc; enc.SetKeyWithIV(key, keysize, iv, keysize); // Make room for padding cipher.resize(plain.size()+AES::BLOCKSIZE); ArraySink cs(&cipher[0], cipher.size()); ArraySource(plain.data(), plain.size(), true, new StreamTransformationFilter(enc, new Redirector(cs))); // Set cipher text length now that its known cipher.resize(cs.TotalPutLength()); char returnValue[cipher.size()]; copy(cipher.begin(), cipher.end(), returnValue); return returnValue; } 

Bloc de décyption:

 char* cspl_crypto::decrypt_rijndael(byte key[], byte iv[], int keysize, char cipher[], int size ){ std::vector v(cipher, cipher + size); vector recover; CBC_Mode::Decryption dec; dec.SetKeyWithIV(key, keysize, iv, keysize); // Recovered text will be less than cipher text recover.resize(v.size()); ArraySink rs(&recover[0], recover.size()); ArraySource(v.data(), v.size(), true, new StreamTransformationFilter(dec, new Redirector(rs))); // Set recovered text length now that its known recover.resize(rs.TotalPutLength()); char returnValue[recover.size()]; copy(recover.begin(), recover.end(), returnValue); return returnValue; } 

Bibliothèque:

 ssortingng cspl_crypto::readFile(){ ssortingng line; ssortingng returnValue = ""; ifstream myfile ("N07.txt"); if (myfile.is_open()) { while ( getline (myfile,line) ) { returnValue += line + '\n'; } myfile.close(); } else returnValue = "Unable to open file"; return returnValue; }