Regarding encryption of text file using AES128bit in cbc mode issue

39 views
Skip to first unread message

Poonam Yadav

unread,
Jan 3, 2014, 5:33:42 AM1/3/14
to cryptop...@googlegroups.com
Hello all,

the given below is the code I have made on encryption and Decryption of text file but I am not getting the same output in original So  please let me know if anybody have idea on it.


encryption code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, size_t len)
{
    const unsigned char * p = (const unsigned char*)pv;
    if (NULL == pv)
        printf("NULL");
    else
    {
        size_t i = 0;
        for (; i<len;++i)
            printf("%02X ", *p++);
    }
    printf("\n");
}

FILE *inpointer,*outpointer;
struct stat st;

// main entrypoint
int main(int argc, char **argv)
{
int size,keylength;
unsigned char ckey[] =  "thiskeyisverybad";
unsigned char ivec[] = "dontusethisinput";
  stat("input.txt", &st);
  size = st.st_size;
  printf("size is %d \n",size);
  inpointer=fopen("input.txt","r");
  outpointer=fopen("encryptinput.txt","w");
AES_KEY enc_key;
    AES_set_encrypt_key(ckey, 128, &enc_key);
const size_t encslength = ((size + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;   
unsigned char indata[size];
unsigned char outdata[encslength];
    memset(indata,0,sizeof(indata));
    fread(indata,size,1, inpointer);
    memset(outdata,0,sizeof(outdata));
    AES_cbc_encrypt(indata, outdata,size,&enc_key,ivec,AES_ENCRYPT);
    fwrite(outdata,sizeof(outdata),1,outpointer);
    printf("original:\t");
    hex_print(indata, sizeof(indata));
    printf("encrypt:\t");
    hex_print(outdata, sizeof(outdata));
  fclose(inpointer);
  fclose(outpointer);
return 0;
}


Decryption code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, size_t len)
{
    const unsigned char * p = (const unsigned char*)pv;
    if (NULL == pv)
        printf("NULL");
    else
    {
        size_t i = 0;
        for (; i<len;++i)
            printf("%02X ", *p++);
    }
    printf("\n");
}

FILE *inpointer,*outpointer;
struct stat st;

// main entrypoint
int main(int argc, char **argv)
{
int size,keylength;
unsigned char ckey[] =  "thiskeyisverybad";
unsigned char ivec[] = "dontusethisinput";
  stat("input.txt", &st);
  size = st.st_size;
inpointer=fopen("encryptinput.txt","r");
outpointer=fopen("output.txt","w");
AES_KEY dec_key;
  AES_set_encrypt_key(ckey,128,&dec_key);
const size_t encslength = ((size + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;   
unsigned char indata[encslength];
unsigned char outdata[size];
 memset(indata,0,sizeof(indata));
 fread(indata,encslength,1,inpointer);
 memset(outdata,0,sizeof(outdata));
 AES_cbc_encrypt(indata, outdata,encslength,&dec_key,ivec,AES_DECRYPT);
 fwrite(outdata,size,1,outpointer);
  printf("original:\t");
  hex_print(indata, sizeof(indata));
  printf("decrypt:\t");
  hex_print(outdata,sizeof(outdata));
  fclose(inpointer);
  fclose(outpointer);
return 0;
}
 

encrypt output:
original plaintext:    68 65 6C 6C 6F 74 68 69 73 69 73 65 6E 63 72 69 0A
encrypt data:    3C 5D 78 9E 47 91 BD BF 55 F4 D6 D3 07 1F 0C 5C 3A 9F 88 9F 0E C6 08 32 FA 3E 7F 34 61 F5 83 28

decrypt output:
original(encrypted data):    3C 5D 78 9E 47 91 BD BF 55 F4 D6 D3 07 1F 0C 5C 3A 9F 88 9F 0E C6 08 32 FA 3E 7F 34 61 F5 83 28
decrypt:    62 20 FF DE B9 C4 3E 43 AB 04 B9 76 F8 6B 72 AA 82


here plaintext and decrypt data are not same.

Regards,
Poonam
Reply all
Reply to author
Forward
0 new messages