C中的Aes-128-cbc加密和openssl中的解密

时间:2013-07-05 12:39:02

标签: openssl aes

我编写了一个代码来读取文件并使用Aes-cbc-128

对其进行加密
# include <stdio.h>
//# include <ctype.h>
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <openssl/aes.h>

# define BLOCK_LEN  16
# define MAXBUF     256
# define MAXHEX     (MAXBUF * 2) + 1
# define KEY        "0123456789abcdef"
# define IV     "RandomIVRandomIV"
unsigned int bin2hex (unsigned char *in, unsigned char *out, unsigned int len);
unsigned int hex2bin (unsigned char *in, unsigned char *out, unsigned int len);
unsigned int PadData (unsigned char *ibuf, unsigned int ilen, int blksize);
unsigned int NoPadLen (unsigned char *ibuf, unsigned int ilen);


 void print_hex(char *iv)
     {int i=0;


        for (i =0; i < 16; ++i)
            printf("%02x", iv[i]);

        printf("\n"); 

     }

void callkey (unsigned char *keyFileName)
{
   unsigned char key[] = KEY;

FILE *keyFile = fopen(keyFileName, "wb");
 fwrite(key, 1, 16, keyFile);
        fclose(keyFile);
printf("Key Hex : "); 
    print_hex(key);
}


int
main(int argc, char *argv[])
{
  unsigned long ilen;
  unsigned char data[MAXBUF];   // command line input
  unsigned char ibuf[MAXBUF + BLOCK_LEN];   // padded input to encrypt
  unsigned char obuf[MAXHEX];   // encrypt output
  unsigned char xbuf[MAXHEX];   // hex encrypt output
  unsigned char ybuf[MAXBUF];   // hex decrypt output
  unsigned char dbuf[MAXBUF];   // decrypt output
  unsigned char key[] = KEY;
  unsigned char iv[]  = IV;
  AES_KEY aeskeyEnc, aeskeyDec;

  unsigned int len =0;

  FILE *inFile = fopen(argv[1], "rb");

  FILE *outFile = fopen(argv[2], "wb");
    unsigned char *k1ey = "static.key";
    callkey(k1ey);


    while((len = fread(data, 1, sizeof(data), inFile)) > 0) 
    { 
     // prepare the input data with padding
     printf("len = %d \n", len);
     memset (ibuf, 0x00, MAXBUF);
     memcpy (ibuf, data, len); 


    // pad and calc length of block to encode
     ilen = PadData (ibuf, len, BLOCK_LEN);
    printf("ilength = %ld \n", ilen);

    // init cipher keys 
    AES_set_encrypt_key (key, 128, &aeskeyEnc);
    AES_set_decrypt_key (key, 128, &aeskeyDec);



    // encrypt string
    memcpy (iv,IV, sizeof (IV));
    memset(obuf, 0x00, MAXHEX);
    AES_cbc_encrypt (ibuf, obuf, ilen, &aeskeyEnc, iv, AES_ENCRYPT);
    len = fwrite(obuf,1, ilen, outFile);
    printf("writtenLen: %d\n", len);

    memset(data, 0x00, MAXBUF);

  } // while (1)


  fclose(inFile);

  fclose(outFile);




  return (0);
}


/*
 * The PadData() function is used to pad input data for cipher block
 * chaining using standard padding as specified in PKCS#5.  Input data
 * is padded in place.  The ilen value is the length of the data before
 * padding.  The blksize parameter is the block size to use (16 for AES
CBC).
 * The function returns the new length of ibuf after padding.
 *
 * NOTE: The ibuf must be large enough to hold at least blksize additional
 * bytes of data to accommodate padding.
 */

unsigned int
PadData (unsigned char *ibuf, unsigned int ilen, int blksize)
{
  unsigned int   i;         // loop counter
  unsigned char  pad;           // pad character (calculated)
  unsigned char *p;         // pointer to end of data



 //if(ilen % blksize == 0)
//   return ilen; 


  // calculate pad character
  pad = (unsigned char) (blksize - (ilen % blksize));


  // append pad to end of string
  p = ibuf + ilen;
  for (i = 0; i < (int) pad; i++) {
    *p = pad;
    ++p;
  }

  return (ilen + pad);
}

在openssl中,我解密了我编码的文件,

gcc aescodefile -o aes -lcrypto -lssl -ldl    #this is to to complile code
./aes filetobeencrypted.txt    encryptedfile  #execute file

openssl enc -aes-128-cbc -d -in filetobeencrypted.txt -out aes-decrypted.c  -p -nosalt -K 30313233343536373839616263646566 -iv 52616e646f6d495652616e646f6d4956

此代码在解密后提供一些垃圾值。我无法增加缓冲区大小。 请建议如何纠正此问题。

1 个答案:

答案 0 :(得分:0)

问题是C代码为每个256字节缓冲区执行填充。相反,它应该加密而不填充 256字节的块。然后它应填充最后一个纯文本块并加密(即使它是一个完整的块大小或0字节)。