Compare commits
3
Commits
master
..
20e7142e01
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20e7142e01 | ||
|
|
4044970b69 | ||
|
|
2a343ec2cd |
@@ -0,0 +1,38 @@
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
|
||||
# 生成私钥
|
||||
private_key = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=2048,
|
||||
backend=default_backend()
|
||||
)
|
||||
|
||||
# 获取公钥
|
||||
public_key = private_key.public_key()
|
||||
|
||||
# 序列化私钥
|
||||
private_pem = private_key.private_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PrivateFormat.PKCS8,
|
||||
encryption_algorithm=serialization.NoEncryption()
|
||||
)
|
||||
|
||||
# 序列化公钥
|
||||
public_pem = public_key.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
)
|
||||
|
||||
# 保存私钥到文件
|
||||
with open('private_key.pem', 'wb') as f:
|
||||
f.write(private_pem)
|
||||
|
||||
# 保存公钥到文件
|
||||
with open('public_key.pem', 'wb') as f:
|
||||
f.write(public_pem)
|
||||
|
||||
print("RSA密钥对生成成功!")
|
||||
print("私钥已保存到: private_key.pem")
|
||||
print("公钥已保存到: public_key.pem")
|
||||
Reference in New Issue
Block a user