我正在尝试在Python中实现Mixnet。考虑链接的Wiki页面中的示例-A
使用A
的公钥加密消息,然后使用B
'加密生成的密文以及M
的地址。的公钥。
当我运行尝试执行上述操作的代码时,得到ValueError: Plaintext is too long.
是因为我没有以正确的方式附加地址B
并且超出了RSA的1024大小。如何使用RSA完成此操作?
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto import Random
def create_rsa():
random_generator = Random.new().read
key = RSA.generate(1024, random_generator) #generate pub and priv key
publickey = key.publickey() # pub key export for exchange
return key, publickey
def encrypt(message, key):
ciphertext = PKCS1_OAEP.new(key)
return ciphertext.encrypt(message)
message = "chocolate milk"
prA, A = create_rsa()
prB, B = create_rsa()
prM, M = create_rsa()
# A sealing stuff
Kb = encrypt(message, B)
KbAddress = Kb + "B"
Km = encrypt(KbAddress, M)