innevolution

openssl AES_cbc_encrypt

Jul 17th, 2013
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. //    gcc -o myap openssl.c -lcrypto
  2. #include "openssl/aes.h"
  3. #include "string.h"
  4. #include "stdio.h"
  5.  
  6.  
  7. int main (int i, char** b){
  8.   char filename[] = "file.txt";
  9.   FILE *file = fopen ( filename, "r" );
  10.   char key[] = "mysecretpassword";
  11.   if (file != NULL) {
  12.     char line [1000];
  13.     char *p = line;
  14.     char *array = line;
  15.     while(fgets(line,sizeof line,file)!= NULL) {
  16.       int ivSize = 16;
  17.       int strLen = strlen(line) - ivSize;
  18.  
  19.       fprintf(stdout,"%s\n",line);
  20.       char otherString[strLen + 1];
  21.  
  22.       strncpy(otherString, p, strLen);
  23.       otherString[strLen] = '\0';
  24.  
  25.       printf("%s \n", otherString);//Here I got the Encrypted string
  26.       array = array + strLen;
  27.       printf("%s \n",array);//Here I got the IV
  28.  
  29.       char buffer[1000];
  30.       AES_KEY dec_key;
  31.       AES_set_decrypt_key(key, 128, &dec_key);
  32.       AES_cbc_encrypt(otherString, buffer, strLen, &dec_key, array, AES_DECRYPT);
  33.       printf("result is: %s\n", buffer);
  34.  
  35.       //Here how to decrypt the Cipher text using "IV" and "key"
  36.     }
  37.     fclose(file);
  38.   }
  39.   else {
  40.     perror(filename);
  41.   }
  42.   return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment