Showing posts with label openSSL. Show all posts
Showing posts with label openSSL. Show all posts

Wednesday, 8 February 2023

Import a self-signed certificate to java keystore

This post is divided into three sections.

a.   Generate a self-signed certificate

b.   Import the certificate to keystore

c.    Export the public and private keys from the keystore

 

Generate a self-signed certificate

Go to this post, and create a .der file

 

Import the certificate to keystore

 Open terminal and execute below command.

 

keytool -import -alias java-blogspot -keystore myKeystore.jks -file myDomain.der

$keytool -import -alias java-blogspot -keystore myKeystore.jks -file myDomain.der
Enter keystore password:  
Re-enter new password: 
Owner: EMAILADDRESS=demo@demo.com, CN=sample-app.com, OU=hr, O=abcCorp, L=Bangalore, ST=Karnataka, C=IN
Issuer: EMAILADDRESS=demo@demo.com, CN=sample-app.com, OU=hr, O=abcCorp, L=Bangalore, ST=Karnataka, C=IN
Serial number: fef9fa87f9f94435
Valid from: Wed Feb 08 10:26:36 IST 2023 until: Thu Feb 08 10:26:36 IST 2024
Certificate fingerprints:
     SHA1: 7C:86:63:8B:3B:92:5B:EA:57:DC:B8:F0:FE:40:57:D7:EF:32:13:D6
     SHA256: E1:C6:0A:BD:34:83:C8:86:3E:65:A4:01:F0:CB:EB:C7:73:EB:F6:19:77:EE:CE:EF:62:44:63:37:73:64:2E:37
Signature algorithm name: SHA1withRSA (weak)
Subject Public Key Algorithm: 2048-bit RSA key
Version: 1

Warning:
The input uses the SHA1withRSA signature algorithm which is considered a security risk. This algorithm will be disabled in a future update.

Trust this certificate? [no]:  y
Certificate was added to keystore

 

Print the entries in keystore.

keytool -list -keystore myKeystore.jks
Enter keystore password:  
Keystore type: PKCS12
Keystore provider: SUN

Your keystore contains 1 entry

java-blogspot, 8 Feb 2023, trustedCertEntry, 
Certificate fingerprint (SHA-256): E1:C6:0A:BD:34:83:C8:86:3E:65:A4:01:F0:CB:EB:C7:73:EB:F6:19:77:EE:CE:EF:62:44:63:37:73:64:2E:37

Export public key for the alias java-blogspot

keytool -export -alias java-blogspot -keystore myKeystore.jks -rfc -file public.cert

$keytool -export -alias java-blogspot -keystore myKeystore.jks -rfc -file public.cert
Enter keystore password:  
Certificate stored in file <public.cert>
$
$
$cat public.cert 
-----BEGIN CERTIFICATE-----
MIIDlDCCAnwCCQD++fqH+flENTANBgkqhkiG9w0BAQUFADCBizELMAkGA1UEBhMC
SU4xEjAQBgNVBAgMCUthcm5hdGFrYTESMBAGA1UEBwwJQmFuZ2Fsb3JlMRAwDgYD
VQQKDAdhYmNDb3JwMQswCQYDVQQLDAJocjEXMBUGA1UEAwwOc2FtcGxlLWFwcC5j
b20xHDAaBgkqhkiG9w0BCQEWDWRlbW9AZGVtby5jb20wHhcNMjMwMjA4MDQ1NjM2
WhcNMjQwMjA4MDQ1NjM2WjCBizELMAkGA1UEBhMCSU4xEjAQBgNVBAgMCUthcm5h
dGFrYTESMBAGA1UEBwwJQmFuZ2Fsb3JlMRAwDgYDVQQKDAdhYmNDb3JwMQswCQYD
VQQLDAJocjEXMBUGA1UEAwwOc2FtcGxlLWFwcC5jb20xHDAaBgkqhkiG9w0BCQEW
DWRlbW9AZGVtby5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC+
DD14AR8oC1z8Jj2EUt7NNTTHVay8V84AeQxjqf5SULDNo428T8h3NaQNwOfpvVlP
zPbln5yit/SCSc5ZHXb2ijdedxcDeBgQn9jjXtoGZAzzjj0s36gMVw8hWJRLnNus
C0vfp7URCc4qvGWD3erktm4hh8IXDgggpOrHNXAdG1NtfUsq7u4Ys6ZUfVWJ0Qd4
PPKZDpGwpfFm07m5PxEJ7DaIMXwJaztdV/ub+DY+R5qzYltMSfNoGcjdL+zWouCh
65rNJC+iYZuh9cp/rb+N16Ln+H8mdt5Z9H/sX/00IftvCtGNyxCK42t+iF0DKiFT
4txB84NA/HvkNtRCtJptAgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAA7zpvhg6gxm
ieEFwqOSpdoUtj1b/UrmOq7Viol/PFZrSH4EWQHmIOZGTl+QQfwviZBjgJCzizRK
G+4CQBvpKTZOA1C6hYdxkhkVfdZ2fAAJahz+FuexzQR7FOxPf75Rixiosc87/m91
OVpu/KsJtUiS44cEwGfgz7LUsh6vUYHQnIIGCu2l0byuUBU64KnX1vpuDKwmQ2II
nzbANQ0CFINctgczk63FaadyPbUzUeuxy3fqxMgeIB92QqW536yGRVAOGqZ0aRgL
ouYm43egLnQMnQacHNXxVwfc/lQfoGRx1t9t46+Hu4KaINqnPYgo/my5XOAJZ+98
d+FHjYT0bYE=
-----END CERTIFICATE-----

Note

Just to note, only public key is embedded in the SSL certificate and Private key is stored on the server and kept secret.

 

Previous                                                 Next                                                 Home

Create a self-signed certificate using OpenSSL

In this post, I am going to explain how to create a self-signed certificate using OpenSSL.

 

1. Create a private key

Open terminal and execute below command.

openssl genrsa -des3 -out myDomain.key 2048

 Remove the -des3 option from the command, if you do not want the private key to be encrypted.

 

$openssl genrsa -des3 -out myDomain.key 2048
Generating RSA private key, 2048 bit long modulus
................+++
.............................+++
e is 65537 (0x10001)
Enter pass phrase for myDomain.key:
Verifying - Enter pass phrase for myDomain.key:

 

I set the password as ‘password123’. Upon successful execution of the command, you can see a file ‘myDomain.key’.

$ls
myDomain.key

 

You can see the content of myDomain.key using cat command.

$cat myDomain.key 
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,9F0FF2109392340E

yySOZ9KoyDUBF1ecjKdeo9kU3veXhF5W9wG2jsMkRFk0S/Y06OaUkoDNH3TlpNzW
iT2m+05CdyNabJMyk44Th1rrzxYVCmvIkWn/JakdMsomlEydVAZOwnx32uccFYma
fNl5HdnWPFDLC0YdEFNt0LxO5sA15wAkfa+XcD/zVNdMxpvB8v8RHYbVJqJD4hNl
4bvLyQJGpbUNvT5ppAap5waoUXrX6D3ApQaJARD1WcRpalKQ9wCavbSemeccRhGJ
1eJH8KzjWw0c5TWDyNrDhPOGZK7LP9vLZ/gFRkPV05Jhc7yU6+vfmIyLQDwG24i/
4EAFVrDxkd6k7As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0
/8TtjRNixD8AI5ZhyaTvXu6gT1eFqIxldT6lmQ16ZK1MqlyxyKXEinJmsPRewt1Z
uRyUS0SA2WkxMqatF3J+xXDG9LT0hzDWzVGJA4vFgaaRlEpai5rgqHii576ZVbQw
ewk4MW8ju1r5OHCOWEoSzD2GZaHoYDyyOKa7t+FoerrzShGv0WxEGm5VYCouQ4JA
-----END RSA PRIVATE KEY-----

 

2. Create a certificate signing request.

Open terminal and execute below command.

openssl req -key myDomain.key -new -out myDomain.csr

 .csr file is needed to sign the certificate.

 

$openssl req -key myDomain.key -new -out myDomain.csr
Enter pass phrase for myDomain.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) []:IN
State or Province Name (full name) []:Karnataka
Locality Name (eg, city) []:Bangalore
Organization Name (eg, company) []:abcCorp
Organizational Unit Name (eg, section) []:hr
Common Name (eg, fully qualified host name) []:sample-app.com
Email Address []:demo@demo.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:password456

 

Now we have two files with us.

$ls
myDomain.csr	myDomain.key

 

I set the password for .csr file as password456.

 

3. Create a self-signed certificate

 

Open terminal and execute below command.

openssl x509 -signkey myDomain.key -in myDomain.csr -req -days 365 -out myDomain.crt

$openssl x509 -signkey myDomain.key -in myDomain.csr -req -days 365 -out myDomain.crt
Signature ok
subject=/C=IN/ST=Karnataka/L=Bangalore/O=abcCorp/OU=hr/CN=sample-app.com/emailAddress=demo@demo.com
Getting Private key
Enter pass phrase for myDomain.key:

 

Upon successful execution of the command, you can see a .crt file.

$ls
myDomain.crt	myDomain.csr	myDomain.key

 

4. View the certificate

Open terminal and execute below command.


openssl x509 -text -noout -in myDomain.crt

$openssl x509 -text -noout -in myDomain.crt
Certificate:
    Data:
        Version: 1 (0x0)
        Serial Number: 18372991616756040757 (0xfef9fa87f9f94435)
    Signature Algorithm: sha1WithRSAEncryption
        Issuer: C=IN, ST=Karnataka, L=Bangalore, O=abcCorp, OU=hr, CN=sample-app.com/emailAddress=demo@demo.com
        Validity
            Not Before: Feb  8 04:56:36 2023 GMT
            Not After : Feb  8 04:56:36 2024 GMT
        Subject: C=IN, ST=Karnataka, L=Bangalore, O=abcCorp, OU=hr, CN=sample-app.com/emailAddress=demo@demo.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    00:be:0c:3d:78:01:1f:28:0b:5c:fc:26:3d:84:52:
                    de:cd:35:34:c7:55:ac:bc:57:ce:00:79:0c:63:a9:
                    fe:52:50:b0:cd:a3:8d:bc:4f:c8:77:35:a4:0d:c0:
                    e7:e9:bd:59:4f:cc:f6:e5:9f:9c:a2:b7:f4:82:49:
                    ce:59:1d:76:f6:8a:37:5e:77:17:03:78:18:10:9f:
                    d8:e3:5e:da:06:64:0c:f3:8e:3d:2c:df:a8:0c:57:
                    0f:21:58:94:4b:9c:db:ac:0b:4b:df:a7:b5:11:09:
                    ce:2a:bc:65:83:dd:ea:e4:b6:6e:21:87:c2:17:0e:
                    08:20:a4:ea:c7:35:70:1d:1b:53:6d:7d:4b:2a:ee:
                    ee:18:b3:a6:54:7d:55:89:d1:07:78:3c:f2:99:0e:
                    91:b0:a5:f1:66:d3:b9:b9:3f:11:09:ec:36:88:31:
                    7c:09:6b:3b:5d:57:fb:9b:f8:36:3e:47:9a:b3:62:
                    5b:4c:49:f3:68:19:c8:dd:2f:ec:d6:a2:e0:a1:eb:
                    9a:cd:24:2f:a2:61:9b:a1:f5:ca:7f:ad:bf:8d:d7:
                    a2:e7:f8:7f:26:76:de:59:f4:7f:ec:5f:fd:34:21:
                    fb:6f:0a:d1:8d:cb:10:8a:e3:6b:7e:88:5d:03:2a:
                    21:53:e2:dc:41:f3:83:40:fc:7b:e4:36:d4:42:b4:
                    9a:6d
                Exponent: 65537 (0x10001)
    Signature Algorithm: sha1WithRSAEncryption
         0e:f3:a6:f8:60:ea:0c:66:89:e1:05:c2:a3:92:a5:da:14:b6:
         3d:5b:fd:4a:e6:3a:ae:d5:8a:89:7f:3c:56:6b:48:7e:04:59:
         01:e6:20:e6:46:4e:5f:90:41:fc:2f:89:90:63:80:90:b3:8b:
         34:4a:1b:ee:02:40:1b:e9:29:36:4e:03:50:ba:85:87:71:92:
         19:15:7d:d6:76:7c:00:09:6a:1c:fe:16:e7:b1:cd:04:7b:14:
         ec:4f:7f:be:51:8b:18:a8:b1:cf:3b:fe:6f:75:39:5a:6e:fc:
         ab:09:b5:48:92:e3:87:04:c0:67:e0:cf:b2:d4:b2:1e:af:51:
         81:d0:9c:82:06:0a:ed:a5:d1:bc:ae:50:15:3a:e0:a9:d7:d6:
         fa:6e:0c:ac:26:43:62:08:9f:36:c0:35:0d:02:14:83:5c:b6:
         07:33:93:ad:c5:69:a7:72:3d:b5:33:51:eb:b1:cb:77:ea:c4:
         c8:1e:20:1f:76:42:a5:b9:df:ac:86:45:50:0e:1a:a6:74:69:
         18:0b:a2:e6:26:e3:77:a0:2e:74:0c:9d:06:9c:1c:d5:f1:57:
         07:dc:fe:54:1f:a0:64:71:d6:df:6d:e3:af:87:bb:82:9a:20:
         da:a7:3d:88:28:fe:6c:b9:5c:e0:09:67:ef:7c:77:e1:47:8d:
         84:f4:6d:81
$

 

5. Convert the .crt file to .der

The file generated in step 3 is in .pem encoding form. Open terminal and execute below command to get the certificate in .der form.

 

openssl x509 -in myDomain.crt -outform der -out myDomain.der

$openssl x509 -in myDomain.crt -outform der -out myDomain.der
$
$ls
myDomain.crt	myDomain.csr	myDomain.der	myDomain.key

6. Convert the .crt file to .pkcs12

Open terminal and execute below command.


openssl pkcs12 -inkey myDomain.key -in myDomain.crt -export -out myDomain.pfx

$openssl pkcs12 -inkey myDomain.key -in myDomain.crt -export -out myDomain.pfx
Enter pass phrase for myDomain.key:
Enter Export Password:
Verifying - Enter Export Password:
$
$ls
myDomain.crt	myDomain.csr	myDomain.der	myDomain.key	myDomain.pfx

Generate private key and self-signed certificate with one command

Open terminal and execute below command.

openssl req -newkey rsa:2048 -keyout myDomain2.key -x509 -days 365 -out myDomain2.crt

$openssl req -newkey rsa:2048 -keyout myDomain2.key -x509 -days 365 -out myDomain2.crt
Generating a 2048 bit RSA private key
....................+++
.................+++
writing new private key to 'myDomain2.key'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) []:IN
State or Province Name (full name) []:Karnataka
Locality Name (eg, city) []:Bangaore
Organization Name (eg, company) []:hr
Organizational Unit Name (eg, section) []:hr
Common Name (eg, fully qualified host name) []:demo@demo.com
Email Address []:demo@demo.com
$
$
$
$
$ls myDomain2*
myDomain2.crt   myDomain2.key


 

Previous                                                 Next                                                 Home

Friday, 8 April 2022

How to associate an existing private key to a self-signed certificate?

Below command associate the private key presented in the file ‘myPrivate.key’ to the domain.

openssl req
-key myPrivate.key
-new
-x509 -days 500 -out domain.crt

 

 

Previous                                                    Next                                                    Home

Sunday, 3 April 2022

openSSL: viewing the certificate as plain text

Execute below command to view the content of domain.crt in plain text.

openssl x509 -text -noout -in domain.crt

 


Previous                                                    Next                                                    Home

openSSL: Generate ssl certificate

In this post, I am going to explain how to generate ssl certificate

 

Step 1: Open terminal and execute below command.

 

openssl req -newkey rsa:2048 -nodes -keyout domain.key -x509 -days 500 -out domain.crt

$openssl req -newkey rsa:2048 -nodes -keyout domain.key -x509 -days 500 -out domain.crt
Generating a 2048 bit RSA private key
.+++
...........................+++
writing new private key to 'domain.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) []:in
State or Province Name (full name) []:karnataka
Locality Name (eg, city) []:Bangalore
Organization Name (eg, company) []:self-learning-java
Organizational Unit Name (eg, section) []:tutorials
Common Name (eg, fully qualified host name) []:self-learning-java.com
Email Address []:self-learning-java@123.com
$

 

Above command prompts some information that is used to while generating the certificate.

 

