0

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)

tl;dr How do I do this with RSA?

captain
  • 815
  • 14
  • 26
  • You should decide if you are using `RSA encryption` (fixed plaintext length) or `block/stream cipher encryption with a key, that's passed along RSA encrypted`, which allows arbitrary length plaintext. – Dima Tisnek Jan 30 '19 at 02:08
  • @DimaTisnek what's an example of the latter? – captain Jan 30 '19 at 02:12
  • 1
    https://crypto.stackexchange.com/questions/14/how-can-i-use-asymmetric-encryption-such-as-rsa-to-encrypt-an-arbitrary-length – Dima Tisnek Jan 30 '19 at 02:16
  • Thanks, @DimaTisnek. So in the link, the solution is to transmit RSA encrypted symmetric key and AES encrypted plaintext. Is it normal to do this in production? Are there any public key cryptography algorithms that allow arbitrary length plaintext? – captain Jan 30 '19 at 19:11
  • 1
    IANAE, it seems that most (all?) production asymmetric cryptography is based on modulo arithmetics (RSA `m^e^d=m (mod n)`, Schnorr `g=h^r (mod p)`, EC over finite field, ElGamal modular mult. inverse) which means that an elementary operation is on an *integer* up to modulus. We just use bit- and byte-strings as representation of such integers. See answers to https://stackoverflow.com/questions/4371729/asymmetric-stream-cipher-with-short-plaintext-short-ciphertext for further leads and discussion. – Dima Tisnek Jan 31 '19 at 02:37
  • Thanks @DimaTisnek! – captain Feb 01 '19 at 19:57

0 Answers0