Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. while((bytes = fread (buffer, 1, 32, fp)) != 0)
  2. {
  3. EVP_DecryptUpdate(e, buffer_out, &c_len, buffer, bytes);
  4. ret = fwrite(buffer_out, 1, c_len, fpout);
  5. printf("c_len %d bytes %d ret %dn", c_len, bytes, ret);
  6. }
  7.  
  8. char *decrypt (char *key,
  9. char *iv,
  10. char *encryptedData,
  11. int encryptedLength)
  12. {
  13. // Initialisation
  14. EVP_CIPHER_CTX *cryptCtx = EVP_CIPHER_CTX_new();
  15. EVP_CIPHER_CTX_init(cryptCtx);
  16. int decryptedLength = 0;
  17. int allocateSize = encryptedLength * sizeof(char);
  18. int lastDecryptLength = 0;
  19. char *decryptedData = (char *) malloc (allocateSize);
  20. memset(decryptedData, 0x00, allocateSize);
  21. int decryptResult = EVP_DecryptInit_ex(cryptCtx,
  22. EVP_bf_cbc(), NULL, key, iv);
  23.  
  24. // EVP_DecryptInit_ex returns 1 if it succeeded.
  25. if (decryptResult == 1)
  26. {
  27. decryptResult = EVP_DecryptUpdate(cryptCtx, decryptedData,
  28. &decryptedLength, encryptedData, encryptedLength);
  29.  
  30. // Note that EVP_DecryptUpdate will alter the value of the third parameter
  31. // to be equal to the amount of data that was written. This is not always the
  32. // entire length of the decrypted data! To finish the decryption process, use
  33. // EVP_DecryptFinal_ex. This will decrypt any remaining data.
  34.  
  35. // Cleanup
  36. if (decryptResult == 1)
  37. {
  38. // Stick the final data at the end of the last
  39. // decrypted data.
  40. EVP_DecryptFinal_ex(cryptCtx,
  41. decryptedData + decryptedLength,
  42. &lastDecryptLength);
  43. decryptedLength = decryptedLength + lastDecryptLength;
  44. decryptedData[decryptedLength – 1] = ”;
  45. printf ("Decrypted size: %dn", decryptedLength);
  46. printf ("Decrypted data: n%snn", decryptedData);
  47. }
  48. else
  49. {
  50. printf ("EVP_DeccryptUpdate failure.n");
  51. }
  52. }
  53. else
  54. {
  55. printf ("EVP_DecryptInit_ex failure.n");
  56. }
  57. EVP_CIPHER_CTX_free(cryptCtx);
  58. EVP_cleanup();
  59. return decryptedData;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement