Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a rsa public key from its modulus and exponent

Tags:

openssl

rsa

I want to verify a RSA signature. I have data to verify, the signature and a public key in a form of modulus and exponent. I'd like to do the verification using openssl. Is it possible? I know I can use openssl rsautl -verify -in sig -inkey key.pem but I don't know how (using openssl) to create a public key having just it's modulus and exponent.

Maybe other ideas how to check this signature (except writing some programs)?

like image 652
emstol Avatar asked Jul 18 '12 12:07

emstol


People also ask

How do you generate a public key from modulus and exponent?

To generate public key from the exponent and modulus, they need to be transformed to BigInteger, and then KeyFactory from Java security can be used.

What is modulus and exponent in RSA public key?

Public key contains modulus and public exponent. Modulus (n) is the product of two prime numbers used to generate the key pair. Public exponent (d) is the exponent used on signed / encoded data to decode the original value.

Is modulus the same as public key?

The public key consists of the modulus (n) and the public exponent (e). The private exponent used for encryption and decryption. Called d. The private key consists of the modulus (n) and the private exponent (d).

How do you calculate RSA modulus?

At the center of the RSA cryptosystem is the RSA modulus N. It is a positive integer which equals the product of two distinct prime numbers p and q: RSA modulus: N = pq.


1 Answers

In order to generate a RSA public key in PEM format to be used with openssl, you can follow these steps.

Create an ASN1 definition file

Modify the following template to include your modulus and exponent

# Start with a SEQUENCE
asn1=SEQUENCE:pubkeyinfo

# pubkeyinfo contains an algorithm identifier and the public key wrapped
# in a BIT STRING
[pubkeyinfo]
algorithm=SEQUENCE:rsa_alg
pubkey=BITWRAP,SEQUENCE:rsapubkey

# algorithm ID for RSA is just an OID and a NULL
[rsa_alg]
algorithm=OID:rsaEncryption
parameter=NULL

# Actual public key: modulus and exponent
[rsapubkey]
n=INTEGER:0x%%MODULUS%%

e=INTEGER:0x%%EXPONENT%%

Instead of editing, you also may want to script this using sed

sed -i "s/%%MODULUS%%/$(xxd -ps -c 256 mymodulus.bin)/" def.asn1

Note the -c 256 should be chosen according to your key length in bytes.

You can use a similar command for the exponent.

Generate your RSA key

Use the following openssl command. This will give you a DER encoded RSA key.

openssl asn1parse -genconf def.asn1 -out pubkey.der -noout

Then convert it into a PEM key

openssl rsa -in pubkey.der -inform der -pubin -out pubkey.pem

Verify using your key

You can use either openssl dgst -verify or openssl rsautl -verify

like image 147
oliv Avatar answered Oct 02 '22 14:10

oliv