Once the command executed successfully, it generate two files ‘domain.crt’ and ‘domain.key’

$ls
domain.crt	domain.key

 


Execute below command to view the content of domain.crt in plain text.

openssl x509 -text -noout -in domain.crt

$openssl x509 -text -noout -in domain.crt
Certificate:
    Data:
        Version: 1 (0x0)
        Serial Number: 10603830801797846590 (0x93285feec781963e)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=in, ST=karnataka, L=Bangalore, O=self-learning-java, OU=tutorials, CN=self-learning-java.com/emailAddress=self-learning-java@123.com
        Validity
            Not Before: Mar 30 11:38:31 2022 GMT
            Not After : Aug 12 11:38:31 2023 GMT
        Subject: C=in, ST=karnataka, L=Bangalore, O=self-learning-java, OU=tutorials, CN=self-learning-java.com/emailAddress=self-learning-java@123.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    00:bf:7c:2a:d2:8c:f2:d0:4a:ce:dd:c3:7e:d6:e9:
                    5c:31:5f:f8:ce:0c:f9:c0:3a:b3:7b:4b:1b:f7:72:
                    48:d2:c1:66:e1:84:47:79:d0:db:f8:6c:fb:43:6a:
                    79:8d:0f:b2:5e:27:1d:22:a2:eb:b6:d7:d3:a0:c6:
                    16:b2:eb:72:45:ce:43:53:a3:ea:3d:59:96:d4:74:
                    d0:df:79:88:c7:35:a5:13:8c:3e:d0:e9:e9:1b:99:
                    08:35:0d:59:40:05:a3:0f:71:ab:b4:f0:40:f8:5d:
                    a1:2a:d1:b7:4e:9a:bc:e3:76:b6:98:43:1a:e5:31:
                    37:f8:42:71:72:38:bb:9a:94:53:2a:a9:63:18:c8:
                    48:00:03:c2:90:ca:49:de:a1:3e:82:db:af:90:e7:
                    5b:21:6d:ae:16:4f:bc:1a:74:97:c7:cb:d7:71:2c:
                    0c:10:b8:77:91:ee:1f:45:6c:8c:55:59:25:33:ec:
                    e5:2c:35:3f:c0:3b:86:28:7d:09:54:5c:1e:8a:a3:
                    b5:b3:ab:d7:21:49:2e:d8:ea:20:5d:c4:17:9f:2e:
                    08:af:c3:02:35:90:79:a7:9b:f2:1d:f6:df:62:d9:
                    9b:b0:df:e0:fc:cd:6e:56:ab:fc:86:f5:72:f5:10:
                    e1:dc:bb:c6:7e:e5:b4:fe:45:3b:f6:fe:74:d3:4e:
                    c4:c3
                Exponent: 65537 (0x10001)
    Signature Algorithm: sha256WithRSAEncryption
         46:6b:5c:b3:f5:ee:f9:6e:5f:cd:9b:c2:53:2e:00:64:42:bf:
         50:32:23:34:5c:4e:0c:30:e5:5d:a2:68:2b:4a:bb:5f:d1:3e:
         91:09:9d:de:49:6f:a9:6d:55:59:a2:52:a8:0c:e4:b6:83:2c:
         2c:f5:50:85:26:ce:87:52:5a:23:e4:d3:00:48:b0:23:40:e0:
         04:f8:94:c7:fc:f6:d1:2b:f6:2f:0d:fe:1b:f4:66:ba:7d:52:
         eb:80:ac:c3:19:4b:7f:39:db:60:c1:f2:2e:16:05:ac:81:b7:
         ec:14:57:a0:29:45:37:e4:70:ff:5f:3e:15:4a:97:eb:e6:74:
         9d:75:1a:cf:3d:41:3e:8b:94:ac:11:7e:9f:ef:50:9f:70:a4:
         b9:e2:d4:67:9a:ca:20:4d:de:9b:75:3e:1d:83:73:4b:63:02:
         4b:3e:da:7b:ec:30:b4:25:2f:12:55:96:ea:54:02:c4:cb:9a:
         b9:f0:e2:c8:e1:35:3e:72:58:96:c3:14:3e:f7:71:e9:9a:84:
         10:5b:eb:c0:85:49:8f:83:b3:32:6e:28:d0:ef:31:cb:8e:ca:
         c3:5a:73:32:47:53:fb:be:45:d4:82:c6:80:5f:74:f7:f3:83:
         58:ed:0e:0f:d2:e1:80:d4:47:ff:0c:14:c9:4d:19:5e:37:a9:
         51:f1:1a:32
