Hi,
I'm trying to do some basic encryption/decryption tests with the openssl RSA routines.
A message seems
to encrypt ok, but when it decrypts theres some random garbage appended onto the end.
Is this normal,
or is it a bug in my code?
This is what I'm doing:
<encrypt.cpp>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <openssl/rsa.h>
#include <openssl/engine.h>
using namespace std;
int main() {
string message;
char pubfile[128], privfile[128], seed[1024];
unsigned int keylength;
RSA *key;
cout<<"Test encryption program for OpenSSL.\n";
cout<<"Key Generation:\nEnter a keylength (bits):\n";
cin>>keylength;
key = RSA_generate_key(keylength, 65537, NULL, NULL);
cout<<"Enter message:\n";
ws(cin);
getline(cin,message);
cout<<"Your message is: "<<message<<endl<<"Size:
"<<message.length()<<endl<<"Keysize: "<<RSA_size(key)<<endl;
FILE *fd;
fd = fopen("key.dat", "w");
char ciphertext[RSA_size(key)];
char decrypted[RSA_size(key)];
strcpy(ciphertext, ""); //Initialise the strings
strcpy(decrypted, "");
if (RSA_private_encrypt(message.length(), (const unsigned char
*)message.c_str(), (unsigned char *)ciphertext, key, RSA_PKCS1_PADDING) == -1) {
cout<<"Error Encrypting Data!\n";
}
cout << "Ciphertext: " << ciphertext << endl;
if (RSA_public_decrypt(sizeof(ciphertext), (const unsigned char *)ciphertext,
(unsigned char *)decrypted, key, RSA_PKCS1_PADDING) == -1) {
cout<<"Error Decrypting Data!\n";
}
cout << "Decrypted: " << decrypted << endl;
return(0);
}
</encrypt.cpp>
This is the output I get from it:
<output>
bash-2.05b# ./encrypt
Test encryption program for OpenSSL.
Key Generation:
Enter a keylength (bits):
2048
Enter message:
Testing, Testing, 123
Your message is: Testing, Testing, 123
Size: 21
Keysize: 256
Ciphertext: ,�G��+�'��S��X��w3�j�\:�?�X����
Decrypted: Testing, Testing, [EMAIL PROTECTED]@0������
bash-2.05b#
</output>
The garbage I'm talking about is the '[EMAIL PROTECTED]@0������' appended to the
original message 'Testing, Testing, 123'.
Thanks for any assistance,
Patrick
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [EMAIL PROTECTED]
Automated List Manager [EMAIL PROTECTED]