生成RSA公钥和私钥对公钥进行base64编码
要生成RSA公钥和私钥,可以使用Python中的cryptography库。下面是一个示例代码:
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
import base64
# 生成RSA密钥对
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
# 获取公钥并进行base64编码
public_key = private_key.public_key()
public_key_pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
public_key_base64 = base64.b64encode(public_key_pem).decode('utf-8')
# 获取私钥
private_key_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
private_key_base64 = base64.b64encode(private_key_pem).decode('utf-8')
print("公钥(base64编码):")
print(public_key_base64)
print("私钥(base64编码):")
print(private_key_base64)
运行上述代码,将会输出生成的RSA公钥和私钥的base64编码形式
原文地址: http://www.cveoy.top/t/topic/i1Pl 著作权归作者所有。请勿转载和采集!