Saturday, June 22, 2019

OpenSSL: Generating an RSA Key From the Command Line

OpenSSL: Generating an

RSA Key From the

Command Line



 

Generate a 2048 bit RSA Key



openssl genrsa  -out private.pem 2048


Generate a 3072 bit RSA Key



openssl genrsa  -out private.pem 3072


Generate a 4096 bit RSA Key



openssl genrsa  -out private.pem 4096


prints out the various public or private key

components in plain text in addition to the

encoded version

openssl rsa -text -in private.pem



 

Export the RSA Public Key to a File



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


   

The Generated Key Files

The generated files are base64-encoded encryption keys in plain text format.
If you select a password for your private key, its file will be encrypted with
your password. Be sure to remember this password or the key pair becomes
useless.

The private.pem file looks something like this:

-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA6JtguftyimdvYIG4X7r6MmrPHBlhs9CrxPZ0nAb/a7bCDxav
/GSEKVQfE6JBI1Ehc7D8ylpI607hTXuBTqVA4Q/nWKPThdeknIl3ORhFlHfHjBhD
H60BwweOuV7mj0lT+gwdqUP/8HtcO6KkiKtXOZ7clZNPyD8kb/A5pq25ucMlcxhO
/aDteFmSudaftwp5CYFfLyX+BIel3mBqQ95DdQmZROrtgDQuspU4kCfMflbyPYso
JgB3uLV/RH7IWvUHwR+IAVjkjluBWdACOcOvEtcss/gI7UIJ2RgcAfO7zICPIk7B
4X49/dzmqDFjBMrm/DiSTbcBRoDHuEvtt59x1wIDAQABAoIBAFPRqclbEqtNGpVs
KURV3FLOqlM10j85sqwHI34WB3SJJuTJCCGrFvTNm2U30sEnOya1YGKKpjwk8Is7
Nh1g70AquvL2FgLk8ix2XL/2PxyBDSculwGTFk9xKb0itXJsuhYgx493MYM28MWM
lj2pgIUC+fnsW5ONLVQo/J1TfNmzCJXcQ3pBq428oljtc5HUEgd9WYr79nwCnb4I
nsH8rJ7JisLrZEVX2sjO7V7JiMJJ/BoSx5XVTREo2ESTsOxpXnHAsbWYof6fTZ9V
4zPI80canzfYnl6Xkm9F8eH+zI5eJRwRh4MlZ7DLtRGh80i370EHTm8k8vKBB4oV
p1/zigECgYEA/AqIFP89ItpwfhGZzNQm1OwJk8dT0zwB428OJanpGnrRqcGmHFtM
/hKJ1L+iBPsejzJJ4GlF12QWmQTsXf7YQjQz10eO8/Eb98JnFTZRV2M6t4yg5J//
N8BqAiq47tcSMaTQoF+m7Y2ow+EWeOZeMFfbRLEazU3AjjBDxw+wVysCgYEA7EKz
VYARhGEd+sEKvLGMiBDQPRfN/RTpYf6D6DclN/FOYSfMXtaMpue+fP0FspJHqs4K
zTGpmPnYugxzT01CHg8C5N0PD5TorxHSWdR8U1lu8oZ5lt5eCjeipClCnwcBlFxL
GabRTLqSxX60LwhzC1ufCx0YBIqSgCzU+ElKOgUCgYANPLhc8fLSC8rwtBfxzAqm
ECeInWVnqLUorsJ9c+kMPPsaAVOqFZl7lpmqlM37mPzH5IpAwQasA1O0ga+wWBwf
jgJTAi0d3sHrR0iGsq2e+1UwIrCokUakNPTcXEYONTl9ZfyXD68CtvfwIbg+bUrx
GwwnFW4k7jp4vUwx/j7ytQKBgBk8JpuDSluxY9pctCDjdfcylItx93aIvUTSQpST
D06iX5TRA2s9z1gkeJwxCmLAbRc5Wr4AB/Vm+lck7UwTHHTJda2sTueDKDdK2ATw
RTdB3i/ZXTNYHQNKXfzay1rhDLIkCYF75+8iHnspxWUgqekUqDAvs1kI1A0MvNKo
QV1hAoGAGY4uqbcqVdrdCp/qe5NFWB+9gWVMq88TBppOXIPaM7MWOJR2mtH4e9Q4
sM1JLOfcCYjYeKVhED7woHmwtl4fy048+PHxGhPoN3ph7mmLd40w8dltFzT6DASe
QhKHiKlMXlmBfz2Et9oOdnQIBXiDUCHUtekEL4iiGguxdlhsI3Q=
-----END RSA PRIVATE KEY-----

 

