I am trying to build a program the takes in a file (an EXE of arbitrary size), encrypts its and copies it to a structure. Then decrypt it later and make sure it is the same for use.
I am having a hard time encrypting then decrypting the file. It seems to not be encrypting properly and I do not know how to test it.
Here are my questions:
- What am I doing wrong here?
- Is there a better library to encrypt using AES? or shall I stick with openSSL
- Lets say I wanted to use another key say "HelloWorld". Can I just use that string and use it as an argument for the encryption algorithm? Do I have to set the correct bit length of the key? If so how?
Code:
struct structData{
unsigned char * FileBuffer;
unsigned long FileSize;
//More stuff in here
};
struct Data sData;
/*
I load the data here, and fill in the data etc
*/
unsigned char Key[]={ //128bit key
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
};
unsigned char *enc_data = malloc(sData->FileSize);//Temporary holder for the File
AES_KEY enc_key;
AES_set_encrypt_key(Key,128,&enc_key);//Put key defined here
AES_encrypt(sData->FileBuffer,enc_data,&enc_key);
sData->FileBuffer = enc_data;//This should move the stuff over
//Should be encrypted here
sData->FileBuffer = enc_data;//Copy the output to the file buffer
free(enc_data);//Free memory
AES_KEY dec_key;
AES_set_decrypt_key(Key, 128,&dec_key);
AES_decrypt(sData->FileBuffer,dec_data,&dec_key);
sData->FileBuffer = dec_data;
free(dec_data);
Anything would help, hopefully I am heading in the right direction, my C skills are a bit rusty.