初始提交:文件传输系统基础框架
This commit is contained in:
@@ -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