I use RSA (1024) to encrypt / decrypt strings. Encryption is performed using the public key and is written in C #, and decryption is performed using c. I am encrypting the string:
86afaecb-c211-4d55-8e90-2b715d6d64b9
and write the encrypted data to a file. Then I use openssl api to read the encrypted data from the file and decrypt it. However, I got the output as:
86afaecb-c211-4d55-8e90-2b715d6d64b9oeheBjQ8fo1AmDnor1D3BLuPyq9wJBAOV + M / WVNYzYr PJBKoskOj + 4LaNpT + SpkfK81nsnQEbHbjgao4eHNU +
There seems to be some firmware at the end of the original line. Why is this happening? And how to solve the problem?
The following is a snippet of code:
// Encrypt { string plainData = "86afaecb-c211-4d55-8e90-2b715d6d64b9"; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(paraPub); byte[] testData = Encoding.UTF8.GetBytes(plainData); byte[] encryptedData = rsa.Encrypt(testData, true); FileStream pFileStream = null; string fileName = "encryptedData.dat"; pFileStream = new FileStream(fileName, FileMode.OpenOrCreate); pFileStream.Write(encryptedData, 0, encryptedData.Length); ... } // Decrypt { char *encrypt = malloc(RSA_size(privateKey)); FILE *out = fopen("encryptedData.dat", "r"); int encrypt_len = fread(encrypt, sizeof(*encrypt), RSA_size(privateKey), out); fclose(out); decrypt = malloc(encrypt_len); if(RSA_private_decrypt(encrypt_len, (unsigned char*)encrypt, (unsigned char*)decrypt, privateKey, RSA_PKCS1_OAEP_PADDING) == -1) { // error handle } printf("Decrypted message: %s\n", decrypt); }
source share