pkcs1与pkcs8格式RSA私钥互相转换

PKCS1(PEM)私钥生成

# openssl genrsa -out rsa_private_key.pem 2048

rsa_private_key.pem 文件内容如下:

-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA8Yf/PlCkJlG72pKWkx+ZKbTiS/hPmT1YtRz6d6bow0nROY5X
e+mGcLhrqNBfUGGsdLYVN6Xa+/YQYyvp9pXGMxmv5a22oFQXQhNX7+Gl0gE/kXKq
guIY0ztE+thmzabFl0+ZNR420la7cl13bJAdXybl8GBjFkXeUuZAkCWHN672JEuc
rMPi6IUTNB+/ksa0jsjge/ixrawm9Hz+XuF8xIYPILSaWixgFibpHR6lEVrSLCnE
aTbSSO7ZQW5areMe37qoUl4osep/eXXJCeCXgaBqExNa7n+LR5JFoMNCAtDsHSzP
Z2moARBLVh0AcyyYq7Xwx9dW5uQ8UlQlfYADXwIDAQABAoIBAH+6re7kcoqZOsDy
OJMBaNw3fl0yqdWeLaVJ9+MC76EnDnBjqDwtWR2PSc/yshUuMidei4XsPgx1KmFn
vcjbl/86HF1YKnZLQil9VUnUiP6wDk/61dV1gC9k7qvBeUxUX7YPMDR5EADYQTTP
3PIanI3PYlZmgsAqntI0Vfxk/EcjRpbGStGQ7yfXVwsNED6PaH63MTYXjP68V++2
wKhH4aNJW6a1qNtKVCnmEyNewFhEQT4EG/mUDphBWCA1AgK5XEH2gNBs+5UixN8h
uNNAznKxADDoolipf6laObcxSZPyU2SwyFYIsFfkuLH8tXsDtbdywc/kcbOLkSM5
wtXQZhECgYEA/YXc3YqXrTSFgSl8RwabfFDJQqjCq7P3963oQZkySvEB5NrYmqIy
c0ZrzuSkTPK0cUv9LXjiv5JB502IdlDEEzhyEvnJNqkaSAXmw5UakPsI9hCVu8vg
/hZOrlEDQXfnERzbfveYWOX9BG2S+d4X1qmd9HxFVb+keeEnYoU5VeUCgYEA8+Qj
t8raDV4H8LBUtPTw424OoFd7buuOZ8RWe0a1BAlMEL6ppCYopBy5wBwhJbyNoyyw
HO53f06Zy+43gsaaxrpdUZ69bFatx+bbYgQeMUhEyslbk1Y8sXu0o+6Q+WXwBUni
r90k+uVS02TE4o2gcfmFmyiOfdY9YEUo4Dkt3/MCgYBgXJccpecM8J+l/my8WmiT
EenNqnOD6/3eaH2VIXOaRIzhvcUC0Y+3fRbRJtrEXKP1RFrRA9e71Q2zqyapQf2l
CJljWQn/+rqAka2qvWvBm4lGbftbzBySt0vkueow4jAzsvJDCj0f3RsUqclpq98H
uqTNHscD1rnfkZL0wYqPeQKBgQCJmJaGJI2sqPY11mWTyfT1eqwJzhE3XPH0nRJl
bljFtDET6V1v3I9lN0ecmMIitiP6gUXCrM8CJegi43yx7TZ5E79+Qt5/ds+ITjhL
LiOSjkExXzSpBSgswawCKJ/l9JrbgPnBWjR40FRQKzPg1SJF6w5zoQfy+qGZ5KrR
BbFZ/QKBgQC40Fe4hDlouBtkX9aQO2gj0m4cAcuJkZ5bkoiWUjOCMK5opTC01/5q
VjQyWn7tjESggCXKrY7a+HTGMJJH/QM9yopVCI4kYXBoPO6s0R8jcGiTSnYRA/K7
uxjloGvgwqUxZhlwUXi6tct6gXEolNkPGDL5uUiQCV6gpBpih6l5pA==
-----END RSA PRIVATE KEY-----

PKCS1私钥转换为PKCS8(该格式一般Java调用)

# openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform pem -nocrypt -out rsa_private_key_pkcs8.pem

rsa_private_key_pkcs8.pem 文件内容如下:

-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDxh/8+UKQmUbva
kpaTH5kptOJL+E+ZPVi1HPp3pujDSdE5jld76YZwuGuo0F9QYax0thU3pdr79hBj
K+n2lcYzGa/lrbagVBdCE1fv4aXSAT+RcqqC4hjTO0T62GbNpsWXT5k1HjbSVrty
XXdskB1fJuXwYGMWRd5S5kCQJYc3rvYkS5ysw+LohRM0H7+SxrSOyOB7+LGtrCb0
fP5e4XzEhg8gtJpaL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2U3R5yYwiK2I/qB
RcKszwIl6CLjfLHtNnkTv35C3n92z4hOOEsuI5KOQTFfNKkFKCzBrAIon+X0mtuA
+cFaNHjQVFArM+DVIkXrDnOhB/L6oZnkqtEFsVn9AoGBALjQV7iEOWi4G2Rf1pA7
aCPSbhwBy4mRnluSiJZSM4IwrmilMLTX/mpWNDJafu2MRKCAJcqtjtr4dMYwkkf9
Az3KilUIjiRhcGg87qzRHyNwaJNKdhED8ru7GOWga+DCpTFmGXBReLq1y3qBcSiU
2Q8YMvm5SJAJXqCkGmKHqXmk
-----END PRIVATE KEY-----

PKCS8格式私钥转换为PKCS1(传统私钥格式)

# openssl rsa -in rsa_private_key_pkcs8.pem -out rsa_private_key_pkcs1.pem

rsa_private_key_pkcs1.pem 文件内容如下:

-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA8Yf/PlCkJlG72pKWkx+ZKbTiS/hPmT1YtRz6d6bow0nROY5X
e+mGcLhrqNBfUGGsdLYVN6Xa+/YQYyvp9pXGMxmv5a22oFQXQhNX7+Gl0gE/kXKq
guIY0ztE+thmzabFl0+ZNR420la7cl13bJAdXybl8GBjFkXeUuZAkCWHN672JEuc
rMPi6IUTNB+/ksa0jsjge/ixrawm9Hz+XuF8xIYPILSaWixgFibpHR6lEVrSLCnE
aTbSSO7ZQW5areMe37qoUl4osep/eXXJCeCXgaBqExNa7n+LR5JFoMNCAtDsHSzP
Z2moARBLVh0AcyyYq7Xwx9dW5uQ8UlQlfYADXwIDAQABAoIBAH+6re7kcoqZOsDy
OJMBaNw3fl0yqdWeLaVJ9+MC76EnDnBjqDwtWR2PSc/yshUuMidei4XsPgx1KmFn
vcjbl/86HF1YKnZLQil9VUnUiP6wDk/61dV1gC9k7qvBeUxUX7YPMDR5EADYQTTP
3PIanI3PYlZmgsAqntI0Vfxk/EcjRpbGStGQ7yfXVwsNED6PaH63MTYXjP68V++2
wKhH4aNJW6a1qNtKVCnmEyNewFhEQT4EG/mUDphBWCA1AgK5XEH2gNBs+5UixN8h
uNNAznKxADDoolipf6laObcxSZPyU2SwyFYIsFfkuLH8tXsDtbdywc/kcbOLkSM5
wtXQZhECgYEA/YXc3YqXrTSFgSl8RwabfFDJQqjCq7P3963oQZkySvEB5NrYmqIy
c0ZrzuSkTPK0cUv9LXjiv5JB502IdlDEEzhyEvnJNqkaSAXmw5UakPsI9hCVu8vg
/hZOrlEDQXfnERzbfveYWOX9BG2S+d4X1qmd9HxFVb+keeEnYoU5VeUCgYEA8+Qj
t8raDV4H8LBUtPTw424OoFd7buuOZ8RWe0a1BAlMEL6ppCYopBy5wBwhJbyNoyyw
HO53f06Zy+43gsaaxrpdUZ69bFatx+bbYgQeMUhEyslbk1Y8sXu0o+6Q+WXwBUni
r90k+uVS02TE4o2gcfmFmyiOfdY9YEUo4Dkt3/MCgYBgXJccpecM8J+l/my8WmiT
EenNqnOD6/3eaH2VIXOaRIzhvcUC0Y+3fRbRJtrEXKP1RFrRA9e71Q2zqyapQf2l
CJljWQn/+rqAka2qvWvBm4lGbftbzBySt0vkueow4jAzsvJDCj0f3RsUqclpq98H
uqTNHscD1rnfkZL0wYqPeQKBgQCJmJaGJI2sqPY11mWTyfT1eqwJzhE3XPH0nRJl
bljFtDET6V1v3I9lN0ecmMIitiP6gUXCrM8CJegi43yx7TZ5E79+Qt5/ds+ITjhL
LiOSjkExXzSpBSgswawCKJ/l9JrbgPnBWjR40FRQKzPg1SJF6w5zoQfy+qGZ5KrR
BbFZ/QKBgQC40Fe4hDlouBtkX9aQO2gj0m4cAcuJkZ5bkoiWUjOCMK5opTC01/5q
VjQyWn7tjESggCXKrY7a+HTGMJJH/QM9yopVCI4kYXBoPO6s0R8jcGiTSnYRA/K7
uxjloGvgwqUxZhlwUXi6tct6gXEolNkPGDL5uUiQCV6gpBpih6l5pA==
-----END RSA PRIVATE KEY-----

与 rsa_private_key.pem 一致

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/25/pkcs1-and-pkcs8-format-rsa-private-key-mutual-conversion/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
pkcs1与pkcs8格式RSA私钥互相转换
PKCS1(PEM)私钥生成 # openssl genrsa -out rsa_private_key.pem 2048 rsa_private_key.pem 文件内容如下: -----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQ……
<<上一篇
下一篇>>
文章目录
关闭
目 录