PEM files are not hex. They are base64, plus header and trailer lines.

You can read any supported PEM file with the functions in pem.h.

OpenSSL historically supports several different PEM formats for 

privatekeys - man PEM -- but the PEM_read routines can handle 

all of them automatically because the header line identifies which.

(For DER format this is not generally true.)

 

For RSA the private key is at least (d,n), and OpenSSL always generates 

the "CRT" form which has half a dozen other elements to allow more 

efficient computation. If you only want d, because you have n from 

elsewhere (e.g. the publickey) and you don't want to be efficient,

just look at the d element in the resulting RSA structure you got from 

PEM_read, or the pkey.rsa->d element in the EVP_PKEY ditto.

It's a bignum. It will have a value that is usually but not always 

the size of n, which is sometimes but not always 256 bytes (2048 bits).

man BN_bn2bin about extracting any bignum to bytes.

 

As already noted 'encrypting' with the private key is really signing, and 

only for RSA; other signing algorithms aren't related to encryption at all.

I'm not sure what you can 'verify .. correct'. If you want to recompute 

a signature and compare it to one OpenSSL did, that only works for 

deterministic padding like PKCS1v1_5. If you use PSS, you can do any

(practical) number of signatures and they are all different but correct.

The generally valid way of verifying an RSA signature is to verify it, which


is 'decrypting' with the *public* key (i.e. p = c^e mod n), unpadding 

with appropriate checks and comparing to the expected value. See PKCS#1.

 

For other algorithms (DSA and ECDSA) repeated signatures are *always* 

different and the only way to verify is to verify.

 

 

From: owner-openssl-us...@openssl.org
[mailto:owner-openssl-us...@openssl.org] On Behalf Of Ameet Lann
Sent: Sunday, November 17, 2013 05:12
To: 'openssl-users@openssl.org'
Subject: *** Spam *** how to extract the private key out of the KEY.PEM

 

Hi,

We work on verification of code in our company.

We use the openssl to generate the key-pair and then to encrypt data using
the key-pair. However we would like to verify that we do everything
correctly so we want to encrypt independently using the private key. So our
question is how do we extract the private key as a 256 byte out of the 1766
bytes in HEX format of the private key section inside the key.pem?

 

Regards,

Ameet

Reply via email to