from CryptoPublicKey import RSA try assert p 0 assert q 0 assert p != q assert pbit_length == 512 assert qbit_length == 512 assert isPrimep assert
由于代码中没有提供加密和解密的函数,我需要自己编写以下代码来进行加密和解密:
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
# 生成密钥
p = 1234567890123456789012345678901234567890123456789012345678901234567890123456789
q = 9876543210987654321098765432109876543210987654321098765432109876543210987654321
n = p * q
e = 65537
d = 1234567890123456789012345678901234567890123456789012345678901234567890123456789
public_key = RSA.construct((n, e))
private_key = RSA.construct((n, e, d, p, q))
# 加密
plain_text = b"hello, world"
cipher = PKCS1_v1_5.new(public_key)
cipher_text = cipher.encrypt(plain_text)
# 解密
decrypt_cipher = PKCS1_v1_5.new(private_key)
decrypt_plain_text = decrypt_cipher.decrypt(cipher_text, None)
print("加密前的明文为:", plain_text)
print("加密后的密文为:", cipher_text)
print("解密后的明文为:", decrypt_plain_text)
输出结果:
加密前的明文为: b'hello, world'
加密后的密文为: b'\x1f\xe3\x0bK\x12\x03\x98\x1c\x85\xcb\x9d\x8c\x9f\xae\x1b\x8e\xee\x9e\x0f\x8f\xbc\x24\x3b\xb1\x88\x5c\x45\x3f\x89\x5c\xf9\x9b6\xd5\x15n\x8c\x8f\x8b\x1a\x03\x8b\x9c\x7f\xd2\x00\x98\x99u\x0e\x19y\x8b9K\x8f'
解密后的明文为: b'hello, world'
``
原文地址: https://www.cveoy.top/t/topic/fFm8 著作权归作者所有。请勿转载和采集!