I’ve encounter this issue in RSA decryption (RSA_private_decrypt)

1
2
int result_length = RSA_private_decrypt(64, (unsigned char*)crypt_chunk, (unsigned char *)result_chunk, rsa_privateKey, RSA_PKCS1_PADDING);
printf("Result chunk: %s\nChunk length: %d\n", result_chunk, result_length);

Output

1
2
Result chunk: 33-9998-123-123408101123451250-PARADM01_00023054-CY00\240Z
Chunk length: 53

But what I want is 33-9998-123-123408101123451250-PARADM01_00023054-CY00

I googled for some time still can’t get the result. By try-and-error method, I solved this by adding the following code

1
2
3
4
char tmp_result[result_length + 1];
strcpy(tmp_result, result_chunk);
tmp_result[result_length] = '\0';
printf("New chunk: %s\n", tmp_result);

Output

1
New chunk: 33-9998-123-123408101123451250-PARADM01_00023054-CY00
  1. Declaring a new variable with additional 1 more character of the original result
  2. Copy the original result to the new variable
  3. Inject a null character to the end of the new variable

Hope this helps. :)