My program could encrypt text and save it in a file, and decrypt the cipher text after getting from the file.

But I keep on getting this error

terminate called after throwing an instance of 'CryptoPP::InvalidCiphertext'
what(): StreamTransformationFilter: ciphertext length is not a multiple of block size
exited with code 3
Here my encrypt code, the reason I seperate them to different files is because I'm trying to remove this annoying error.

Qt Code:
  1. byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
  2. memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
  3. memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
  4. string ciphertext[7];
  5.  
  6.  
  7.  
  8. CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
  9. CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
  10.  
  11. CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext[0] ) );
  12. stfEncryptor.Put( reinterpret_cast<const unsigned char*>( line[0].c_str() ), line[0].length() + 1 );
  13. stfEncryptor.MessageEnd();
  14.  
  15. ofstream outfile("./QA.dll");
  16. outfile << ciphertext[0] << endl;
  17.  
  18.  
  19.  
  20. CryptoPP::AES::Encryption aesEncryption1(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
  21. CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption1( aesEncryption1, iv );
  22.  
  23. CryptoPP::StreamTransformationFilter stfEncryptor1(cbcEncryption1, new CryptoPP::StringSink( ciphertext[1] ) );
  24. stfEncryptor1.Put( reinterpret_cast<const unsigned char*>( line[1].c_str() ), line[1].length() + 1 );
  25. stfEncryptor1.MessageEnd();
  26.  
  27. ofstream outfile1("./QF.dll");
  28. outfile1 << ciphertext[1] << endl;
  29.  
  30.  
  31.  
  32. CryptoPP::AES::Encryption aesEncryption2(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
  33. CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption2( aesEncryption2, iv );
  34.  
  35. CryptoPP::StreamTransformationFilter stfEncryptor2(cbcEncryption2, new CryptoPP::StringSink( ciphertext[2] ) );
  36. stfEncryptor2.Put( reinterpret_cast<const unsigned char*>( line[2].c_str() ), line[2].length() + 1 );
  37. stfEncryptor2.MessageEnd();
  38.  
  39. ofstream outfile2("./QG.dll");
  40. outfile2 << ciphertext[2] << endl;
  41. ....
To copy to clipboard, switch view to plain text mode 


Here decrypt's code, I made key1, key2 is also because of trying to remove the error

Qt Code:
  1. ifstream infile("./Q.dll");
  2. if (!infile.is_open())
  3. return 1;
  4. getline(infile, ciphertext[0]);
  5.  
  6. ifstream infile1("./QF.dll");
  7. if (!infile1.is_open())
  8. return 1;
  9. getline(infile1, ciphertext[1]);
  10.  
  11. ifstream infile2("./QG.dll");
  12. if (!infile2.is_open())
  13. return 1;
  14. getline(infile2, ciphertext[2]);
  15. ...
  16.  
  17. // Decrypt other passwords
  18. // **********************
  19. // **********************
  20. // **********************
  21. // **********************
  22. // **********************
  23. // Set key
  24.  
  25.  
  26. byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
  27. memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
  28. memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
  29.  
  30.  
  31.  
  32. // DECRYPT START~~~
  33.  
  34. if (ciphertext[0].size() != 0)
  35. {
  36. CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
  37. CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
  38.  
  39. CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( qdec[0] ) );
  40. stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext[0].c_str() ), ciphertext[0].size() );
  41. stfDecryptor.MessageEnd();
  42. QApplication::processEvents();
  43. }
  44. // **********************
  45.  
  46.  
  47. byte key1[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv1[ CryptoPP::AES::BLOCKSIZE ];
  48. memset( key1, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
  49. memset( iv1, 0x00, CryptoPP::AES::BLOCKSIZE );
  50.  
  51.  
  52. if (ciphertext[1].size() != 0)
  53. {
  54. CryptoPP::AES::Decryption aesDecryption1(key1, CryptoPP::AES::DEFAULT_KEYLENGTH);
  55. CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption1( aesDecryption1, iv1 );
  56.  
  57. CryptoPP::StreamTransformationFilter stfDecryptor1(cbcDecryption1, new CryptoPP::StringSink( qdec[1] ) );
  58. stfDecryptor1.Put( reinterpret_cast<const unsigned char*>( ciphertext[1].c_str() ), ciphertext[1].size() );
  59. stfDecryptor1.MessageEnd();
  60. QApplication::processEvents();
  61. }
  62.  
  63. // **********************
  64.  
  65.  
  66. byte key2[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv2[ CryptoPP::AES::BLOCKSIZE ];
  67. memset( key2, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
  68. memset( iv2, 0x00, CryptoPP::AES::BLOCKSIZE );
  69.  
  70.  
  71. if (ciphertext[2].size() != 0)
  72. {
  73. CryptoPP::AES::Decryption aesDecryption2(key2, CryptoPP::AES::DEFAULT_KEYLENGTH);
  74. CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption2( aesDecryption2, iv2 );
  75.  
  76. CryptoPP::StreamTransformationFilter stfDecryptor2(cbcDecryption2, new CryptoPP::StringSink( qdec[2] ) );
  77. stfDecryptor2.Put( reinterpret_cast<const unsigned char*>( ciphertext[2].c_str() ), ciphertext[2].size() );
  78. stfDecryptor2.MessageEnd();
  79. QApplication::processEvents();
  80. }
  81. ......
  82. // **********************
To copy to clipboard, switch view to plain text mode 

And also, this is AES, but where do I actually set the secret key?