FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


AES encryption in C++





Fire Boar
Hey, I found this lightweight library on the internet. It's an implementation of the Rijndael algorithm (AES) and I've modified it slightly to work cross-platform. However, there are some problems. Here's the test code:

Code:
#include "AES.h"
#include <fstream>

// Encrypt a file
void AESEncryptFile(const char * psFileName)
{
   // Open the input/output file streams
   std::ifstream ifile(psFileName, std::ios_base::binary);
   std::ofstream ofile("temp.file", std::ios_base::binary);

   // Get the file's size
   ifile.seekg(0, std::ios_base::end);
   int size,fsize = ifile.tellg();
   ifile.seekg(0, std::ios_base::beg);

   // Round the size up to 16 bytes
   size = (fsize+15)&(~15);

   char * ibuffer = new char[size];
   char * obuffer = new char[size];
   ifile.read(ibuffer,fsize);

   AES crypt;
   crypt.SetParameters(128);
   // Use a sample key
   unsigned char key[16] = "0123456789abcde";
   crypt.StartEncryption(key);
   crypt.Encrypt(reinterpret_cast<const unsigned char*>(ibuffer),reinterpret_cast<unsigned char*>(obuffer),size/16);

   ofile.write(obuffer,size);

   delete [] ibuffer;
   delete [] obuffer;

   ofile.close();
   ifile.close();
}

// Decrypt a file
void AESDecryptFile(const char * psFileName)
{
   // Open the input/output file streams
   std::ifstream ifile("temp.file", std::ios_base::binary);
   std::ofstream ofile(psFileName, std::ios_base::binary);

   // Get the file's size
   ifile.seekg(0, std::ios_base::end);
   int size,fsize = ifile.tellg();
   ifile.seekg(0, std::ios_base::beg);

   // Round the size up to 16 bytes
   size = (fsize+15)&(~15);

   char * ibuffer = new char[size];
   char * obuffer = new char[size];
   ifile.read(ibuffer,fsize);

   AES crypt;
   crypt.SetParameters(128);
   // Use a sample key
   unsigned char key[16] = "0123456789abcde";
   crypt.StartDecryption(key);
   crypt.Decrypt(reinterpret_cast<const unsigned char*>(ibuffer),reinterpret_cast<unsigned char*>(obuffer),size/16);

   ofile.write(obuffer,size);

   delete [] ibuffer;
   delete [] obuffer;

   ofile.close();
   ifile.close();
}

int main()
{
   // test a file encryption
   AESEncryptFile("main.cpp");
   AESDecryptFile("test");

   return 0;
}


This uses Chris Lomont's AES library.

AES.h
AES.cpp

It doesn't seem to be encrypting all the file, since the ciphertext file is about half the size of the plaintext. I have admittedly changed a couple of things in AES.cpp to make it cross-platform (defined the rotr and rotl functions is all I think I did), but that shouldn't have made it break horribly. I think the problem lies in main.cpp (the code posted above). Any ideas?
Related topics
Command and Conquer
Funny C variable
What can I do with C-Panel?
A "small" list of free apps
HELP ME in C++
Languages in c++
C++ TUTORIAL : INTRODUCTION TO C++
I searching for a site
Encrypt with C#, Decrypt with PHP
Getting wpa-psk working on a dell 1370 on LINUX.
which server operating system?
WEP or WPA?
I'm still a noobie at home networks, please help...
Encrypting and decrypting a file in Python
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.