0

When I decrypt the 5 levels RSA encrypted message the result isn't the original message although I use the right private keys and the right private keys

I have already tried playing with the integers and the amount of encryptions. It's working if I only encrypt once

import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
import ast

publickeys = []
privatekeys = []

for i in range (0,5):
    random_generator = Random.new().read
    key = RSA.generate(1024, random_generator)
    privatekeys.append(key)
    publickeys.append(key.publickey())

data = "ack"

for publickey in publickeys:
    data = publickey.encrypt(str(data), 32)

for i in range (1,len(privatekeys)+1):
    data = privatekeys[-i].decrypt(ast.literal_eval(str(tuple(data))))

There are no error messages but the results aren't what I expected. instead of getting the right message "ack" I am getting a weird string.

Wang Liang
  • 4,244
  • 6
  • 22
  • 45
Tomer Dwir
  • 11
  • 3

1 Answers1

0

Discard the None value returned by encrypt; it doesn't help you to keep it around, and greatly complicates decoding the encrypted string.

for publickey in publickeys:
    data, _ = publickey.encrypt(data, 32)

for privatekey in reversed(privatekeys):
    data = privatekey.decrypt(data)
chepner
  • 497,756
  • 71
  • 530
  • 681