Advertisement
Guest User

encrypt decrypt

a guest
Dec 7th, 2021
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.02 KB | None | 0 0
  1.  
  2. int encrypt(unsigned char *key, unsigned char *iv, char *plaintext_file)
  3. {
  4.     EVP_CIPHER_CTX *ctx;
  5.     const EVP_CIPHER *cipher_type = EVP_aes_256_cbc();
  6.     int cipher_block_size = EVP_CIPHER_block_size(cipher_type);
  7.     unsigned char in_buf[BUFSIZE], out_buf[BUFSIZE + cipher_block_size];
  8.     int num_bytes_read, out_len;
  9.  
  10.     FILE *fIN = fopen(plaintext_file,"rb");
  11.     if(fIN==NULL)
  12.     {
  13.        handleErrors();
  14.     }
  15.     char encrypted_file[1024];
  16.     snprintf(encrypted_file,sizeof(encrypted_file),"%s.%s",plaintext_file,ENCRYPT_EXT);
  17.     printf("%s\n",encrypted_file);
  18.     FILE *fOUT = fopen(encrypted_file,"wb");
  19.     if(fOUT==NULL)
  20.     {
  21.        handleErrors();
  22.     }
  23.    
  24.     /* Create and initialise the context */
  25.     if(!(ctx = EVP_CIPHER_CTX_new()))
  26.         handleErrors();
  27.  
  28.     /*
  29.      * Initialise the encryption operation. IMPORTANT - ensure you use a key
  30.      * and IV size appropriate for your cipher
  31.      * In this example we are using 256 bit AES (i.e. a 256 bit key). The
  32.      * IV size for *most* modes is the same as the block size. For AES this
  33.      * is 128 bits
  34.      */
  35.     if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
  36.         handleErrors();
  37.  
  38.     /*
  39.      * Provide the message to be encrypted, and obtain the encrypted output.
  40.      * EVP_EncryptUpdate can be called multiple times if necessary
  41.      */
  42.     num_bytes_read = fread(in_buf, sizeof(unsigned char), BUFSIZE, fIN);
  43.  
  44.     while(num_bytes_read > 0)
  45.     {  
  46.         if(!EVP_EncryptUpdate(ctx, out_buf, &out_len, in_buf, num_bytes_read)){
  47.             handleErrors();}
  48.  
  49.     fwrite(out_buf, sizeof(unsigned char), out_len, fOUT);
  50.     num_bytes_read = fread(in_buf, sizeof(unsigned char), BUFSIZE, fIN);
  51.  
  52.     }
  53.     if(1 != EVP_EncryptFinal_ex(ctx, out_buf, &out_len))
  54.         handleErrors();
  55.  
  56.     /*
  57.      * Finalise the encryption. Further ciphertext bytes may be written at
  58.      * this stage.
  59.      */
  60.  
  61.     fwrite(out_buf, sizeof(unsigned char), out_len, fOUT);
  62.  
  63.     /* Clean up */
  64.     EVP_CIPHER_CTX_free(ctx);
  65.    
  66.     fclose(fIN);
  67.     fclose(fOUT);
  68.  
  69.     return 1;
  70. }
  71.  
  72.  
  73. int decrypt(unsigned char *key, unsigned char *iv, char *cipher_file)
  74. {
  75.     EVP_CIPHER_CTX *ctx;
  76.     const EVP_CIPHER *cipher_type = EVP_aes_256_cbc();
  77.     int cipher_block_size = EVP_CIPHER_block_size(cipher_type);
  78.     unsigned char in_buf[BUFSIZE], out_buf[BUFSIZE + cipher_block_size];
  79.     int num_bytes_read, out_len;
  80.  
  81.     FILE *fIN = fopen(cipher_file,"rb");
  82.     if(fIN==NULL)
  83.     {
  84.        handleErrors();
  85.     }
  86.     char plaintext_file[1024];
  87.     snprintf(plaintext_file,strlen(cipher_file)-(EXT_LEN),"%s",cipher_file);
  88.     FILE *fOUT = fopen(plaintext_file,"wb");
  89.     if(fOUT==NULL)
  90.     {
  91.        handleErrors();
  92.     }
  93.  
  94.  
  95.     /* Create and initialise the context */
  96.     if(!(ctx = EVP_CIPHER_CTX_new()))
  97.         handleErrors();
  98.  
  99.     /*
  100.      * Initialise the decryption operation. IMPORTANT - ensure you use a key
  101.      * and IV size appropriate for your cipher
  102.      * In this example we are using 256 bit AES (i.e. a 256 bit key). The
  103.      * IV size for *most* modes is the same as the block size. For AES this
  104.      * is 128 bits
  105.      */
  106.     if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
  107.         handleErrors();
  108.  
  109.     /*
  110.      * Provide the message to be decrypted, and obtain the plaintext output.
  111.      * EVP_DecryptUpdate can be called multiple times if necessary.
  112.      */
  113.     num_bytes_read = fread(in_buf, sizeof(unsigned char), BUFSIZE, fIN);
  114.  
  115.     while(num_bytes_read > 0)
  116.     {
  117.         if(!EVP_DecryptUpdate(ctx, out_buf, &out_len, in_buf, num_bytes_read)){
  118.                         handleErrors();}
  119.  
  120.         fwrite(out_buf, sizeof(unsigned char), out_len, fOUT);
  121.         num_bytes_read = fread(in_buf, sizeof(unsigned char), BUFSIZE, fIN);
  122.  
  123.     }
  124.    if(1 != EVP_DecryptFinal_ex(ctx, out_buf, &out_len))
  125.         handleErrors();
  126.  
  127.     fwrite(out_buf, sizeof(unsigned char), out_len, fOUT);
  128.  
  129.    
  130.  
  131.     /* Clean up */
  132.     fclose(fOUT);
  133.     fclose(fIN);
  134.     EVP_CIPHER_CTX_free(ctx);
  135.  
  136.     return 1;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement