openssl 常用命令 | Openssl Useful commands

标签: none

openssl 证书操作

openssl x509 部分命令

openssl x509 -in signed.crt -noout -dates           # 打印证书的过期时间
openssl x509 -in cert.pem -noout -text            # 打印出证书的内容:
openssl x509 -in cert.pem -noout -serial            # 打印出证书的系列号
openssl x509 -in cert.pem -noout -subject           # 打印出证书的拥有者名字
openssl x509 -in cert.pem -noout -subject -nameopt RFC2253  # 以RFC2253规定的格式打印出证书的拥有者名字
openssl x509 -in cert.pem -noout -subject -nameopt oneline -nameopt -escmsb    #在支持UTF8的终端一行过打印出证书的拥有者名字
openssl x509 -in cert.pem -noout -fingerprint          #打印出证书的MD5特征参数
openssl x509 -sha1 -in cert.pem -noout -fingerprint       #打印出证书的SHA特征参数
openssl x509 -in cert.pem -inform PEM -out cert.der -outform DER     #把PEM格式的证书转化成DER格式
openssl x509 -x509toreq -in cert.pem -out req.pem -signkey key.pem    #把一个证书转化成CSR
openssl x509 -req -in careq.pem -extfile openssl.cnf -extensions v3_ca -signkey key.pem -out cacert.pem  #给一个CSR进行处理,颁发字签名证书,增加CA扩展项
openssl x509 -req -in req.pem -extfile openssl.cnf -extensions v3_usr -CA cacert.pem -CAkey key.pem -CAcreateserial  #给一个CSR签名,增加用户证书扩展项
openssl req -in my.csr -noout -text          #查看csr文件细节

SSL PrivateKey(证书私钥)添加和去除密码

检测server.key 密码是否正确

$ openssl rsa -text -noout -in server.key
Private-Key: (2048 bit)
modulus:
00:b0:fd:c2:81:60:3f:d2:dc:fe:2d:34:c6:46:1e:
08:72:c3:78:f3:4d:12:16:b9:39:3e:0b:d3:8b:e7:
…

给server.key 添加密码

$ openssl rsa -des -in server.key -out encrypt.key
writing RSA key
Enter PEM pass phrase: 密码
Verifying - Enter PEM pass phrase: 再次输入密码
encrypt.key 这个文件就是加密过的key

去掉密码, encrypt.key 有密码 / nopassword.key 无密码

$ openssl rsa -in encrypt.key -out nopassword.key
writing RSA key
Enter PEM pass phrase: 密码
Verifying - Enter PEM pass phrase: 再次输入密码

PKCS#12 去掉私钥密码 (.pfx or .p12):

拿到一个证书,里面有私钥和公钥,合在一起,pkc12的格式转的pem格式,有密码,现在需要把密码去掉

查看证书信息

$ openssl pkcs12 -info -in cert.pfx

pkc12 转 pem

$ openssl pkcs12 -in cert.pfx -out cert2.pem

分离公钥和私钥

$ openssl x509 -in cert2.pem -out cert2.crt
$ openssl rsa -in cert2.pem -out cert2.key

去掉私钥密码

$ openssl rsa -in cert2.key -out cert22.key

合并成pfx文件

$ openssl pkcs12 -export -inkey cert22.key -in cert2.crt -out cert2.pfx

转回 pem

$ openssl pkcs12 -in cert2.pfx -out cert22.pem

扫描二维码,在手机上阅读!

添加新评论