Hello,
> 
> #include <cstdio>
> #include "openssl/rsa.h"
> #include "openssl/pem.h"
> #include "openssl/err.h"
> 
> 
> using namespace std;
> int main () 
> {
> RSA *rsaPrivKey = RSA_new();
Check error code !

> RSA *rsaPubKey = RSA_new();
Check error code !

> unsigned char inText[512];
> 
> 
> ERR_load_crypto_strings();
> FILE  *pubKey = fopen("rsa_publickey.pem", "r");
> FILE  *privKey = fopen("rsa_privatekey.pem", "r");
> if(pubKey)
> {
> rsaPubKey = PEM_read_RSA_PUBKEY(pubKey, &rsaPubKey, NULL, NULL);
Check error code !

> }
> if(privKey)
> rsaPrivKey = PEM_read_RSAPrivateKey(privKey, &rsaPrivKey, NULL, NULL);
Check error code !

> 
> cout << "Enter a value to be encoded: " << endl;
> cin >> inText;
> 
> 
> // Encryption
> int sigretVal = RSA_size(rsaPubKey);
> unsigned char *sigBuffer = new unsigned char[sigretVal];
> int size = strlen((char*)inText);
> RSA_public_encrypt(size, inText, sigBuffer, rsaPubKey,
> RSA_PKCS1_OAEP_PADDING);
Check error code !

> 
> // Decryption
> sigretVal = RSA_size(rsaPrivKey);
> unsigned char *plainText = new unsigned char[sigretVal];
> int encSize = strlen((char*)sigBuffer);
Do not use strlen() on encrypted data, this should be RSA_size() len.

> cout << "Encoded data size: " << encSize << endl;
> RSA_private_decrypt(encSize, sigBuffer, plainText, rsaPrivKey,
> RSA_PKCS1_OAEP_PADDING);
Check error code !

> ERR_print_errors_fp(stdout);
> cout << "The decrypted message is: " << plainText << endl;
> 
> 
> RSA_free(rsaPubKey);
> RSA_free(rsaPrivKey);
> delete [] sigBuffer;
> delete [] plainText;
> 
> 
> }

Best regards,
-- 
Marek Marcola <[EMAIL PROTECTED]>

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to