打开命令行工具,然后输入 openssl打开openssl,接着只要三句命令就可以搞定。
Mac:~/Desktop $ openssl
2、第一句命令:生成私钥:
OpenSSL> genrsa -out rsa_private_key.pem 2048
接下来Terminal显示内容如下:
Generating RSA private key, 2048 bit long modulus
...............+++
..............................................+++
e is 65537 (0x10001)
3、第二句命令从私钥中导出公钥:
OpenSSL> rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem。
Terminal显示:
writing RSA key
4.生成的文件如下:
5、以上命令默认生成的PEM为PKCS#1格式的数据。如果需要PKCS#8数据格式的PEM文件,则需要把RSA私钥转换成PKCS#8格式:
OpenSSL> pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt
//提示输入密码,密码为空(直接回车)就行;
Enter Encryption Password:
Verifying - Enter Encryption Password:
Terminal显示如下:
-----BEGIN PRIVATE KEY-----
MIIE6TAbBgkqhkiG9w0BBQMwDgQIiQ4PFYMqa7ECAggABIIEyARDwR3lbjFQYGmy
ASuI2D8LJXOtQDf/BIfFFln6d9UgppOEWlKIrp1ms6BF0nP76mtBiOpsk5QjPhTK
U4snDLT/w/hTc2OBPe66poOtzlVPtKLacfTH1pMzHUWcVz4cmCI+SAMTRkdQ1EG+
UtmdlISxoHqFIuiP4GsuyItVLrqvYMEN+4rMkcO6HgKTInvAVogp05QzJCSOVKCX
6C9Jpgdy+la9rIqteueioYofmgFojZGQtuCh03BgFBD5cHjMWNZ9X9vmxq/ih0ib
NhUptlvwQeaA2OO0pDhOM613b8osGFxvhb14KjMCQdfaYAu9fYw2rFNbZG4Q6BBu
7j+iheHiUdqA/2K9IjTf8kcJGYbQvAlz8GdL/psosk7irBndHEOOZTLgY4xU6t71
Vxwyf9WHbTuuY+MZbzMdo02lKTcznpiT0wdEu+gD5juz5wTVKyZ2HIoT+47Vte6Z
J16X9X1jw3KlIwDl/vFTEBGS1lfn8kMxyuGbomEMlF+If8MXvtY3mfBO5FHSy271
YVRCemxygsUpnMRgRjBdeCAefO8e+vEI2Rh/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
-----END PRIVATE KEY-----
然后将生成的结果覆盖原来私钥的内容即可。