Advertisement
Guest User

crypto

a guest
Jul 9th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. unsigned char clave[] = "testkey123456789";
  2. unsigned char iv[] = "ivkey12345678912";
  3. void desencriptar(char *archivo_nombre) {
  4. FILE *archivo = fopen(archivo_nombre, "r");
  5.  
  6. if(!archivo) {
  7. fprintf(_log, "No se pudo abrir el archivo\n");
  8. return;
  9. }
  10.  
  11. fseek(archivo, 0, SEEK_END);
  12. int size = ftell(archivo);
  13. fseek(archivo, 0, SEEK_SET);
  14. unsigned char *buffer = malloc(sizeof(char) * size);
  15. unsigned char *salida = malloc(size*2);
  16. memset(buffer, 0, sizeof(buffer));
  17. fread(buffer, size, 1, archivo);
  18. fclose(archivo);
  19.  
  20. archivo = fopen(archivo_nombre, "w");
  21. if(!archivo){
  22. fprintf(_log, "No se pudo abrir el archivo\n");
  23. return;
  24. }
  25.  
  26. int outLen1 = 0; int outLen2 = 0;
  27. EVP_CIPHER_CTX ctx;
  28. EVP_DecryptInit(&ctx,EVP_aes_128_cbc(),clave,iv);
  29. EVP_DecryptUpdate(&ctx,salida,&outLen1,buffer,size);
  30. EVP_DecryptFinal(&ctx,salida + outLen1,&outLen2);
  31. fwrite(salida,sizeof(char),outLen1 + outLen2,archivo);
  32. fprintf(_log, "Desencriptado, nombre: %s\n", archivo_nombre);
  33.  
  34. fclose(archivo);
  35. free(buffer);
  36. free(salida);
  37. }
  38.  
  39. void encriptar(char *archivo_nombre) {
  40.  
  41. if(!archivo_nombre) return;
  42. FILE *archivo = fopen(archivo_nombre, "r");
  43.  
  44. if(!archivo) {
  45. fprintf(_log, "No se pudo abrir el archivo\n");
  46. MessageBox(NULL, "ds", "s", MB_OK);
  47. return;
  48. }
  49.  
  50. fseek(archivo, 0, SEEK_END);
  51. int size = ftell(archivo);
  52. fseek(archivo, 0, SEEK_SET);
  53. unsigned char *buffer = malloc(sizeof(char) * size);
  54. unsigned char *salida = malloc(size*2);
  55. memset(buffer, 0, sizeof(buffer));
  56. fread(buffer, size, 1, archivo);
  57. fclose(archivo);
  58.  
  59. archivo = fopen(archivo_nombre, "w");
  60. if(!archivo){
  61. fprintf(_log, "No se pudo abrir el archivo\n");
  62. return;
  63. }
  64.  
  65. if(size > 0) {
  66. fprintf(_log, "Encriptando %s...\n", archivo_nombre);
  67.  
  68. int outLen1 = 0; int outLen2 = 0;
  69. EVP_CIPHER_CTX ctx;
  70. EVP_EncryptInit(&ctx,EVP_aes_128_cbc(),clave,iv);
  71. EVP_EncryptUpdate(&ctx,salida,&outLen1,buffer,size);
  72. EVP_EncryptFinal(&ctx,salida + outLen1,&outLen2);
  73. fwrite(salida,sizeof(char),outLen1 + outLen2,archivo);
  74.  
  75. }
  76.  
  77. fclose(archivo);
  78. free(buffer);
  79. free(salida);
  80. char nombrenuevo[1024];
  81. sprintf(nombrenuevo, "%s.encrypted", archivo_nombre);
  82. rename(archivo_nombre, nombrenuevo);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement