jump to navigation

Utilizing rijndael source code March 6, 2007

Posted by TSAI HONG-BIN in Programming.
trackback

I hate to admit that I’m not a code guru, well, sadly my code has proven everything.


#include
#include
#include
#include
#include "rijndael-api-fst.h"

#define KEYSIZE 256
#define IVSIZE 64

int getiv(BYTE *iv){
FILE *fp;
fp = fopen("key.16", "rb");
if(fgets(iv, (IVSIZE/4)+1, fp) > 0){
return 1;
}
else{
return 0;
}
return 1;
}

int getkey(BYTE *keydata){
FILE *fp;
fp = fopen("key.256","rb");

if(fgets(keydata, (KEYSIZE/4)+1, fp) >0){
return 1;
}
else{
printf("read key error\n");
return 0;
}

return 1;
}

int encrypt(char *file){
char cfile[32];
sprintf(cfile, "%s.enc", file);
int bytesRead=0;
int ret = 0;
BYTE key[KEYSIZE/4+1];
memset(key, '', sizeof(key)); // clean up key space
BYTE IV[IVSIZE/4+1];
memset(IV, '', sizeof(IV));
getkey(key); // read keyMaterial from keyfile
getiv(IV);
keyInstance keyInst;
cipherInstance cipherInst;

BYTE pbuf[1024], cbuf[1024];
memset(pbuf, '', sizeof pbuf); // clean up plaintext space
memset(cbuf, '', sizeof cbuf); // clean up ciphertext space

makeKey(&keyInst, DIR_ENCRYPT, KEYSIZE, key); // run rijndael key schedule
cipherInit(&cipherInst, MODE_CBC, NULL); // initiate cipher mode

FILE *fp;
fp = fopen(file, "rb");

FILE *cfp;
cfp = fopen(cfile, "wb");

for(;;){
bytesRead = fread(pbuf, 1, (sizeof pbuf), fp); // read 1024 bytes from file at a time

if (bytesRead \n");

}

int main(int argc, char *argv[]){
if (argc > 1){
if( strcmp( argv[1], "encrypt") == 0){
printf("start encrypting file %s\n", argv[2]);
encrypt(argv[2]);
}
else if( strcmp( argv[1], "decrypt") == 0){
decrypt(argv[2]);
}
else{
usage();
return 0;
}
}
else{
usage();
return 0;
}
return 1;
}

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: