Advertisement
Guest User

openssl API Usage

a guest
Dec 29th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <openssl/conf.h>
  5. #include <openssl/evp.h>
  6. #include <openssl/err.h>
  7.  
  8. void handleErrors(void)
  9. {
  10. ERR_print_errors_fp(stderr);
  11. abort();
  12. }
  13.  
  14. int decryptFile(const char *inputFile, const char *outputFile, const char *passphrase)
  15. {
  16. OpenSSL_add_all_algorithms();
  17. ERR_load_crypto_strings();
  18.  
  19. FILE *inFile = fopen(inputFile, "rb");
  20. if (!inFile) {
  21. perror("Error opening input file");
  22. return 1;
  23. }
  24.  
  25. FILE *outFile = fopen(outputFile, "wb");
  26. if (!outFile) {
  27. perror("Error opening output file");
  28. fclose(inFile);
  29. return 1;
  30. }
  31.  
  32. const EVP_CIPHER *cipher = EVP_aes_256_cbc();
  33. const EVP_MD *digest = EVP_sha256();
  34.  
  35. unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
  36. unsigned char salt[8] = {0};
  37. fread(salt, 1, 8, inFile); // skip Salted__
  38. fread(salt, 1, 8, inFile); // store next 8 bytes
  39. if (!EVP_BytesToKey(cipher, digest, salt, (const unsigned char *)passphrase,
  40. strlen(passphrase), 1, key, iv)) {
  41. #if 0
  42. if (!EVP_BytesToKey(cipher, digest, NULL, (const unsigned char *)passphrase, strlen(passphrase), 1, key, iv)) {
  43. #endif
  44. perror("Error deriving key and IV");
  45. fclose(inFile);
  46. fclose(outFile);
  47. return 1;
  48. }
  49.  
  50. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  51. if (!ctx) {
  52. perror("Error creating context");
  53. fclose(inFile);
  54. fclose(outFile);
  55. return 1;
  56. }
  57.  
  58. if (1 != EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv)) {
  59. perror("Error initializing decryption");
  60. EVP_CIPHER_CTX_free(ctx);
  61. fclose(inFile);
  62. fclose(outFile);
  63. return 1;
  64. }
  65. EVP_CIPHER_CTX_set_padding(ctx, 0);
  66.  
  67. unsigned char inBuf[1024], outBuf[1024];
  68. int bytesRead, decryptedLen;
  69.  
  70. while ((bytesRead = fread(inBuf, 1, sizeof(inBuf), inFile)) > 0) {
  71. if (1 != EVP_DecryptUpdate(ctx, outBuf, &decryptedLen, inBuf, bytesRead)) {
  72. perror("Error updating decryption");
  73. EVP_CIPHER_CTX_free(ctx);
  74. fclose(inFile);
  75. fclose(outFile);
  76. return 1;
  77. }
  78.  
  79. fwrite(outBuf, 1, decryptedLen, outFile);
  80. }
  81.  
  82. if (1 != EVP_DecryptFinal_ex(ctx, outBuf, &decryptedLen)) {
  83. ERR_print_errors_fp(stderr);
  84. perror("Error finalizing decryption");
  85. EVP_CIPHER_CTX_free(ctx);
  86. fclose(inFile);
  87. fclose(outFile);
  88. return 1;
  89. }
  90.  
  91. fwrite(outBuf, 1, decryptedLen, outFile);
  92.  
  93. EVP_CIPHER_CTX_free(ctx);
  94. fclose(inFile);
  95. fclose(outFile);
  96.  
  97. return 0;
  98. }
  99.  
  100. int main()
  101. {
  102. const char *inputFile = "encrypted_file.enc";
  103. const char *outputFile = "decrypted_file.txt";
  104. const char *passphrase = "111111";
  105.  
  106. if (decryptFile(inputFile, outputFile, passphrase) == 0) {
  107. printf("File decrypted successfully.\n");
  108. } else {
  109. fprintf(stderr, "Error decrypting file.\n");
  110. }
  111.  
  112. ERR_free_strings();
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement