#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <openssl/evp.h>

void test0()
{
  
unsigned char key[16]="2222222222222222";
unsigned char temp[18];
unsigned char out[18];
unsigned char iv[8]="01234567";

int olen, i, tlen;
memset(temp, 50, 18);
memset(out, 0, 18);
temp[0] = 0; temp[1] = 16;

EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init (&ctx);
EVP_EncryptInit(&ctx, EVP_aes_128_ofb (), key, iv);
EVP_EncryptUpdate (&ctx, out, &olen, temp, 18);
EVP_EncryptFinal (&ctx, out + olen, & tlen) ;
EVP_CIPHER_CTX_cleanup(&ctx);

for (i=0; i<olen+tlen; i++)
{
        	
    fprintf(stderr,"input:%d encrypted:%d\n", temp[i], out[i]);

} 

return ;
}

void test1()
{
  
unsigned char key[16]="2222222222222222";
unsigned char iv[8]="01234567";
unsigned char temp[18];
unsigned char out[18];

int olen, i, tlen;
memset(temp, 50, 18);
memset(out, 0, 18);
temp[0] = 0; temp[1] = 16;

EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init (&ctx);
EVP_EncryptInit(&ctx, EVP_aes_128_ofb (), key, iv);
EVP_EncryptUpdate (&ctx, out, &olen, temp, 18);
EVP_EncryptFinal (&ctx, out + olen, & tlen) ;
EVP_CIPHER_CTX_cleanup(&ctx);

for (i=0; i<olen+tlen; i++)
{
        	
    fprintf(stderr,"input:%d encrypted:%d\n", temp[i], out[i]);

} 

return ;
}

static void test2(int len)
{
    unsigned char key[16]="2222222222222222";
    unsigned char iv[8]="01234567";
int i = 0, tlen;
unsigned char *temp1 = (unsigned char *)malloc(len); 
unsigned char *buf = (unsigned char *)malloc(len); 
int olen = 0;
EVP_CIPHER_CTX ctx;
FILE *	infile = fopen("data.dat","rb");
fread(buf, len, 1, infile);
fclose(infile);
EVP_CIPHER_CTX_init (&ctx);
EVP_DecryptInit(&ctx, EVP_aes_128_ofb (), key, iv);
EVP_DecryptUpdate (&ctx, temp1, &olen, buf, len);
EVP_DecryptFinal (&ctx, temp1 + olen, & tlen) ;
        for (i=0; i<len; i++)
        {
        	
fprintf(stderr,"input %d decrypted %d. \n", buf[i], temp1[i]);

        }
  free(temp1);
  free(buf);
EVP_CIPHER_CTX_cleanup(&ctx);
return;
}

int main (int argc, char *argv[])
{
  test0();
  test1();
  test2(18);
  return 0;
}
