I'm using AES encryption. The encrypted text is then saved into a binary file. And elsewhere, that file is opened and the text decrypted. Pretty standard procedure..
However, when deserializing the text from the binary file, I can't get it properly decrypted. Here's the code:
ciphertext is the result of an AES encryption, and when I do the encrypt/decrypt process on the same class to test it, works perfectly. But when I encrypt/decrypt on different programs passing the encrypted value with a binary file, the value gets corrupted.
strlen returns the length of a null terminated string. If Encrypt() doesn't return such string (which is very likely I think), then strlen won't work as expected.
Determine the length of the string some other way.
I've rewritten the code in a shorter project in an effort of seeking the problem more efficiently. I'm not really sure now if the problem is within the binary file though..
First I encrypt a text into ciphertext1
I serialize the value within a file
I deserialize that same value into ciphertext.
After the deserialization, ciphertext has a length of 144
ciphertext1 and ciphertext are unsigned char
The problem is that lengthLicenseFile and ciphertext1 have the same length, 131. However, ciphertext has a length of 144, with this symbols áýýýý««««««««þ.
Same question as before. Does strlen(ciphertext1) return 144 or does it encounter a nul character in the encrypted text and return a value you weren't expecting? (Say around character 131?)
Why are you using reinterpret_cast for a static_cast job?
Alright. I misread your original post. I thought you were losing characters, not gaining them.
This is what's happening: Your orginal data is delimited by a '\0' and doesn't have any of those in the 'body' of the string. However, strlen returns the length of the string without the final '\0', so when you write it, you don't write the nul terminating character.
Then when you read it in, you must try to use strlen (or some other function that depends on the string being nul-terminated) which results in you accessing memory you don't own, because the terminating nul character is not present.
The simplest fix would be to change line 7 in the last code you posted to specify the length as strlen(...)+1