PEM_read_RSAPublicKey returns NULL

Hi all,

I am trying to write a program in C which will generate private and public keys using openssl RSA and use these for encryption and decryption. I am able to generate the keys successfully and write these to files. I am able to read the private key successfully. I can encrypt and decrypt using this private key. But finally I want to be able to send the key to another program. So I need to read the public key. But it is returning NULL. :frowning: This is what I am trying to do.

#include <stdio.h>
#include <algorithm>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/crypto.h"
#include "openssl/x509.h"
#include "openssl/pem.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
#include "openssl/rsa.h"

int main(int argc, char *argv[])
{
RSA *rsa=NULL, *rsa_rpu=NULL;
OpenSSL_add_all_algorithms();
//Generate the keys
if ((rsa=RSA_generate_key(2048,65537,NULL,NULL)) == NULL){
       cout<<"Creation of keys failed"<<endl;
}
else
{
       cout << "success"<<endl;
}

FILE *fp;
 
fp = fopen("server_public.key","w");
 if (!PEM_write_RSA_PUBKEY(fp, rsa )){
        cout<<"Error writing public key"<<endl;
}
else{
       cout<<"Public key written succesfully"<<endl;
}
fclose(fp);
fp = fopen("server_private.key","w");
if (!PEM_write_RSAPrivateKey(fp, rsa, EVP_des_ede3_cbc(), (unsigned char *)"mypassword" , strlen("mypassword"),NULL, NULL)){
       cout<<"Error writing private key"<<endl;
}
else{
       cout<<"Private key written succesfully"<<endl;
}
fclose(fp);
fp = fopen("server_private.key","r");
rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
if (rsa==NULL){
       cout<<"Reading of private key failed"<<endl;
}else{
       cout<<"Reading of private key successful"<<endl;
}
fclose(fp);
fp = fopen("server_public.key","r");
 rsa_rpu = PEM_read_RSAPublicKey(fp,NULL, NULL, NULL);
 cout << "rsa_rpu is: "<<rsa_rpu;
if (rsa_rpu==NULL){
      cout<<"Reading of public key failed"<<endl;
}
else{
      cout<<"Reading of public key successful"<<endl;
}
unsigned char *encryptedString=(unsigned char *)malloc(RSA_size(rsa));
RSA_blinding_off(rsa);
if (RSA_public_encrypt(strlen("TrialString")+1,(unsigned char*)"TrialString",(unsigned char*)encryptedString,rsa,RSA_PKCS1_PADDING)==-1){
     cout<<"encryption failed "<<endl;
}
else{
     cout<<"Encryption success"<<endl;
}
unsigned char *plainText=(unsigned char *)malloc(RSA_size(rsa));
if (RSA_private_decrypt(RSA_size(rsa),encryptedString,(unsigned char*)plainText,rsa,RSA_PKCS1_PADDING)==-1){
     cout<<"Decryption failed "<<endl;
}
else{
     cout<<"Decryption success"<<endl;
}
cout<<"Plain text"<<plainText<<endl;

 return 0;
}

I have tried replacing the part in read with server_private.key but it is not working.

This is what I get:

success
Public key written succesfully
Private key written succesfully
Enter PEM pass phrase:
Reading of private key successful
rsa_rpu is: 0Reading of public key failed
Encryption success
Decryption success
Plain textTrialString

The encryption and decryption are working because I am populating rsa using the private key for both encryption and decryption

Thanks in advance!!!

Hi,

The issue got resolved.

I changed PEM_read_RSAPublicKey to PEM_read_RSA_PUBKEY and it worked.:slight_smile:

Thanks