The public key, public.pem, file looks like:



-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6JtguftyimdvYIG4X7r6
MmrPHBlhs9CrxPZ0nAb/a7bCDxav/GSEKVQfE6JBI1Ehc7D8ylpI607hTXuBTqVA
4Q/nWKPThdeknIl3ORhFlHfHjBhDH60BwweOuV7mj0lT+gwdqUP/8HtcO6KkiKtX
OZ7clZNPyD8kb/A5pq25ucMlcxhO/aDteFmSudaftwp5CYFfLyX+BIel3mBqQ95D
dQmZROrtgDQuspU4kCfMflbyPYsoJgB3uLV/RH7IWvUHwR+IAVjkjluBWdACOcOv
Etcss/gI7UIJ2RgcAfO7zICPIk7B4X49/dzmqDFjBMrm/DiSTbcBRoDHuEvtt59x
1wIDAQAB
-----END PUBLIC KEY-----


Encrypt/Decrypt Using RSA Public/Private Key

Step:-1
Create Demo.txt


cat demo.txt
Hello This is Demo for Encrypt file


Step:-2
Encrypt Demo.txt File using RSA Public Key
openssl rsautl -encrypt -in demo.txt -pubin -inkey public.pem -out demo_encrypted.pem


Step:-3
Decrypt Demo.txt Encrypted file using RSA Private Key
openssl rsautl -encrypt -in demo.txt -pubin -inkey public.pem -out demo_encrypted.pem


Step:4
Check the Decrypted file its should be same as demo.txt
cat demo_descrypted.pem
Hello This is Demo for Encrypt file

5 comments:

  1. Hi Vijay,

    I believe in step 2 and Step 3 both , you've given screenshot of the Encrypt command and the decryption command is missing.

    Thanks,
    Bits

    ReplyDelete
  2. I must appreciate you for providing such a valuable content for us. I found so many interesting stuff in your blog Helped a lot in increasing my knowledgeebay bellen

    ReplyDelete
  3. Hi

    Let p, q, and e be three prime numbers. Let n = p*q. We will use (e, n) as the public key. Please
    calculate the private key d. The hexadecimal values of p, q, and e are listed in the following. It should be
    noted that although p and q used in this task are quite large numbers, they are not large enough to be secure.
    We intentionally make them small for the sake of simplicity. In practice, these numbers should be at least
    512 bits long (the one used here are only 128 bits).
    p = F7E75FDC469067FFDC4E847C51F452DF
    q = E85CED54AF57E53E092113E62F436F4F
    e = 0D88C3

    ReplyDelete
  4. can you provide me commands for this

    ReplyDelete
  5. Chennai's No.1 software training institute, Infycle Technologies, provides the best Big Data Hadoop Training in Chennai for students, freshers, and tech professionals along with other corporate courses such as Data Science, Cloud computing, DevOps, Digital Marketing, Python, Big Data, Selenium, Java, Hadoop, iOS, and Android development with 100% hands-on training. After the completion of training, the students will be sent for placement interviews in the core MNC's. Call 7502633633 to get more info and a free demo.Big Data Hadoop Training in Chennai | Infycle Technologies

    ReplyDelete

Wireless Security Configuration: Protect Your Network Now!

Introduction: In today’s connected world, wireless networks are as common as smartphones, and they’re often the gateway to our personal, pr...