#c++ openssl encrypt /decrypt

29 messages · Page 1 of 1 (latest)

leaden basaltBOT
#

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.

elfin arch
#

Did you mean Error: Unable to decrypt input data.?

last pewter
#

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);
    }
elfin arch
#

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.

last pewter
#

i see, thanks, any suggestion to solve that in my code?

elfin arch
#

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.

last pewter
#

i will give it a try, thanks

elfin arch
#

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.

last pewter
#

i used small file as i test will give it a try to decode the buffer and write it back

elfin arch
#

Not sure if copy/pasting works with binary data though.

last pewter
#

doubt it does , thanks i will give it a try now

elfin arch
#

Maybe you can find one that uses file uploads instead.

last pewter
#

i guess it fail in encrypt

#

as this the encrypted data

#

as the file like this:

elfin arch
#

I think the file lengths should be the same, you can check that.

last pewter
#

its same size

elfin arch
#

Then it's probably just the editor displaying it strangely.

#

Text editors don't like binary data much either.

last pewter
#

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

leaden basaltBOT
#

Thank you and let us know if you have any more questions!

elfin arch
#

Outdone by AI. Oh well.