通过Anaconda openssl进行格式转换
一、下载及安装Anaconda
二、通过Anaconda的openssl对文件进行操作
1、将xxx.cer和xxxpfx文件转换成xxx.pem格式的文件
openssl pkcs12 -in xxx.pfx -nodes -out xxx.pem
openssl x509 -inform der -in xxx.cer -out xxx.pem
2、从xxx.pfx中提取密钥对
openssl pkcs12 -in xxx.pfx -nocerts -nodes -out private_pc.key
3、从密钥对中提取私钥(头部格式:-----BEGIN RSA PUBLIC KEY-----)
openssl rsa -in xxx.key -out private.pem
4、从密钥对提取公钥(头部格式:-----BEGIN RSA PRIVATE KEY-----)
openssl rsa -in private.pem -RSAPublicKey_out -out public.pem
5、从密钥对提取公钥(头部格式:-----BEGIN PUBLIC KEY-----)
openssl rsa -in private.key -pubout -out public.key
三、Python通过RSA加密
import rsa
# 生成密钥
(pubkey, privkey) = rsa.newkeys(1024)
# 保存密钥
with open('public.pem', 'w+') as f:
f.write(pubkey.save_pkcs1().decode())
with open('private.pem', 'w+') as f:
f.write(privkey.save_pkcs1().decode())
# 导入密钥
with open('public.pem', 'r') as f:
pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
with open('private.pem', 'r') as f:
privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
# 明文
message = '123123ssss'
# 公钥加密
crypto = rsa.encrypt(message.encode(), pubkey)
# print(crypto)
# 私钥解密
message = rsa.decrypt(crypto, privkey).decode()
print(message)