SHARE
TWEET

Untitled

a guest Oct 20th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <openssl/conf.h>
  4. #include <openssl/evp.h>
  5. #include <openssl/err.h>
  6.  
  7.  
  8. int decriptareECB(unsigned char *textCriptat, int lungimeaTextuluiCriptat
  9. , unsigned char *cheie,
  10.            unsigned char *textSimplu)
  11. {
  12.     EVP_CIPHER_CTX *context;
  13.  
  14.     int lungime;
  15.  
  16.     int lungimeaTextuluiSimplu;
  17.  
  18.     /* Create and initialise the context */
  19.     context = EVP_CIPHER_CTX_new();
  20.  
  21.     EVP_DecryptInit_ex(context, EVP_aes_128_ecb(), NULL, cheie, NULL);
  22.  
  23.     EVP_DecryptUpdate(context, textSimplu, &lungime, textCriptat, lungimeaTextuluiCriptat);
  24.     lungimeaTextuluiSimplu = lungime;
  25.  
  26.     EVP_DecryptFinal_ex(context, textSimplu + lungime, &lungime);
  27.  
  28.     lungimeaTextuluiSimplu += lungime;
  29.  
  30.  
  31.     EVP_CIPHER_CTX_free(context);
  32.  
  33.     return lungimeaTextuluiSimplu;
  34. }
  35.  
  36. int decriptareCBC(unsigned char *textCriptat, int lungimeaTextuluiCriptat, unsigned char *cheie,
  37.            unsigned char *iv, unsigned char *textSimplu)
  38. {
  39.     EVP_CIPHER_CTX *context;
  40.  
  41.     int lungime;
  42.  
  43.     int lungimeaTextuluiSimplu;
  44.  
  45.     /* Create and initialise the context */
  46.     context = EVP_CIPHER_CTX_new();
  47.  
  48.     EVP_DecryptInit_ex(context, EVP_aes_128_cbc(), NULL, cheie, iv);
  49.  
  50.     EVP_DecryptUpdate(context, textSimplu, &lungime, textCriptat, lungimeaTextuluiCriptat);
  51.  
  52.     lungimeaTextuluiSimplu = lungime;
  53.  
  54.     EVP_DecryptFinal_ex(context, textSimplu + lungime, &lungime);
  55.  
  56.     lungimeaTextuluiSimplu += lungime;
  57.  
  58.  
  59.     EVP_CIPHER_CTX_free(context);
  60.  
  61.     return lungimeaTextuluiSimplu;
  62. }
  63.  
  64. int main( int argc, char *argv[] )
  65. {
  66.     if(argc !=4)
  67.     {
  68.         printf("%s", "Numar invalid de argumente");
  69.         exit(0);
  70.     }
  71.  
  72.     unsigned char *cheie;
  73.     unsigned char *iv = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
  74.  
  75.     unsigned char decryptedtext[500];
  76.  
  77.     int lungimeaTextuluiDecriptat, lungimeaTextuluiCriptat;
  78.  
  79.  
  80.     FILE *fC, *fR, *fD;
  81.     // FILE *fR;
  82.     // FILE *fD;
  83.     size_t len = 0;
  84.     ssize_t read;
  85.     char line[128];
  86.     int flen;
  87.     int flen2;
  88.     unsigned char *buff = NULL;
  89.     unsigned char *buff2 = NULL;
  90.     fR = fopen(argv[1], "r");
  91.     fC = fopen(argv[2], "r");
  92.  
  93.     fD = fopen("dictionar.txt", "r");
  94.  
  95.  
  96.     fseek (fC, 0, SEEK_END);
  97.     flen = ftell (fC);
  98.     rewind (fC);
  99.  
  100.     fseek (fR, 0, SEEK_END);
  101.     flen2 = ftell (fR);
  102.     rewind (fR);
  103.  
  104.     buff = (unsigned char *)malloc(flen2+4);
  105.     fread (buff, sizeof(char), flen, fC);
  106.  
  107.     buff2 = (unsigned char *)malloc(flen2+4);
  108.     fread (buff2, sizeof(char), flen2, fR);
  109.  
  110.  
  111.  
  112.  
  113.  
  114.     while (fgets(line, sizeof(line) - 4, fD))
  115.     {
  116.  
  117.         line[strlen(line) - 1] = '\0';
  118.         int i;
  119.        
  120.  
  121.         for (i = strlen(line) + 1; i <= 30; i++)
  122.             strcat(line, " ");
  123.         cheie = line;
  124.         if (strcmp(argv[3], "ECB") ==0)
  125.             {
  126.                 lungimeaTextuluiDecriptat = decriptareECB(buff, flen, cheie, decryptedtext);
  127.                 decryptedtext[lungimeaTextuluiDecriptat] = '\0';
  128.             }
  129.         else if (strcmp(argv[3], "CBC") ==0)
  130.             {  
  131.                 lungimeaTextuluiDecriptat = decriptareCBC(buff, flen, cheie, iv, decryptedtext);
  132.                 decryptedtext[lungimeaTextuluiDecriptat] = '\0';
  133.                
  134.             }
  135.         else
  136.         {
  137.             printf("%s", "Invalid third argument");
  138.             break;
  139.         }
  140.  
  141.        
  142.         printf("%s", decryptedtext);
  143.  
  144.         if (strcmp(buff2, decryptedtext) == 0)
  145.         {
  146.             printf("%s", cheie);
  147.            
  148.             break;
  149.         }
  150.  
  151.  
  152.     }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.     return 0;
  159. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top