简体   繁体   中英

Why isn't my 5 layer RSA encryption working properly? the decryption doesn't return the right value

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM