On Sat, 2003-11-15 at 13:46, Stephen torri wrote:
> Here is the code that I am using to encrypt and decrypt a text. What I
> do not see is how the decryption is failing to work. Below is the output
> (char and hex for both the plain text). Question is why is the decrypt
> code rubbish and not the original plain text?

Error message: In previous email

Stack trace: Attached file (encryption_test.strace)

Test program: Attached file (encryption_test.cpp)
  requies: boost (1.30.2), crypto++ (5.1)

  OS: Linux base.torri.org 2.4.20-gentoo-r5 #3 SMP Thu Aug 14 00:24:32
CDT 2003 i686 Pentium III (Katmai) GenuineIntel GNU/Linux

  GCC: Reading specs from 
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/specs
Configured with: /var/tmp/portage/gcc-3.2.3-r2/work/gcc-3.2.3/configure
--prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.2
--includedir=/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include
--datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.2
--mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.2/man
--infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.2/info --enable-shared
--host=i686-pc-linux-gnu --target=i686-pc-linux-gnu --with-system-zlib
--enable-languages=c,c++,f77,objc,java --enable-threads=posix
--enable-long-long --disable-checking --enable-cstdio=stdio
--enable-clocale=generic --enable-__cxa_atexit
--enable-version-specific-runtime-libs
--with-gxx-include-dir=/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include/g++-v3 
--with-local-prefix=/usr/local --enable-shared --enable-nls --without-included-gettext
Thread model: posix
gcc version 3.2.3 20030422 (Gentoo Linux 1.4 3.2.3-r2, propolice)

Build:

g++ -I <crypto++ directory> -L <crypto++ directory> -l <crypto++ lib> -o
encryption_test encryption_test.cpp

Stephen


#include <modes.h>
#include <aes.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <boost/format.hpp>
#include <base64.h>
#include <filters.h>

using namespace std;
using namespace CryptoPP;
using boost::format;
using boost::io::group;
using boost::io::str;

/*---------------------
 * CipherText class
 *---------------------*/
class CipherText {
 public:
  static void hex_output (std::string prefix, std::string input);
  static void char_output (std::string prefix, std::string input);
};

void
CipherText::hex_output (string prefix, string input) {
  cout << prefix;
  for (unsigned int i = 0; i < input.size(); ++i) {
    cout << format(" %1% ") % group(hex, showbase, (int)input[i]);
  }
  cout << endl;
}

void
CipherText::char_output (string prefix, string input) {
  cout << prefix;
  for (unsigned int i = 0; i < input.size(); ++i) {
    cout << format(" %1% ") % group(hex, showbase, input[i]);
  }
  cout << endl;
}

/*---------------------
 * AES_CFB_CipherEngien class
 *---------------------*/
class AES_CFB_CipherEngine {
 public:
  AES_CFB_CipherEngine (std::string cipher_name, unsigned int cipher_type);
  virtual ~AES_CFB_CipherEngine ();
  virtual std::string encrypt (std::string text);
  virtual std::string decrypt (std::string text);
  virtual std::string algorithm_name (void) {
    return m_algorithm_name;
  }
  virtual unsigned int algorithm_type (void) {
    return m_algorithm_type;
  }

 private:
  static const byte m_encryKey[32];
  static const byte m_iv[8];
  std::string m_algorithm_name;
  unsigned int m_algorithm_type;
};


const byte AES_CFB_CipherEngine::m_encryKey[32] = {
    0x3F, 0x6F, 0x6B, 0x69, 0x20, 0x5E, 0x5F, 0x34, 0x3F,
    0x6F, 0x6B, 0x69, 0x20, 0x5E, 0x5F, 0x34, 0x3F, 0x6F,
    0x6B, 0x69, 0x20, 0x5E, 0x5F, 0x34, 0x3F, 0x6F, 0x6B,
    0x69, 0x20, 0x5E, 0x5F, 0x34};

const byte AES_CFB_CipherEngine::m_iv[8] = {0x48, 0x34, 0x95, 0xA4, 0x49, 0xFF, 0x0F, 0x19};

