Advertisement
anutka

Untitled

May 21st, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <openssl/evp.h>
  3. #include <unistd.h>
  4. #include <assert.h>
  5. #include <string.h>
  6.  
  7.  
  8. int main(int argc, char* argv[]) {
  9.     EVP_CIPHER_CTX* context = EVP_CIPHER_CTX_new();
  10.  
  11.     int salt_size = 8;
  12.     unsigned char salt[salt_size];
  13.     fread(salt, sizeof(unsigned char), salt_size, stdin);
  14.     fread(salt, sizeof(unsigned char), salt_size, stdin);
  15.  
  16.     //unsigned char preamble[16];
  17.     //read(STDIN_FILENO, preamble, sizeof(preamble));
  18.     //unsigned char salt[8];
  19.     //memcpy(salt, preamble + 8, sizeof(salt));
  20.  
  21.     printf("INPUT : %s\n", salt);
  22.     fflush(stdout);
  23.  
  24.     unsigned char key[32];
  25.     unsigned char init_value[16];
  26.     EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), salt, argv[1], strlen(argv[1]), 1, key, init_value);
  27.     EVP_DecryptInit(context, EVP_aes_256_cbc(), key, init_value);
  28.  
  29.     int buff_size = EVP_CIPHER_CTX_block_size(context);
  30.     unsigned char buffer[buff_size];
  31.     int readed = fread((void*)buffer, sizeof(unsigned char), buff_size, stdin);
  32.  
  33.     int out_size = EVP_CIPHER_CTX_block_size(context);
  34.     unsigned char out[out_size];
  35.     int written;
  36.  
  37.     while(readed > 0 || written > 0) {
  38.         assert(EVP_DecryptUpdate(context, out, &written, buffer, readed) > 0);
  39.         //printf("%d\n", written);
  40.         fflush(stdout);
  41.         printf("%.*s", written, out);
  42.         //fwrite((void*)out, sizeof(unsigned char), written, stdout);
  43.         fflush(stdout);
  44.         //printf("here");
  45.         readed = fread((void*)buffer, sizeof(unsigned char), buff_size, stdin);
  46.     }
  47.  
  48.     EVP_DecryptFinal(context, out, &written);
  49.  
  50.     printf("%.*s", written, out);
  51.     fflush(stdout);
  52.     EVP_CIPHER_CTX_free(context);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement