Guest User

Untitled

a guest
Apr 16th, 2012
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Nastasie Ion Octavian
  3. http://tavi-tech.blogspot.com/2012/04/linux-aes-encryption-c.html
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <openssl/evp.h>
  10. #include <openssl/aes.h>
  11.  
  12. #define FILE_BUFFER AES_BLOCK_SIZE*1024
  13.  
  14. unsigned char* encrypt_data(unsigned char* data, int *data_len, unsigned char* key, unsigned char* iv)
  15. {
  16.     EVP_CIPHER_CTX enc_cipher;
  17.     unsigned char *enc_data;
  18.     int enc_len = 0, final_len = 0;
  19.  
  20.     EVP_CIPHER_CTX_init(&enc_cipher);
  21.     EVP_EncryptInit_ex(&enc_cipher, EVP_aes_256_cbc(), NULL, key, iv);
  22.  
  23.     enc_data = (unsigned char*)malloc(*data_len + AES_BLOCK_SIZE - *data_len % AES_BLOCK_SIZE);
  24.     if(!enc_data)
  25.         return NULL;
  26.  
  27.     EVP_EncryptUpdate(&enc_cipher, enc_data, &enc_len, data, *data_len);
  28.     EVP_EncryptFinal_ex(&enc_cipher, enc_data + enc_len, &final_len);
  29.  
  30.     *data_len = enc_len + final_len;
  31.     EVP_CIPHER_CTX_cleanup(&enc_cipher);
  32.     return enc_data;
  33. }
  34.  
  35. unsigned char* decrypt_data(unsigned char* data, int *data_len, unsigned char* key, unsigned char* iv)
  36. {
  37.     EVP_CIPHER_CTX dec_cipher;
  38.     unsigned char *dec_data;
  39.     int dec_len = 0, fin_len = 0;
  40.  
  41.     EVP_CIPHER_CTX_init(&dec_cipher);
  42.     EVP_DecryptInit_ex(&dec_cipher, EVP_aes_256_cbc(), NULL, key, iv);
  43.    
  44.     dec_data = (unsigned char *)malloc(*data_len);
  45.     if(!dec_data)
  46.         return NULL;
  47.    
  48.     EVP_DecryptUpdate(&dec_cipher, dec_data, &dec_len, data, *data_len);
  49.     EVP_DecryptFinal(&dec_cipher, dec_data + dec_len, &fin_len);
  50.  
  51.     *data_len = dec_len + fin_len;
  52.     EVP_CIPHER_CTX_cleanup(&dec_cipher);
  53.     return dec_data;
  54. }
  55.  
  56. int encrypt_file(char* file_in, char* file_out, unsigned char* key, unsigned char* iv)
  57. {
  58.     FILE *fin, *fout;
  59.     EVP_CIPHER_CTX enc_cipher;
  60.     unsigned char buffer[FILE_BUFFER];
  61.     unsigned char aux[FILE_BUFFER];
  62.     int read_len, write_len = 0;
  63.  
  64.     fin = fopen(file_in,"rb");
  65.     if(!fin)
  66.         return -1;
  67.     fout = fopen(file_out, "wb");
  68.     if(!fout)
  69.         return -2;
  70.  
  71.     EVP_CIPHER_CTX_init(&enc_cipher);
  72.     EVP_EncryptInit_ex(&enc_cipher, EVP_aes_256_cbc(), NULL, key, iv);
  73.    
  74.     while((read_len = fread(buffer,1,FILE_BUFFER,fin)) == FILE_BUFFER)
  75.     {
  76.         EVP_EncryptUpdate(&enc_cipher, aux, &write_len, buffer, read_len);
  77.         fwrite(aux,1,write_len, fout);
  78.     }
  79.     EVP_EncryptUpdate(&enc_cipher, aux, &write_len, buffer, read_len);
  80.     read_len = write_len;
  81.     EVP_EncryptFinal_ex(&enc_cipher, aux + write_len, &write_len);
  82.     fwrite(aux,1,read_len + write_len, fout);
  83.  
  84.     fclose(fin);
  85.     fclose(fout);
  86.     EVP_CIPHER_CTX_cleanup(&enc_cipher);
  87.     return 0;
  88. }
  89.  
  90. int decrypt_file(char* file_in, char* file_out, unsigned char* key, unsigned char* iv)
  91. {
  92.     FILE *fin, *fout;
  93.     EVP_CIPHER_CTX dec_cipher;
  94.     unsigned char buffer[FILE_BUFFER];
  95.     unsigned char aux[FILE_BUFFER];
  96.     int read_len, write_len = 0;
  97.  
  98.     fin = fopen(file_in,"rb");
  99.     if(!fin)
  100.         return -1;
  101.     fout = fopen(file_out, "wb");
  102.     if(!fout)
  103.         return -2;
  104.  
  105.  
  106.     EVP_CIPHER_CTX_init(&dec_cipher);
  107.     EVP_DecryptInit_ex(&dec_cipher, EVP_aes_256_cbc(), NULL, key, iv);
  108.  
  109.     while((read_len = fread(buffer,1,FILE_BUFFER,fin)) == FILE_BUFFER)
  110.     {
  111.         EVP_DecryptUpdate(&dec_cipher, aux, &write_len, buffer, read_len);
  112.         fwrite(aux,1,write_len, fout);
  113.     }
  114.     EVP_DecryptUpdate(&dec_cipher, aux, &write_len, buffer, read_len);
  115.     read_len = write_len;
  116.     EVP_DecryptFinal_ex(&dec_cipher, aux + read_len, &write_len);
  117.     fwrite(aux,1, read_len + write_len, fout);
  118.    
  119.     fclose(fin);
  120.     fclose(fout);
  121.     EVP_CIPHER_CTX_cleanup(&dec_cipher);
  122.     return 0;
  123. }
  124.  
  125. void cbuff()
  126. {
  127.     int ch;
  128.     while( (ch =(int)getchar()) != EOF && (char)ch != '\n');
  129. }
  130.    
  131. int main()
  132. {
  133.     unsigned char key[] = "012345678901234a";
  134.     unsigned char iv[] = "012435678901234d";
  135.     char inp[30], enc[30], dec[30];
  136.     int lene;
  137.  
  138.     printf("input: ");
  139.     scanf("%s",inp);
  140.     cbuff();
  141.    
  142.     printf("enc: ");
  143.     scanf("%s",enc);
  144.     cbuff();
  145.  
  146.     printf("dec: ");
  147.     scanf("%s",dec);
  148.     cbuff();
  149.    
  150.     encrypt_file(inp, enc, key, iv);
  151.     decrypt_file(enc, dec, key, iv);
  152.  
  153.     return 0;
  154. }
RAW Paste Data