生成RSA密钥对
from Crypto.PublicKey import RSA
# 生成2048位的RSA密钥对
key = RSA.generate(2048)
# 获取私钥和公钥
private_key = key.export_key()
public_key = key.publickey().export_key()
print("Private Key:", private_key.decode())
print("Public Key:", public_key.decode())
密钥
Private Key: -----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAnxpYCbb4dyt9qalpaX7n9GWXSQztcGMJgO5LAlwyyEic06wg
02/B0DsV0WXjVHHQFtpXd2U6PxE6gcHSJydH91gSbGTfc21owIrqd/j5I8xgYVjp
MNvcwwaubujEv5O0aqqywPYhFkShRV3J7kXdcj30FpKtAz4fnj9lnpxicGCIPWJf
V96VSe1Q61tbXS1lhN8SC+NiB+CfEpsltAVmlzaYxfDjnI8Ns0ZvqvM+FRBw/6f0
34MnEl8Q0CWHqz/C7B6wHlx3pfrrBgfjJLMxDQNobJj64r1FRWTCZAKzbMUpzuMY
MZ6sVJL7DxXbZqzz3rywJ9X8/hjwwCHriSfMrQIDAQABAoIBAAMToVbewO3yW1sS
ju/z/Pb5l/QDr1EYY9tTFa7qqti7VBJhJmla+eaASKsPS1bGCf4ceXXc8UQS8zdb
JiiNAT6ZPLyiiWepWqM5NyYxca/tiIJEE+Or/MLmDYLQ4YJ1kBbtTMj1JC1Sum5Q
TVKxlddbDK37EXlvPsadTJEao79Z4WNxKM4YpxMe7rfOx/LEdP5qnSsHiWnYjuDv
OeHZEbRf01HKm2Cxjhs2LSXqwUSUCk+Kz6sj2NA+YwrzdcNLEbSJ8MSfrpehorqo
9CtP47wB4eL3Loeb2abfkiBD3SM8jlHLKJSCrj8zPgKnDvwAJ429/2uz8LStTTh3
UVYMggkCgYEAwegyaeDp/Otq/pReuWvOgA36y42lt5ve8tW4QjOsQxbEfypbLcyI
LirgcfZkWgJ+TJCMjlgPLdAhyAjz9AEwv4m6XVfBfHlhjKmUWUspovFi0Fgdu7Y7
n6LAl9InL24iABPPolSSEWTKiCBX8KVxUHygJ9RQ9EnLoYgAXsrKzlcCgYEA0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
DQNobJj64r1FRWTCZAKzbMUpzuMYMZ6sVJL7DxXbZqzz3rywJ9X8/hjwwCHriSfM
rQIDAQAB
-----END PUBLIC KEY-----
保存密钥到文件
# 将私钥和公钥保存到文件
with open('private.pem', 'wb') as f:
f.write(private_key)
with open('public.pem', 'wb') as f:
f.write(public_key)
加密和解密
from Crypto.PublicKey import RSA
# 生成2048位的RSA密钥对
key = RSA.generate(2048)
# 获取私钥和公钥
private_key = key.export_key()
public_key = key.publickey().export_key()
# 将私钥和公钥保存到文件
with open('private.pem', 'wb') as f:
f.write(private_key)
with open('public.pem', 'wb') as f:
f.write(public_key)
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
with open('public.pem','rb') as f:
public_key = RSA.import_key(f.read())
with open('private.pem','rb') as f:
private_key = RSA.import_key(f.read())
# 加密
message = "Bileton".encode("utf-8")
cipher_rsa = PKCS1_OAEP.new(public_key)
encrypted_message = cipher_rsa.encrypt(message).hex()
print(encrypted_message) # 每次生成的密文都不一样
# 解密
original_encrypted_message = bytes.fromhex(encrypted_message)
cipher_rsa_decrypt = PKCS1_OAEP.new(private_key)
decrypt_message = cipher_rsa_decrypt.decrypt(original_encrypted_message).decode("utf-8")
print(decrypt_message)
输出
800868d5564180ae3dbb88066622bb0f094dea6fc61320cdbe4f10a216f9edbf2e148de0116afbfff59f923b6309fee1fbfdc5b0de29d27d7c65b1367d4a8dd2b70ee2e7bb759f2a6f0bc14a815c61c44281cc89262c4de479dc1ad68e95e9245496c4422bd6af0104c2539cffecde13756258af51c33203651951a469a62ec7dd998a6a4f0179f9730bd9dcc916f65162382a0e2b19002690a0c00e97b1fed941e9f6ddee3f37a4c3a73e82ed86be2448d6a44a17a312436629d27fbb21f3495996ca2ee7d8b97f35c0b525cac9c1658b68b3d4e6a776b541853b7a6813ce9a762d42194934ea04a2e002093a5bbe002540a1b5341e006d73857804a82d292f
Bileton