am having a problem with AES encryption, decryption using 
crypto++4.2. The decrypted string contains extra characters than the 
input.

I am attaching the input files. The code follows:

#include <iostream.h>
#include "aes.h"

#define KEYSIZE 16

using namespace CryptoPP;
using namespace CryptoPP;

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

    byte key[KEYSIZE];
    memcpy(key, "1234567890123456", KEYSIZE);

    AESEncryption aesEnc(key, KEYSIZE);
  
    char in[] = "toEncrypt.txt";
    char enc[] = "encrypted.txt";
    
    // read in binary mode is this correct?
    FILE* fp = fopen(in, "rb");
    if (!fp)
    {
        cout<<"file could not be opened: "<<in;
        return;
    }
    // write in binary mode is this correct?
    FILE* outFp = fopen(enc, "wb");
    if (!outFp)
    {
        cout<<"file could not be opened: "<<enc;
        return;
    }
    
    byte buffer[KEYSIZE];
    byte encrypted[KEYSIZE];    
    while (1)
    {
        if (feof(fp))
            break;
        memset(buffer, 0, KEYSIZE);
        fread(buffer,sizeof(byte), KEYSIZE,fp);
        memset(encrypted, 0, KEYSIZE);
        aesEnc.ProcessBlock(buffer, encrypted);
          // am I doing something wrong here..(KEYSIZE) ??
        fwrite(encrypted, sizeof(byte), KEYSIZE, outFp);
    }
    
    fclose(fp);
    fclose(outFp);
    /****************encrypt***********************/

    
    /****************decrypt***********************/
    AESDecryption aesDec(key, KEYSIZE);
    
    char dec[] = "decrypted.txt";
    fp = fopen(enc, "rb");
    if (!fp)
    {
        cout<<"file could not be opened: "<<in;
        return;
    }
    outFp = fopen(dec, "wb");
    if (!outFp)
    {
        cout<<"file could not be opened: "<<dec;
        return;
    }
    
    byte decrypted[KEYSIZE];
    while (1)
    {
        if (feof(fp))
            break;
        memset(buffer, 0, KEYSIZE);
        fread(buffer,sizeof(byte),KEYSIZE,fp);
        memset(decrypted, 0, KEYSIZE);
        aesDec.ProcessBlock(buffer, decrypted);
        fwrite(decrypted, sizeof(byte), KEYSIZE, outFp);
    }
    
    fclose(fp);
    fclose(outFp);
    /****************decrypt***********************/

    return;
}


Reply via email to