PEM_read_RSAPublicKey returns NULL


 
Thread Tools Search this Thread
Top Forums Programming PEM_read_RSAPublicKey returns NULL
# 1  
Old 11-20-2008
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. Smilie This is what I am trying to do.
Code:
#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!!!

Last edited by vino; 11-20-2008 at 05:14 AM..
# 2  
Old 11-20-2008
Issue resolved

Hi,

The issue got resolved.

I changed PEM_read_RSAPublicKey to PEM_read_RSA_PUBKEY and it worked.Smilie

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting standard out to /dev/null goes to file "/dev/null" instead

I apologize if this question has been answered else where or is too elementary. I ran across a KSH script (long unimportant story) that does this: if ; then CAS_SRC_LOG="/var/log/cas_src.log 2>&1" else CAS_SRC_LOG="/dev/null 2>&1" fithen does this: /usr/bin/echo "heartbeat:... (5 Replies)
Discussion started by: jbmorrisonjr
5 Replies

2. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1 (3 Replies)
Discussion started by: glev2005
3 Replies

3. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

4. Shell Programming and Scripting

Grep returns nothing

Hi all, I am trying to grep a .txt file for a word. When I hit enter, it returns back to $ The file is 4155402 in size and is named in this way: *_eveningtimes_done_log.txt I use this command, being in the same directory as the file: grep -i "invalid" *_eveningtimes_done_log.txt ... (16 Replies)
Discussion started by: DallasT
16 Replies

5. Shell Programming and Scripting

awk returns null?

hi i try to check if awk returns null and i dont know how it's works this is the command set EndByC = `ls -l $base | awk '/.c$/ {print $9}'` if ($EndByC=="") then #check if ther is XXX.c directory echo Sorry ther is no XXX.c folder "in" $base path echo the XXX.c folder is necessary... (6 Replies)
Discussion started by: frenkelor
6 Replies

6. UNIX for Dummies Questions & Answers

echo statement when find returns null

Hi, How do you echo something once when a find statement returns null results? This is when using mutiple locations and mutiple arguments. The below find command the inner loop of a nested for loop where the outter loop holds the $args and the inner loop holds the locations. find... (2 Replies)
Discussion started by: tchoruma
2 Replies

7. IP Networking

gethostbyname_r returns NULL when hostname has dash

We have a code to find the DNS entry of a host that has a trailing '-' in its url (format example: mysite-.watch.com): if(gethostbyname_r(host,host_ent,host_buffer,host_buffer_size,&host_error)==NULL) { //failed } But when remove the '-' from the host name the code does not return... (12 Replies)
Discussion started by: uunniixx
12 Replies

8. Shell Programming and Scripting

compare null with non-null

I've got a very peculiar situation. I'm trying to find out if we can compare null fields with non-null. I've output csv files from SQL and Oracle. I need to compare each field from the files, and then find out any differences. The files usualy have over 500 fields, and send the resule to DBA.... (8 Replies)
Discussion started by: nitin
8 Replies

9. Programming

inet_addr() returns 0

Im trying to connect to a particular IP address and I'm tying to use gethostbyaddr() and inet_addr() to do this. However, when I tried using inet_addr(), I always get a return value of 0 when I tried to connect to "172.21.16.238". Hope someone here could help me on this. I already tried using inet_... (1 Reply)
Discussion started by: soulfactory2002
1 Replies
Login or Register to Ask a Question