Results 1 to 2 of 2

Thread: Cryptopp Help

  1. #1
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Cryptopp Help

    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?

  2. #2
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Cryptopp Help

    I found out that this error occurs is related to the plain text inserted. And now I guess is because of some unicode while reading. I ran gdb and check the cipher size and found out that 1 of them is 14 byte instead of 16 and when I print it out it shows only 3/4 of the cipher. The input file is 16 byte. So it seems I need to try to read unicode from file. But how to read?

Similar Threads

  1. HELP! Need to run cryptopp from Qt Creator
    By irin.gen in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2013, 23:06
  2. Problem with qt5 and cryptopp
    By januszmk in forum Newbie
    Replies: 0
    Last Post: 25th January 2013, 10:12
  3. HELP! cryptopp.dso file is missing
    By taufikardi in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 30th July 2012, 15:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.