I'm trying to implement Mixnet in Python. Consider the example in the linked wiki page — A
encrypts a message with A
's public key and then encrypts the resulting ciphertext along with B
's address with M
's public key.
When I run my code which attempts to do the above, I get ValueError: Plaintext is too long.
that's because I'm not appending the address B
the right way and I'm exceeding the 1024 size of RSA. How do I accomplish this with 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)