$

 

Previous                                                    Next                                                    Home

openSSL: export private and public keys to a pem file

Step 1: Generate a key pair by executing below command.

openssl genrsa -des3 -out my_rsa_key_pair 2048

 

Option -des3 make sure that the pem file generated in the above step will be protected by a passphrase.

$openssl genrsa -des3 -out my_rsa_key_pair 2048
Generating RSA private key, 2048 bit long modulus
..................................+++
..........................+++
e is 65537 (0x10001)
Enter pass phrase for my_rsa_key_pair:
Verifying - Enter pass phrase for my_rsa_key_pair:
$
$ls
my_rsa_key_pair

 

 


Step 2: Export private key to privateKey.pem file by executing below commands.

 

openssl pkcs8 -topk8 -inform PEM -in my_rsa_key_pair -outform PEM -out privateKey.pem -nocrypt

$openssl pkcs8 -topk8 -inform PEM -in my_rsa_key_pair -outform PEM -out privateKey.pem -nocrypt
Enter pass phrase for my_rsa_key_pair:
$
$ls
my_rsa_key_pair	privateKey.pem
$
$cat privateKey.pem 
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCggQ7vAqJvYhYv
v7V6GeoDS258BeANEShKWslGqN8V6R2xLMB/dFyyDt50YBFb7gScliZQmNjFDjo/
EbtaETGxxTv5958TOA4VfurEwHrzMbyChTZwAKlBKhvyyVFN2ZATLR0jMg97g6f1
PdDvCYFIbk15RMKjSXSEz7xG8K6hKvkdoFqk4MORHvvC1e438+KqwhwSgVCa5D0E
UQ0Hm6aUOjRrVwn7iuG/pktndPWMWfqcS9yGUIdOlg2F1rZQdorej0O9MCVsIEON
nGXhStwnGJMchGcraFsEsS8JROmVcZQV+8ebPM2R+hmIQi1xAAw4stWQ4EJkJq2b
SHop9ekHAgMBAAECggEASqyg4QyNvFqbTe3Ypg1Gde/zYFpvakTwRN/GQ0ywNgZ2
XQcgLGxb8C/Ayc4kZWvw01V8UAU83TmIWe2LCBV11GqaQ+s0JIHw84lP+ug+i8U5
GlSWBjgYhwvrYuLNI/Yz9OgJZWWWFHYlf5EaEwyODL/c5a3bDJNRVbU8uJij9rFF
9jTjSgAb6+s/XvL27uo8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to publicKey.pem file by executing below command.

 

openssl rsa -in my_rsa_key_pair -outform PEM -pubout -out publicKey.pem


 

$openssl rsa -in my_rsa_key_pair -outform PEM -pubout -out publicKey.pem
Enter pass phrase for my_rsa_key_pair:
writing RSA key
$
$ls
my_rsa_key_pair	privateKey.pem	publicKey.pem
$
$cat publicKey.pem 
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoIEO7wKib2IWL7+1ehnq
A0tufAXgDREoSlrJRqjfFekdsSzAf3Rcsg7edGARW+4EnJYmUJjYxQ46PxG7WhEx
scU7+fefEzgOFX7qxMB68zG8goU2cACpQSob8slRTdmQEy0dIzIPe4On9T3Q7wmB
SG5NeUTCo0l0hM+8RvCuoSr5HaBapODDkR77wtXuN/PiqsIcEoFQmuQ9BFENB5um
lDo0a1cJ+4rhv6ZLZ3T1jFn6nEvchlCHTpYNhda2UHaK3o9DvTAlbCBDjZxl4Urc
JxiTHIRnK2hbBLEvCUTplXGUFfvHmzzNkfoZiEItcQAMOLLVkOBCZCatm0h6KfXp
BwIDAQAB
-----END PUBLIC KEY-----

 


 

Previous                                                    Next                                                    Home