0

In this code:

int encrypted_length = RSA_public_encrypt (lenPlainText, plainText, encrypted, rsa, padding);
int encrypted_length2 = strlen((char *) encrypted);

Using same plainText Sometimes (only Sometimes) this two lengthes doesnt match;

Both should always be 256.

Always the first (encrypted length) shows 256. Both the second sometimes shows smaller quantities.

And when that happens the key appears truncated if I try to show it on the output.

I imagine the unsigned char "encrypted" sometimes comes with zeros (null) and so it's truncating.

This can happen?

jww
  • 97,681
  • 90
  • 411
  • 885
Neumann
  • 319
  • 2
  • 14
  • There is also [EVP Symmetric Encryption and Decryption | C++ Programs](https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption#C.2B.2B_Programs) on the OpenSSL wiki. Its a different interface, but roughly the same question. – jww Dec 28 '17 at 21:23
  • Your string has embedded NULLs, so `strlen` does not work as expected. You need to use a pointer and explicit length. Also see [Null character in encrypted data](https://stackoverflow.com/q/22969876/608639), [Encrypted data as a string](https://stackoverflow.com/q/21530922/608639) (OpenSSL), [AES padding and writing the ciphertext to a disk file](https://stackoverflow.com/q/27385456/608639) (.Net), [Having trouble decrypting a well-formed cipher text using Crypto++](https://stackoverflow.com/q/34167305/608639) (Qt and Crypto++), etc. – jww Dec 28 '17 at 23:40
  • Thank you jww. I read the posts you pointed out. I understand that I need to use memcpy of ciphertext, but after, functions that use char as: b64_encode (encrypted, base64encrypted) will not have this same problem with the value created by memcopy? what kind of variable should I use as memcpy target ?? – Neumann Dec 29 '17 at 12:20

0 Answers0