Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- while((bytes = fread (buffer, 1, 32, fp)) != 0)
- {
- EVP_DecryptUpdate(e, buffer_out, &c_len, buffer, bytes);
- ret = fwrite(buffer_out, 1, c_len, fpout);
- printf("c_len %d bytes %d ret %dn", c_len, bytes, ret);
- }
- char *decrypt (char *key,
- char *iv,
- char *encryptedData,
- int encryptedLength)
- {
- // Initialisation
- EVP_CIPHER_CTX *cryptCtx = EVP_CIPHER_CTX_new();
- EVP_CIPHER_CTX_init(cryptCtx);
- int decryptedLength = 0;
- int allocateSize = encryptedLength * sizeof(char);
- int lastDecryptLength = 0;
- char *decryptedData = (char *) malloc (allocateSize);
- memset(decryptedData, 0x00, allocateSize);
- int decryptResult = EVP_DecryptInit_ex(cryptCtx,
- EVP_bf_cbc(), NULL, key, iv);
- // EVP_DecryptInit_ex returns 1 if it succeeded.
- if (decryptResult == 1)
- {
- decryptResult = EVP_DecryptUpdate(cryptCtx, decryptedData,
- &decryptedLength, encryptedData, encryptedLength);
- // Note that EVP_DecryptUpdate will alter the value of the third parameter
- // to be equal to the amount of data that was written. This is not always the
- // entire length of the decrypted data! To finish the decryption process, use
- // EVP_DecryptFinal_ex. This will decrypt any remaining data.
- // Cleanup
- if (decryptResult == 1)
- {
- // Stick the final data at the end of the last
- // decrypted data.
- EVP_DecryptFinal_ex(cryptCtx,
- decryptedData + decryptedLength,
- &lastDecryptLength);
- decryptedLength = decryptedLength + lastDecryptLength;
- decryptedData[decryptedLength – 1] = ”;
- printf ("Decrypted size: %dn", decryptedLength);
- printf ("Decrypted data: n%snn", decryptedData);
- }
- else
- {
- printf ("EVP_DeccryptUpdate failure.n");
- }
- }
- else
- {
- printf ("EVP_DecryptInit_ex failure.n");
- }
- EVP_CIPHER_CTX_free(cryptCtx);
- EVP_cleanup();
- return decryptedData;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement