简体   繁体   中英

OpenSSL RSA-2048 unencrypted block is longer than it should be

I am using the OpenSSL library in order to encrypt and decrypt a string. While doing so, I am able to successfully encrypt a string and store it. The problem I am having comes when I try to decrypt this.

The string I am trying to encrypt and decrypt is contained in a const unsigned char Text[8] . The string is 8 characters long. So I encrypt it with no trouble, but when I decrypt the function and try to output the decrypted string into a char array it is longer than it should be. What I mean is that, for example I try to output the decrypted string into a variable such as the following:

char *DecryptedString = (char *)
    GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, strlen(Text));

I decrypt and output to this, and when I printf DecryptedString I get the correct first 8 characters, but along with a trailing 3 characters. So if the original string was "2js84js8" with a strlen() of 8, the output of DecryptedString when printed would be "2js84js8╝Γ1" with a strlen() of 11.

I even tried manually limiting the length of DecryptedString by replacing strlen(Text) with 9 (one extra for null terminator), got the same result.

And the reason I am using GlobalAlloc with GMEM_FIXED | GMEM_ZEROINIT GMEM_FIXED | GMEM_ZEROINIT is because I was told that zeroing the memory might solve my problem, but it did not prevail. And I am using the standard RSA encryption and decryption functions of OpenSSL ( RSA_public_encrypt & RSA_private_decrypt , I more or less followed this guide ). Does anyone know what is causing this issue or what I am doing wrong?

EDIT: Here is an example of the problem I am having, specific code ( :) ). BTW I excluded the includes for the openssl stuff:

int main()

{

char TestString[] = "d7f3h47k"; // 8 long obviously
RSA *RSAKey = RSA_generate_key(2048, 3, NULL, NULL);

char *EncryptedData = (char *)malloc(RSA_size(RSAKey));
int EncryptLength;

if ((EncryptLength = RSA_public_encrypt(strlen(TestString), (unsigned char *)TestString, (unsigned char *)EncryptedData, RSAKey, RSA_PKCS1_OAEP_PADDING)) == -1) {
    printf("Failed encrypt.");
    getchar();

    return 1;
}

char *DecryptedData = (char *)malloc(strlen(TestString));
if (RSA_private_decrypt(EncryptLength, (unsigned char *)EncryptedData, (unsigned char *)DecryptedData, RSAKey, RSA_PKCS1_OAEP_PADDING) == -1) {
    printf("Failed decrypt.");
    getchar();

    return 1;
}

printf("Original data: %s \nLength of original string: %d \n", TestString, strlen(TestString));
printf("Encrypted data: %s \nLength of encrypted data: %d \n", EncryptedData, strlen(EncryptedData));
printf("Decrypted data: %s \nLength of decrypted data: %d \n", DecryptedData, strlen(DecryptedData));
getchar();

return 0;

}

EVERYTHING WORKS FINE NOW. I was able to solve the problem by increasing the size of my memory allocated for the decrypted string to strlen(TestString) + 1, in order to store a null terminator I believe. My guess is that neighboring RAM leaked in when I did not have enough room in the previous allocation size. I hope this helps anyone else having issues with this kind of thing. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM