Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- http://stackoverflow.com/questions/12153009/openssl-c-example-of-aes-gcm-using-evp-interfaces
- http://stackoverflow.com/questions/23391125/how-to-convert-a-hexbytes-array-to-a-string-in-c-c-on-arduino
- ***********
- **DRIPHER**
- ***********
- TODO:
- 1-ic e ckey ja gerados agr é usar a informacao
- 2-perceber pq na esta a decifrar ou se esta a cifrar mal
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <openssl/evp.h>
- #include <openssl/aes.h>
- #include <openssl/sha.h>
- #include <openssl/rand.h>
- #include <openssl/md5.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- void gencKey( unsigned char *ckey ); //gerar ckey atraves de /dev/urandom ou /dev/random
- void genIv( unsigned char *ivec ); // gerar iv atraves do /dev/urandom ou /dev/random
- void encrypt( FILE *ifp, FILE *ofp, unsigned char * ckey, unsigned char * ivec ); // using aes_cbc_256
- void decrypt( FILE *ifp, FILE *ofp, unsigned char * ckey, unsigned char * ivec ); // using aes_cbc_256
- void calc_sha256( char file_name[] ); //file ou path?
- void calc_md5( char file_name[] );
- int main( int argc, char const *argv[] ){
- FILE *fIn, *fOut, *fOut2;
- fIn = fopen("t.txt", "rb"); // rb ou r+?
- fOut = fopen("resultado.txt", "wb" );
- fOut2 = fopen("decifrado.txt", "w+"); //wb ou w+?
- const int bufSize = 32;
- const int ivSize = 16;
- unsigned char * ivec= malloc(ivSize); // para 128 bits [16]
- unsigned char * ckey = malloc(bufSize); // assim ou ckey[32]?
- //ckey = "thiskeyisverybad";
- encrypt (fIn, fOut, ckey, ivec);
- decrypt (fOut, fOut2, ckey, ivec);
- calc_md5 ("t.txt");
- calc_sha256("t.txt");
- fclose(fIn);
- fclose(fOut);
- fclose(fOut2);
- }
- void gencKey( unsigned char *ckey ){
- int randomDataF = open("/dev/urandom", O_RDONLY);
- int verifyRead=0;
- if( randomDataF < 0){
- printf("Error gencKey, openning file negative file descriptor: %d\nExit now\n", randomDataF);
- return ;
- }
- verifyRead = read(randomDataF, ckey, 32);
- if(verifyRead<0){
- printf("Error reading from file gencKey\nExit now\n");
- return ;
- }
- }
- void genIv( unsigned char *ivec ){
- int randomDataF = open ("/dev/urandom", O_RDONLY);
- int verify=0;
- char c[16];
- if(randomDataF < 0){
- printf("error genIv, bad file descriptor:%d\nExit now\n", randomDataF);
- return;
- }
- read(randomDataF, ivec, 16);
- if(verify<0){
- printf("error genIv, cant read\nExit now\n");
- return;
- }
- strcpy(c, ivec);
- //int teste=0;
- // teste=strcmp(c, ivec);
- //printf("%d\n", teste);
- /*int i=0;
- for(i = 0; i < strlen(c); i++) printf("%02x", c[i]);
- printf("\nIV- %s \n", ivec);
- */
- }
- void encrypt(FILE *ifp, FILE *ofp, unsigned char * ckey, unsigned char * ivec)
- {
- fseek(ifp, 0L, SEEK_END);
- int fsize = ftell(ifp);
- fseek(ifp, 0L, SEEK_SET);
- int outLen1 = 0;
- int outLen2 = 0;
- unsigned char *in = malloc(fsize);
- unsigned char *out = malloc(fsize*2); //tem de ser pelo menos 1 bloco maior q o in dai o *2
- fread(in,sizeof(char),fsize, ifp);//Read Entire File
- EVP_CIPHER_CTX ctx;
- EVP_EncryptInit(&ctx,EVP_aes_256_cbc(),ckey,ivec);
- EVP_EncryptUpdate(&ctx,out,&outLen1,in,fsize);
- EVP_EncryptFinal(&ctx,out + outLen1,&outLen2);
- fwrite(out,sizeof(char),outLen1 + outLen2,ofp);
- EVP_CIPHER_CTX_cleanup(&ctx); //usar ou n usar?
- }
- void decrypt(FILE *ifp, FILE *ofp, unsigned char * ckey, unsigned char * ivec)
- {
- fseek(ifp, 0L, SEEK_END); /* f size */
- int fsize = ftell(ifp);
- fseek(ifp, 0L, SEEK_SET); // back to normal
- printf("CKEY - %s\n", ckey);
- int outLen1 = 0; int outLen2 = 0;
- unsigned char *in = malloc(fsize);
- //printf("OLA\n");
- unsigned char *out = malloc(fsize);
- //Read File
- fread(in,sizeof(char),fsize, ifp);//Read Entire File
- //setup decryption
- EVP_CIPHER_CTX ctx;
- EVP_DecryptInit(&ctx,EVP_aes_256_cbc(),ckey,ivec);
- EVP_DecryptUpdate(&ctx,out,&outLen1,in,fsize);
- EVP_DecryptFinal(&ctx,out + outLen1,&outLen2);
- fwrite(out,sizeof(char),outLen1+outLen2,ofp);
- }
- void calc_sha256 (char file_name[]){ //acabar de confirmar
- FILE* file = fopen(file_name, "rb");
- int bytesRead = 0;
- const int bufSize = 32768;
- char* buffer = malloc(bufSize);
- unsigned char hash[SHA256_DIGEST_LENGTH];
- SHA256_CTX sha256;
- SHA256_Init(&sha256);
- while((bytesRead = fread(buffer, 1, bufSize, file))){
- SHA256_Update(&sha256, buffer, bytesRead);
- }
- SHA256_Final(hash, &sha256);
- /* parte teste */
- char teste[65];
- int n=0;
- // printf("0x");
- for (n = 0; n < SHA256_DIGEST_LENGTH; n++)
- //printf("%02x", hash[n]);
- sprintf(teste + (n * 2) ,"%02x",(unsigned char)hash[n]);
- putchar('\n');
- printf("%s\n", teste);
- }
- void calc_md5 (char file_name[]){
- FILE *f = fopen (file_name, "rb");
- int bytesRead=0;
- MD5_CTX ctx;
- unsigned char c [MD5_DIGEST_LENGTH];
- unsigned char data[1024];
- if(f==NULL){
- printf("error open file calc_MD5, leaving now ...\n");
- return;}
- MD5_Init (&ctx);
- while ((bytesRead = fread (data, 1, 1024, f)) != 0)
- MD5_Update (&ctx, data, bytesRead);
- MD5_Final (c,&ctx);
- int i;
- /* parte teste */
- for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
- printf (" %s\n", file_name);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement