How to generate MODULUS for RSA using openssl?

If you use openssl RSA API to encrypt/decrypt/sign/verify, you will need a MODULUS, which is part of a RSA public key. To generate a MODULUS, first you need to generate a RSA private key:

openssl genrsa -out mykey.key 1024

Generating RSA private key, 1024 bit long modulus
...................................+++++
..+++++
e is 65537 (0x10001)

You can see it generates a 1024 bit RSA private key with the exponent 65537(RSA_F4). You can specify another exponent(3) when generating the key.

openssl genrsa -3 -out private.key 1024
Generating RSA private key, 1024 bit long modulus
...................+++++
...................................+++++
e is 3 (0x3)

However, you cannot specify other numbers except 65537 and 3 for the exponent. The key file has the following content:

—–BEGIN RSA PRIVATE KEY—–
MIICXAIBAAKBgQDKrGUwOmzDpiTdMIwNQMP/6DcKPfeOX7uKVZDLL8raP4+WDslp
qRVp+N7AC8v59LBNJaAXMXi36JhNDKtRZU9IUCB2Mc2EwCGaiROHLo1kkFhMdcZf
YBrn8A99R/Ed/4rMpdXY8MJWnUnv0/kUuv8MEbKFOQewA1ClBm1jXoXgGwIBAwKB
gQCHHZjK0Z3XxBiTdbKzgIKqms9cKU+0P9JcOQsyH9yRf7UOtIZGcLjxUJSAB91R
TcreGRVky6XP8GWIsxzg7jTZrrfY2/UzbpImD0MGJU46jCcq6f2HKOOuZf6dMW7a
aZ1zGNVpOgzWZPh2vkykIJGNB9MHzvbZoToFceDfgi8H+wJBAPzbTa2uD6jg+8Xu
R7/07rR41wfZqDEMH+1ZvY8zAzC75MX+jQfn4UkzBVYELmWSEq47OUt5CIkrRvKP
9B7UzMUCQQDNMWM6L6fxZWWsQLY2ox4JpLUO8GxxuUJpt9Pul9MwYrs6ly4RxzO8
ojhggfAkkqXXusA3HGE4577DB/8aodfAkEAqJIzyR61G0Cn2UmFKqNJzaXkr+Zw
ILK/85EpCiICIH1DLqmzWpqWMMyuOVge7mFhyXzQ3PtbBhzZ9wqivziIgwJBAIjL
l3wfxUuY7nLVzs8XaVvDI19K8vZ7gZvP4p8P4iBB0icPdAvaIn3BeusBSsMMbo/R
1XoS61re4/Cyv/2cWj8CQA9FDNuV+FcJCjQIAiu0MFefa8ztjGIet5BueIGWBqGi
3QB2wTyf0lAbB6cdy/JDQWkSjg0t7gr4VtELDWGBsAA=
—–END RSA PRIVATE KEY—–

You can extract the public key from the private key file:

openssl rsa -in private.key -outform PEM -pubout -out public.pem

Here is what the public key looks like:

—–BEGIN PUBLIC KEY—–
MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDKrGUwOmzDpiTdMIwNQMP/6DcK
PfeOX7uKVZD689raP4+WDslpqRVp+N7AC8v59LBNJaAXMXi36JhNDKtRZU9IUCB2
Mc2EwCGaiROHLo1kkFhMdcZfYBrn8A99R/Ed/4rMpdXY8MJWnUnv0/kUuv8MEbKF
OQewA1ClBm1jXoXgGwIBAw==
—–END PUBLIC KEY—–

The MODULUS and the exponent are in the public key, and you can get them using the following command:

openssl rsa -pubin -in public.pem -text -noout
Public-Key: (1024 bit)
Modulus:
    00:ca:ac:65:30:3a:6c:c3:a6:24:dd:30:8c:0d:40:
    c3:ff:e8:37:0a:3d:f7:8e:5f:bb:8a:55:90:cb:2f:
    ca:da:3f:8f:96:0e:c9:69:a9:15:69:f8:de:c0:0b:
    cb:f9:f4:b0:4d:25:a0:17:31:78:b7:e8:98:4d:0c:
    ab:51:65:4f:48:50:20:76:27:cd:84:c0:21:9a:89:
    13:87:2e:8d:64:90:58:4c:35:c6:5f:60:1a:e7:f0:
    0f:7d:47:f1:1d:ff:8a:cc:a5:d5:d8:f0:c2:56:9d:
    49:ef:d3:f9:14:ba:ff:0c:11:b2:85:39:07:b0:03:
    50:a5:06:6d:63:5e:85:e0:1b
Exponent: 3 (0x3)

Alternatively, you can get the modulus, together with other components such as publicExponent, privateExponent, prime1, prime2, exponent1, exponent2, coefficient, using one command:

openssl rsa -in private.key -text

 

Leave a Reply