AES_CFB_CipherEngine::AES_CFB_CipherEngine (string cipher_name, unsigned int cipher_type) 
  : m_algorithm_name (cipher_name),
    m_algorithm_type (cipher_type)
{}

AES_CFB_CipherEngine::~AES_CFB_CipherEngine (){}

std::string AES_CFB_CipherEngine::encrypt (std::string plaintext) {

  std::string ciphertext;
 
  try {
    
    CipherText::char_output ("Plaintext char: ", plaintext);
    CipherText::hex_output ("Plaintext hex: ", plaintext);
    
    CFB_Mode<AES>::Encryption aes_encrypt (m_encryKey, AES::DEFAULT_KEYLENGTH, m_iv);
    StreamTransformationFilter *cfbEncryptor =
      new StreamTransformationFilter (aes_encrypt, new Base64Encoder (new StringSink (ciphertext)));
    StringSource source (plaintext, true, cfbEncryptor);

    CipherText::char_output ("Ciphertext char: " , ciphertext);
    CipherText::hex_output ("Ciphertext hex: ", ciphertext);
  }
    
  catch ( InvalidDataFormat err ) {
    cout << "AES_CFB_CipherEngine FAILURE" << endl;
    cout << "Cipher text length: " << ciphertext.size() << endl;
    cout << "Plain text length:  " << plaintext.size() << endl;
    cout << "InvalidDataFormat: " << err.GetWhat() << endl;
    exit(0);
  }

  return ciphertext; 

}
    
std::string AES_CFB_CipherEngine::decrypt (std::string ciphertext) {

  std::string plaintext;

  try {

    CipherText::char_output ("Ciphertext char: " , ciphertext);
    CipherText::hex_output ("Ciphertext hex: ", ciphertext);
    
    CFB_Mode<AES>::Decryption aes_decrypt (m_encryKey, AES::DEFAULT_KEYLENGTH, m_iv);

    StreamTransformationFilter *cfbDecryptor = 
      new StreamTransformationFilter (aes_decrypt, new Base64Decoder (new StringSink (plaintext)));

    StringSource source (ciphertext, true, cfbDecryptor);

    CipherText::hex_output ("Plaintext hex: ", plaintext);
    CipherText::char_output ("Plaintext char: ", plaintext);
  }
    
  catch ( InvalidDataFormat err ) {
    cout << "AES_CFB_CipherEngine FAILURE" << endl;
    cout << "Cipher text length: " << ciphertext.length() << endl;
    cout << "Plain text length:  " << plaintext.length() << endl;
    cout << "InvalidDataFormat: " << err.GetWhat() << endl;
    exit(0);
  }

  return plaintext; 
}

/*---------------------
 * general functions
 *---------------------*/
void load (AES_CFB_CipherEngine& m_cipher, string filename) {

  ifstream input_file;
  stringstream input_data;

  input_file.open (filename.c_str(), ios::binary);

  if (input_file.is_open()) {
    string input_line;
    while (! input_file.eof()) {
      std::getline (input_file, input_line);
      input_data << input_line;
    }
    input_file.close();

    /* Line Feed (0xa) is ignored when using std::getline */
    input_data << (char)10;
    string plain_text = m_cipher.decrypt ( input_data.str() );
    cout << "Decrypted output:" << endl;
    cout << plain_text << endl;
  }
  else {
    cout << "Failed to load " << filename << endl;
    exit(0);
  }
}

void save (AES_CFB_CipherEngine& m_cipher, string filename, string plaintext) {

  ofstream output_file;
  output_file.open (filename.c_str(), ios::binary);
  if (output_file.is_open()) {
    string cipher_text = m_cipher.encrypt ( plaintext );
    output_file << cipher_text << endl;
  }
  else {
    cout << "Failed to save " << filename << endl;
    exit(0);
  }
}

int main () {
  string filename = "Foo.txt";
  string plaintext = "Boo Hoo I am so scared";
  AES_CFB_CipherEngine m_cipher ("AES (CFB)", 0);

  save (m_cipher, filename, plaintext);
  load (m_cipher, filename);
  
  return 0;
}

Attachment: encryption_test.strace
Description: Binary data

Reply via email to