Jeffrey Walton wrote:

Hi All,

Using Crypto++ 5.1, MSVC++ 6.0 SP5, Processor Pack installed.

When I change to CFB mode (from CBC mode), the following demo breaks. How does 
one instruct the StreamTransformationFilter to pad? I must have missed it in 
the documentation.

Thanks,
Jeff


CryptoPP::CBC_Mode_ExternalCipher::... // OK
to
CryptoPP::CFB_Mode_ExternalCipher::... // Breaks

*******************************************
*******************************************
#include <iostream>

#include "modes.h"
#include "aes.h"
#include "filters.h"

int main(int argc, char* argv[]) {

   //
   // Key and IV setup
   //
   byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
   memset( key, 0x01, CryptoPP::AES::DEFAULT_KEYLENGTH );
   memset( iv, 0x01, CryptoPP::AES::BLOCKSIZE );

   //
   // String and Sink setup
   //
   std::string plaintext = "Now is the time for all good men to come to the 
aide...";
   std::string ciphertext;
   std::string decryptedtext;

   //
   // Dump Plain Text
   //
   std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
   std::cout << plaintext;
   std::cout << std::endl << std::endl;

   //
   // Create Cipher Text
   //
   CryptoPP::AES::Encryption aesEncryption(key, 
CryptoPP::AES::DEFAULT_KEYLENGTH);
   CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, 
iv );

   CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new 
CryptoPP::StringSink( ciphertext ) );
   stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() 
), plaintext.length() + 1 );
   stfEncryptor.MessageEnd();

   //
   // Dump Cipher Text
   //
   std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;

   for( int i = 0; i < ciphertext.size(); i++ ) {
       std::cout << "0x" << (0xFF & (unsigned int)ciphertext[i]) << " ";
   }
   std::cout << std::endl << std::endl;

   //
   // Decrypt
   //
   CryptoPP::AES::Decryption aesDecryption(key, 
CryptoPP::AES::DEFAULT_KEYLENGTH);
   CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, 
iv );

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

   //
   // Dump Decrypted Text
   //
   std::cout << "Decrypted Text: " << std::endl;
   std::cout << decryptedtext;
   std::cout << std::endl << std::endl;

   return 0;
}





Hi Jeff,
I had the same problem a year and a half ago :)
The problem is because only for decryption in ECB and CBC mode you use CryptoPP::AES::Decryption onject, in all others modes you should use CryptoPP::AES::Encryption object. Why? dont know, ask Wei Dai :)

So your decryption code should look like:

   //
   // Decrypt
   //

   //for ECB and CBC
   CryptoPP::AES::Decryption aesDecryption(key, 
CryptoPP::AES::DEFAULT_KEYLENGTH);
   //for CFB, OFB and CTR
   CryptoPP::AES::Encryption aesDecryption(key, 
CryptoPP::AES::DEFAULT_KEYLENGTH);

   CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, 
iv );


Reply via email to