1.先创建一个文件夹存放生成下来的文件,位置在Documents下,名为pay。
2.通过命令行进入到当前文件夹下:
cd /Users/yococo/Documents/pay
如:
yococodeiMac:~ yococo$ cd /Users/yococo/Documents/pay
3.进入 OpenSSL 程序:
openssl
如:
yococodeiMac:pay yococo$ openssl
4.生成
genrsa -out rsa_private_key.pem 1024
如:
OpenSSL> genrsa -out rsa_private_key.pem 1024
Generating RSA private key, 1024 bit long modulus
..........................................++++++
..............................................................++++++
e is 65537 (0x10001)
假如出现这样的信息:
OpenSSL> genrsa -out /Users/yococo/Documents/pay/rsa_private_key.pem 1024
Generating RSA private key, 1024 bit long modulus
...................................................................++++++
...................++++++
unable to write 'random state'
e is 65537 (0x10001)
则可以在生成 前执行命令:
sudo rm ~/.rnd
如:
yococodeiMac:pay yococo$ sudo rm ~/.rnd
Password:
5.生成
rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
如:OpenSSL> rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
writing RSA key
OpenSSL>
6.生成
pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt
如:OpenSSL> pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALvg2hNomwq2tLsF
RoNp1uVhZlTyBW64WhDPBcb3gKDrMHZb0SmTNb5RM4mn2rciGOgu4M2yr6lvl/IR
zz+xe0PA6x1XDfJ1Pt+wSYwqPYSMlOl1Ey0Z0Jc8WxYZD73NmckQ2so7ouRKioYo
weztNvFPFkW2l4JmRlW8A5DxRF1DAgMBAAECgYEAt6rFBhxJFVwvqnUpzeZ2Ep/5
Lim7bdYDFlOMTko/ERFauxpfi8lBzOANBhF2G3nV54WMdwqr93QEkr0eYiHbYV8X
4eMaOGDm5lm2e+chj3fJ+BTEUxRdWhwJMaox+FWpOcnY1iQ+SAwVagl1joF2Qujm
3y/W+PVcQOiHOrzer8ECQQDryRWnY3SXorvLwCnWCUoWHe1amqidEf58qhs/mqPN
kwgV/jFSX3Ls5flkMEpzk8XLv4PDdlYguwChsittIqx7AkEAy/xSeYwLzGITrvln
EcXSwnE7PcqnpazgqrVaOPkw69VmhQ5ushq3iauO1YtirMOVARomT+8w24IRtxxe
Kd6r2QJBAMx6V6gRjTRPsXMylFWFOtXRYFz50Q5eY4ezcewH7NREFXgVOKrKEf22
N0CWzIpM9fULgnaJconyAss3m4eK0W8CQCj/icQMNcLaY4JjdifITtft0i5VMOTH
nlAxKvUQgJilx3kusWjynbj51jdtUGPQ233hN5oRRQyZU94twW9A5eECQFqpq9ZN
bCfoyCBwb1BuTHn7B610qqPBifxhGVAkt8zhnI0AJG2ffRk+zvRR+cwtJ7o9Fw+v
sD/+nphpjq1BkcQ=
-----END PRIVATE KEY-----
7.退出OpenSSL 程序
q
如:
OpenSSL> q
8.
openssl rsa -inform PEM -in rsa_public_key.pem -pubin -text
如:
yococodeiMac:pay yococo$ openssl rsa -inform PEM -in rsa_public_key.pem -pubin -text
Modulus (1024 bit):
00:bb:e0:da:13:68:9b:0a:b6:b4:bb:05:46:83:69:
d6:e5:61:66:54:f2:05:6e:b8:5a:10:cf:05:c6:f7:
80:a0:eb:30:76:5b:d1:29:93:35:be:51:33:89:a7:
da:b7:22:18:e8:2e:e0:cd:b2:af:a9:6f:97:f2:11:
cf:3f:b1:7b:43:c0:eb:1d:57:0d:f2:75:3e:df:b0:
49:8c:2a:3d:84:8c:94:e9:75:13:2d:19:d0:97:3c:
5b:16:19:0f:bd:cd:99:c9:10:da:ca:3b:a2:e4:4a:
8a:86:28:c1:ec:ed:36:f1:4f:16:45:b6:97:82:66:
46:55:bc:03:90:f1:44:5d:43
Exponent: 65537 (0x10001)
writing RSA key
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC74NoTaJsKtrS7BUaDadblYWZU
8gVuuFoQzwXG94Cg6zB2W9EpkzW+UTOJp9q3IhjoLuDNsq+pb5fyEc8/sXtDwOsd
Vw3ydT7fsEmMKj2EjJTpdRMtGdCXPFsWGQ+9zZnJENrKO6LkSoqGKMHs7TbxTxZF
tpeCZkZVvAOQ8URdQwIDAQAB
-----END PUBLIC KEY-----