When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
29 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
Did you mean Error: Unable to decrypt input data.?
yes
while reading
// Read the input data and decrypt it
while (input.read(buffer, 1024)) {
if (EVP_DecryptUpdate(ctx, reinterpret_cast<unsigned char*>(buffer), &len, reinterpret_cast<unsigned char*>(buffer), 1024) != 1) {
std::cerr << "Error: Unable to decrypt input data." << std::endl;
input.close();
output.close();
EVP_CIPHER_CTX_free(ctx);
return false;
}
output.write(buffer, len);
}
I found this comment:
I can reproduce the issue. However, the ciphertext does not look like it is valid. During decryption, an integrity check is performed, which generally fails if the ciphertext is not valid. If I use a valid ciphertext, the code works as expected.
So I would check if the output and input are actually the same.
i see, thanks, any suggestion to solve that in my code?
To narrow it down I would decrypt the same buffer you just encrypted. If that fails too then there is an issue with the parameters somewhere, otherwise it's a problem with file writing or reading.
i will give it a try, thanks
Then you either end up with very simple code that encrypts and decrypts a buffer and fails which should make it easier to spot the error. Maybe ask again with said simpler example.
The file reading/writing you can probably figure out.
i used small file as i test will give it a try to decode the buffer and write it back
You can also use a website like https://encode-decode.com/aes-128-cbc-encrypt-online/ where you can compare the generated file with what the website produces, then you know if the encryption or decryption part are misbehaving.
Not sure if copy/pasting works with binary data though.
doubt it does , thanks i will give it a try now
Maybe you can find one that uses file uploads instead.
I think the file lengths should be the same, you can check that.
its same size
Then it's probably just the editor displaying it strangely.
Text editors don't like binary data much either.
could be buffer size issue or not related ? in my previous code , i used std::String , length for buffer size?
well seems ai found the issue, thanks
EVP_DecryptUpdate function modifies the buffer in place, so you can't simply pass the same buffer to both the input and output arguments. Instead, you should use a separate buffer for the output data.
Here is the corrected version of the decryption function:
!solve
Thank you and let us know if you have any more questions!
Outdone by AI. Oh well.