转换 RSA 密码格式 PKCS1 PKCS8

创建日期: 2024-07-18 14:20 | 作者: 风波 | 浏览次数: 18 | 分类: OpenSSL

来源:https://www.upsync.dev/2022/02/22/loading-a-rsa-private-key

Converting PKCS#1 RSA key to PKCS#8 format

  1. 生成 PKCS#1 格式的 RSA 秘钥文件
openssl genrsa -f4 -out rsa.key 2048

或者使用 python代码生成(pip install rsa -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

import rsa
(pubkey, privkey) = rsa.newkeys(512) # 
bindata = privkey.save_pkcs1()
with open(rsa.key, 'wb') as f:
    f.write(bindata)
  1. Convert the rsa.key to PKCS#8 format
openssl pkcs8 -topk8 -inform PEM -outform DER -nocrypt -in rsa.key -out rsa.key.der

此时 rsa.key.der 是二进制数据格式

  1. 转换为 PEM 格式
openssl rsa -in rsa.key.der -inform DER
18 浏览
0 评论