关于
pem
、der
、crt
、cer
、key
的内容…
区别
证书本身只是文本文件,通过不同的编码方式使用不同的后缀名保存起来。
其中pem
和der
是编码格式:
pem
是文本格式,也就是ASCII(Base64)
格式,开源软件一般用这个比较多。Base64
编码使用了64个字符(The “standard” alphabet uses A-Z, a-z, 0-9 and + and /, with = as a padding character. There are URL-safe variants.),将原始的8*3
个bit分成6*4
个bit,每6个bit编码成对应的字符。因此比原始问价占据更多的空间(约8/6=1.33
倍)。der
是二进制格式,windows用这个。(It’s useful to think of it as a binary version of the base64-encoded PEM file.)
而crt
、cer
只是证书的扩展名,key
是秘钥的扩展名,当然pem
和der
也可以作为扩展名。
使用openssl可以直接读取证书,默认编码格式是pem
,可以通过指定-inform
为der
改变编码格式:
# 查看pem格式的证书
openssl x509 -in ca.crt -text -noout
# 查看der格式的证书
openssl x509 -in ca.crt -inform der -text -noout
# 查看pem格式的私钥
openssl rsa -in exp3.pri.key -inform pem -text -noout
-noout Don't print key out
-text Print the key in text
如果编码格式不对就会报错。
pem
格式的证书就是以BEGIN
和END
开始和结束。
der
格式的证书用文本方式打开就像是乱码一样,不过der
格式的证书更小一些。
转换
使用openssl可以进行编码的转换。
# pem转der
openssl x509 -in cert.pem -outform der -out cert.der
# der转pem
openssl x509 -in cert.der -inform der -outform pem -out cert.pem
参考:
- https://stackoverflow.com/questions/22743415/what-are-the-differences-between-pem-cer-and-der
- https://serverfault.com/questions/9708/what-is-a-pem-file-and-how-does-it-differ-from-other-openssl-generated-key-file
- http://www.gtopia.org/blog/2010/02/der-vs-crt-vs-cer-vs-pem-certificates/
- https://www.poftut.com/convert-der-pem-pem-der-certificate-format-openssl/
- http://info.ssl.com/article.aspx?id=12149