March 21st, 2010, 07:21 PM
-
AES Encrypt/Decrypt in C
Hi how can i encrypt a file in aes with 256 key in c?
Thank you very much!
March 22nd, 2010, 07:37 AM
-
Originally Posted by iassael
Hi how can i encrypt a file in aes with 256 key in c?
Thank you very much!
or c++
any suggestions?
March 22nd, 2010, 10:19 AM
-
Alright...
First, get a decent AES- implementation.
There are many freely available; I picked this one at random; it's BSD- licensed (meaning you can use it for whatever you want, just mention Brian Gladman in a copyright notice).
For building the library (I didn't bother with the slightly faster ASM- version), I just used:
Code:
gcc -c -O2 -fomit-frame-pointer aescrypt.c aeskey.c aestab.c aes_modes.c
ar rcs libaes.a *.o
If you use Visual Studio, get the corresponding project files...
Here is an example of how your program could look like:
c Code:
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "aes.h"
void encrypt(const char *fileIn, const char *fileOut,
const unsigned char *key);
void decrypt(const char *fileIn, const char *fileOut,
const unsigned char *key);
int main() {
const unsigned char key[] = "my key";
srand(time(NULL));
aes_init();
encrypt("main.c", "main.c.encrypted", key);
decrypt("main.c.encrypted", "main.c.decrypted", key);
return 0;
}
void encrypt(const char *fileIn, const char *fileOut,
const unsigned char *key) {
int i;
aes_encrypt_ctx ctx[1];
unsigned char iv[16]; /* initialisation vector */
unsigned char inBuffer[200], outBuffer[200];
FILE *inFile = fopen(fileIn, "rb");
FILE *outFile = fopen(fileOut, "wb");
/* pick a random initialisation vector */
for(i = 0; i < 16; ++i)
iv[i] = rand() & 0xFF;
fwrite(iv, 1, 16, outFile);
aes_encrypt_key256(key, ctx);
while((i = fread(inBuffer, 1, sizeof(inBuffer), inFile)) > 0) {
aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
fwrite(outBuffer, 1, i, outFile);
}
fclose(inFile);
fclose(outFile);
}
void decrypt(const char *fileIn, const char *fileOut,
const unsigned char *key) {
int i;
aes_encrypt_ctx ctx[1];
unsigned char iv[16]; /* initialisation vector */
unsigned char inBuffer[200], outBuffer[200];
FILE *inFile = fopen(fileIn, "rb");
FILE *outFile = fopen(fileOut, "wb");
/* read initialization vector from file */
if(fread(iv, 1, 16, inFile) < 16) {
fclose(inFile);
fclose(outFile);
return; /* error: file doesn't even contain an initialisation vector */
}
aes_encrypt_key256(key, ctx);
while((i = fread(inBuffer, 1, sizeof(inBuffer), inFile)) > 0) {
aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
fwrite(outBuffer, 1, i, outFile);
}
fclose(inFile);
fclose(outFile);
}
You have to link with the library you previously created, and "aes.h", "brg_types.h" must be in your includepath.
Note that I'm pretty clueless about cryptography- storing the initialisation vector as plaintext inside the encrypted file may or may not be "secure", but I guess it's better than using always the same one, and seems to be common practise...
--edit: fixed typos
March 22nd, 2010, 11:49 AM
-
Originally Posted by h4rdc0ded
Alright...
First, get a decent AES- implementation.
There are many freely available; I picked
this one at random; it's BSD- licensed (meaning you can use it for whatever you want, just mention Brian Gladman in a copyright notice).
For building the library (I didn't bother with the slightly faster ASM- version), I just used:
Code:
gcc -c -O2 -fomit-frame-pointer aescrypt.c aeskey.c aestab.c aes_modes.c
ar rcs libaes.a *.o
If you use Visual Studio, get the corresponding project files...
Here is an example of how your program could look like:
c Code:
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "aes.h"
void encrypt(const char *fileIn, const char *fileOut,
const unsigned char *key);
void decrypt(const char *fileIn, const char *fileOut,
const unsigned char *key);
int main() {
const unsigned char key[] = "my key";
srand(time(NULL));
aes_init();
encrypt("main.c", "main.c.encrypted", key);
decrypt("main.c.encrypted", "main.c.decrypted", key);
return 0;
}
void encrypt(const char *fileIn, const char *fileOut,
const unsigned char *key) {
int i;
aes_encrypt_ctx ctx[1];
unsigned char iv[16]; /* initialisation vector */
unsigned char inBuffer[200], outBuffer[200];
FILE *inFile = fopen(fileIn, "rb");
FILE *outFile = fopen(fileOut, "wb");
/* pick a random initialisation vector */
for(i = 0; i < 16; ++i)
iv[i] = rand() & 0xFF;
fwrite(iv, 1, 16, outFile);
aes_encrypt_key256(key, ctx);
while((i = fread(inBuffer, 1, sizeof(inBuffer), inFile)) > 0) {
aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
fwrite(outBuffer, 1, i, outFile);
}
aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
fwrite(outBuffer, 1, i, outFile);
fclose(inFile);
fclose(outFile);
}
void decrypt(const char *fileIn, const char *fileOut,
const unsigned char *key) {
int i;
aes_encrypt_ctx ctx[1];
unsigned char iv[16]; /* initialisation vector */
unsigned char inBuffer[200], outBuffer[200];
FILE *inFile = fopen(fileIn, "rb");
FILE *outFile = fopen(fileOut, "wb");
/* read initialization vector from file */
if(fread(iv, 1, 16, inFile) < 16)
return; /* error: file doesn't even contain an initialisation vector */
aes_encrypt_key256(key, ctx);
while((i = fread(inBuffer, 1, sizeof(inBuffer), inFile)) > 0) {
aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
fwrite(outBuffer, 1, i, outFile);
}
fclose(inFile);
fclose(outFile);
}
You have to link with the library you previously created, and "aes.h", "brg_types.h" must be in your includepath.
Note that I'm pretty clueless about cryptography- storing the initialisation vector as plaintext inside the encrypted file may or may not be "secure", but I guess it's better than using always the same one, and seems to be common practise...
--edit: fixed typos
thanks man!!! Its perfect i will check it in a couple of hours :)
March 22nd, 2010, 08:41 PM
-
Originally Posted by iassael
thanks man!!! Its perfect i will check it in a couple of hours :)
although i included the source and was found i get a lot of "unidentified reference to aes_init()" like errors ...
Any suggestions?
Thanks!
March 23rd, 2010, 12:02 AM
-
Set your project path settings to point to the AES lib
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
March 23rd, 2010, 09:41 AM
-
Originally Posted by Scorpions4ever
Set your project path settings to point to the AES lib
of course i did it.... :)
March 23rd, 2010, 05:33 PM
-
u r right it works perfect many thanks! :)
February 20th, 2013, 04:31 AM
-
need your help doesn't work
hi :hi:
I try to execute the code but it doesn't work :( please if you don't mind telling me the steps in details because I don't have a good experience in c and I will be thankful :D
February 20th, 2013, 04:26 PM
-
Originally Posted by 7anoun
hi :hi:
I try to execute the code but it doesn't work :( please if you don't mind telling me the steps in details because I don't have a good experience in c and I will be thankful :D
Do you have a text file named main.c in the same folder as your successfully compiled binary?
Have you built the library as follows?
gcc -c -O2 -fomit-frame-pointer aescrypt.c aeskey.c aestab.c aes_modes.c
ar rcs libaes.a *.o
Have you compiled your test app as follows (assuming the source is named test.cpp?
gcc test.cpp libaes.a