Hi,
I have been trying to read the keys generated by "openssl genrsa ..."
and "openssl rsa -pubout ..." commands.
I successfully (according to the return code) read the private key with
if (in = BIO_new_file("rsakey.pem", "r")) {
int ok;
printf ("Created private BIO\n");
ok = (PEM_read_bio_RSAPrivateKey(in, &rsa, NULL, NULL) != NULL);
printf ("ok = %s\n", (ok != 0) ? "true":"false");
BIO_free(in);
}
but the similar code using PEM_read_bio_RSAPublicKey() doesn't want to
work.
Can someone help with ether some example code, or some idea what I am
doing wrong?
I want to be able to encript/decrypt a string with both the private
and public keys.
Hopefully Neil.
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <stdio.h>
BIO *bio_err=0;
int main (int argc, char**argv)
{
SSL_CTX *ctx;
SSL *ssl;
BIO *sbio;
RSA *rsa;
#if 0
if ((rsa = RSA_new()) != NULL) {
printf ("Allocated new RSA structure\n");
if (RSA_print(bio_err, rsa, 0) == 0)
printf ("Failed to print\n");
printf ("size = %d bytes\n", RSA_size(rsa));
RSA_free(rsa);
}
#endif
#if 0
if ((rsa = RSA_generate_key(2048, 17, NULL, NULL)) != NULL) {
int ok;
printf ("Allocated new RSA structure\n");
ok = PEM_write_RSAPublicKey(stdout, rsa);
RSA_free(rsa);
}
#endif
if ((rsa = RSA_new()) != NULL) {
printf ("Allocated new RSA structure\n");
BIO *in;
if (in = BIO_new_file("rsakey.pem", "r")) {
int ok;
printf ("Created private BIO\n");
ok = (PEM_read_bio_RSAPrivateKey(in, &rsa, NULL, NULL) != NULL);
printf ("ok = %s\n", (ok != 0) ? "true":"false");
BIO_free(in);
}
if (in = BIO_new_file("pubkey.pem", "r")) {
int ok;
printf ("Created public BIO\n");
ok = (PEM_read_bio_RSAPublicKey(in, &rsa, NULL, NULL) != NULL);
printf ("ok = %s\n", (ok != 0) ? "true":"false");
BIO_free(in);
}
PEM_write_RSAPublicKey(stdout, rsa);
RSA_free(rsa);
}
}