You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.6 KiB
40 lines
1.6 KiB
import os
|
|
from cryptography.hazmat.primitives import serialization
|
|
from cryptography.hazmat.primitives.asymmetric import rsa, ed25519
|
|
|
|
def generate_test_keys():
|
|
# 确保目录存在
|
|
os.makedirs("keys", exist_ok=True)
|
|
|
|
# 1. 生成接收方的 RSA 密钥对 (用于数字信封)
|
|
rsa_priv = rsa.generate_private_key(public_exponent=65537, key_size=2048)
|
|
with open("keys/receiver_rsa_priv.pem", "wb") as f:
|
|
f.write(rsa_priv.private_bytes(
|
|
encoding=serialization.Encoding.PEM,
|
|
format=serialization.PrivateFormat.PKCS8,
|
|
encryption_algorithm=serialization.NoEncryption()
|
|
))
|
|
with open("keys/receiver_rsa_pub.pem", "wb") as f:
|
|
f.write(rsa_priv.public_key().public_bytes(
|
|
encoding=serialization.Encoding.PEM,
|
|
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
|
))
|
|
|
|
# 2. 生成发送方的 Ed25519 密钥对 (用于数字签名)
|
|
ed_priv = ed25519.Ed25519PrivateKey.generate()
|
|
with open("keys/sender_ed25519_priv.pem", "wb") as f:
|
|
f.write(ed_priv.private_bytes(
|
|
encoding=serialization.Encoding.PEM,
|
|
format=serialization.PrivateFormat.PKCS8,
|
|
encryption_algorithm=serialization.NoEncryption()
|
|
))
|
|
with open("keys/sender_ed25519_pub.pem", "wb") as f:
|
|
f.write(ed_priv.public_key().public_bytes(
|
|
encoding=serialization.Encoding.PEM,
|
|
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
|
))
|
|
|
|
print("测试密钥已生成在 keys/ 目录下。")
|
|
|
|
if __name__ == "__main__":
|
|
generate_test_keys() |