Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.39 KB | None | 0 0
  1. /*
  2. http://stackoverflow.com/questions/12153009/openssl-c-example-of-aes-gcm-using-evp-interfaces
  3. http://stackoverflow.com/questions/23391125/how-to-convert-a-hexbytes-array-to-a-string-in-c-c-on-arduino
  4.  
  5.                   ***********
  6.                   **DRIPHER**
  7.                   ***********
  8.  
  9. TODO:
  10. 1-ic e ckey ja gerados agr é usar a informacao
  11. 2-perceber pq na esta a decifrar ou se esta a cifrar mal
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <dirent.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/aes.h>
  20. #include <openssl/sha.h>
  21. #include <openssl/rand.h>
  22. #include <openssl/md5.h>
  23. #include <fcntl.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27.  
  28.  
  29. void gencKey( unsigned char *ckey ); //gerar ckey atraves de /dev/urandom ou /dev/random
  30. void genIv( unsigned char *ivec );  // gerar iv atraves do /dev/urandom ou /dev/random
  31.  
  32. void encrypt( FILE *ifp, FILE *ofp, unsigned char * ckey, unsigned char * ivec ); // using aes_cbc_256
  33. void decrypt( FILE *ifp, FILE *ofp, unsigned char * ckey, unsigned char * ivec ); // using aes_cbc_256
  34. void calc_sha256( char file_name[] ); //file ou path?
  35. void calc_md5( char file_name[] );
  36.  
  37.  
  38. int main( int argc, char const *argv[] ){
  39.  
  40. FILE *fIn, *fOut, *fOut2;
  41.  
  42.   fIn = fopen("t.txt", "rb"); // rb ou  r+?
  43.   fOut = fopen("resultado.txt", "wb" );
  44.   fOut2 = fopen("decifrado.txt", "w+"); //wb ou w+?
  45.  
  46.   const    int    bufSize = 32;
  47.   const    int    ivSize  = 16;
  48.   unsigned char * ivec= malloc(ivSize); // para 128 bits [16]
  49.   unsigned char * ckey = malloc(bufSize); // assim ou ckey[32]?
  50.  
  51.   //ckey = "thiskeyisverybad";
  52.  
  53.  
  54.   encrypt (fIn, fOut, ckey, ivec);
  55.   decrypt (fOut, fOut2, ckey, ivec);
  56.   calc_md5 ("t.txt");
  57.   calc_sha256("t.txt");
  58.  
  59. fclose(fIn);
  60. fclose(fOut);
  61. fclose(fOut2);
  62. }
  63.  
  64. void gencKey( unsigned char *ckey ){
  65.  
  66.   int randomDataF = open("/dev/urandom", O_RDONLY);
  67.   int verifyRead=0;
  68.  
  69.   if( randomDataF < 0){
  70.     printf("Error gencKey, openning file negative file descriptor: %d\nExit now\n", randomDataF);
  71.     return ;
  72.   }
  73.  
  74.  verifyRead = read(randomDataF, ckey, 32);
  75.  
  76.   if(verifyRead<0){
  77.     printf("Error reading from file gencKey\nExit now\n");
  78.     return ;
  79.   }
  80.  
  81. }
  82.  
  83. void genIv( unsigned char *ivec ){
  84.  
  85.   int randomDataF = open ("/dev/urandom", O_RDONLY);
  86.   int verify=0;
  87.   char c[16];
  88.  
  89.   if(randomDataF < 0){
  90.     printf("error genIv, bad file descriptor:%d\nExit now\n", randomDataF);
  91.    return;
  92.   }
  93.  
  94.   read(randomDataF, ivec, 16);
  95.  
  96.   if(verify<0){
  97.     printf("error genIv, cant read\nExit now\n");
  98.     return;
  99.   }
  100.  
  101.   strcpy(c, ivec);
  102. //int teste=0;
  103.  // teste=strcmp(c, ivec);
  104.  //printf("%d\n", teste);
  105. /*int i=0;
  106.   for(i = 0; i < strlen(c); i++) printf("%02x", c[i]);
  107.   printf("\nIV- %s \n", ivec);
  108. */
  109. }
  110.  
  111. void encrypt(FILE *ifp, FILE *ofp, unsigned char * ckey, unsigned char * ivec)
  112. {
  113.   fseek(ifp, 0L, SEEK_END);
  114.   int fsize = ftell(ifp);
  115.   fseek(ifp, 0L, SEEK_SET);
  116.  
  117.   int outLen1 = 0;
  118.   int outLen2 = 0;
  119.   unsigned char *in = malloc(fsize);
  120.   unsigned char *out = malloc(fsize*2); //tem de ser pelo menos 1 bloco maior q o in dai o *2
  121.  
  122.   fread(in,sizeof(char),fsize, ifp);//Read Entire File
  123.  
  124.   EVP_CIPHER_CTX ctx;
  125.   EVP_EncryptInit(&ctx,EVP_aes_256_cbc(),ckey,ivec);
  126.   EVP_EncryptUpdate(&ctx,out,&outLen1,in,fsize);
  127.   EVP_EncryptFinal(&ctx,out + outLen1,&outLen2);
  128.   fwrite(out,sizeof(char),outLen1 + outLen2,ofp);
  129.  
  130.   EVP_CIPHER_CTX_cleanup(&ctx);  //usar ou n usar?
  131. }
  132.  
  133.  
  134. void decrypt(FILE *ifp, FILE *ofp, unsigned char * ckey, unsigned char * ivec)
  135. {
  136.    
  137.     fseek(ifp, 0L, SEEK_END); /* f size  */
  138.     int fsize = ftell(ifp);  
  139.    
  140.     fseek(ifp, 0L, SEEK_SET); // back to normal
  141.     printf("CKEY - %s\n", ckey);
  142.  
  143.     int outLen1 = 0; int outLen2 = 0;
  144.     unsigned char *in = malloc(fsize);
  145.     //printf("OLA\n");
  146.     unsigned char *out = malloc(fsize);
  147.  
  148.     //Read File
  149.     fread(in,sizeof(char),fsize, ifp);//Read Entire File
  150.  
  151.     //setup decryption
  152.     EVP_CIPHER_CTX ctx;
  153.     EVP_DecryptInit(&ctx,EVP_aes_256_cbc(),ckey,ivec);
  154.     EVP_DecryptUpdate(&ctx,out,&outLen1,in,fsize);
  155.     EVP_DecryptFinal(&ctx,out + outLen1,&outLen2);
  156.     fwrite(out,sizeof(char),outLen1+outLen2,ofp);
  157. }
  158.  
  159.  
  160. void calc_sha256 (char file_name[]){ //acabar de confirmar
  161.  
  162.   FILE* file = fopen(file_name, "rb");
  163.  
  164.   int bytesRead = 0;
  165.   const int bufSize = 32768;
  166.   char* buffer = malloc(bufSize);
  167.   unsigned char hash[SHA256_DIGEST_LENGTH];
  168.   SHA256_CTX sha256;
  169.  
  170.  
  171.   SHA256_Init(&sha256);
  172.  
  173.   while((bytesRead = fread(buffer, 1, bufSize, file))){
  174.     SHA256_Update(&sha256, buffer, bytesRead);
  175.   }
  176.  
  177.  
  178.   SHA256_Final(hash, &sha256);
  179.  
  180. /* parte teste */
  181.   char teste[65];
  182.   int n=0;
  183. //  printf("0x");
  184.   for (n = 0; n < SHA256_DIGEST_LENGTH; n++)
  185.            //printf("%02x", hash[n]);
  186.     sprintf(teste + (n * 2) ,"%02x",(unsigned char)hash[n]);
  187.   putchar('\n');
  188.   printf("%s\n", teste);   
  189.  
  190. }
  191.  
  192. void calc_md5 (char file_name[]){
  193.  
  194.   FILE *f = fopen (file_name, "rb");
  195.  
  196.   int bytesRead=0;
  197.   MD5_CTX ctx;
  198.   unsigned char c [MD5_DIGEST_LENGTH];
  199.   unsigned char data[1024];
  200.  
  201.   if(f==NULL){
  202.     printf("error open file calc_MD5, leaving now ...\n");
  203.     return;}
  204.  
  205.   MD5_Init (&ctx);
  206.  
  207.   while ((bytesRead = fread (data, 1, 1024, f)) != 0)
  208.         MD5_Update (&ctx, data, bytesRead);
  209.     MD5_Final (c,&ctx);
  210.  
  211.   int i;
  212.                      /* parte teste */
  213.   for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
  214.     printf (" %s\n", file_name);
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement