解密问题(Python)

时间:2018-12-18 18:45:33

标签: python encryption routing tor

我正在尝试在python中创建一个洋葱路由器,在将每条消息发送到3个节点进行解密之前,每条消息将被加密3次。

在下面进行的测试中,我对消息进行了三次加密,但是解密后,我只会得到垃圾。我认为这可能与我将填充或编码/解码应用于字节的顺序有关。

 simpleMessage = "Hello!!!!!"
    CMD1 = "MSG"
    CMD2 = "FWD"
    CMD3 = "FWD"

    messagePackage = (CMD1, simpleMessage)
    messagePackage = str(messagePackage).encode('utf-8')
    messagePackage = self.padder(messagePackage)
    onionLayer1 = encryption.symmetricEncryption(key1[0], key1[1], messagePackage)

    messagePackage2 = (CMD2, onionLayer1)
    messagePackage2 = str(messagePackage2).encode('utf-8')
    messagePackage2 = self.padder(messagePackage2)

    onionLayer2 = encryption.symmetricEncryption(key2[0], key2[1], messagePackage2)

    messagePackage3 = (CMD3, onionLayer2)
    messagePackage3 = str(messagePackage3).encode('utf-8')
    messagePackage3 = self.padder(messagePackage3)

    onionLayer3 = encryption.symmetricEncryption(key3[0], key3[1], messagePackage3)

    onionLayerPeeled3 = encryption.symmetricDecrypt(key3[0], key3[1], onionLayer3)
    onionLayerPeeled2 = encryption.symmetricDecrypt(key2[0], key2[1], onionLayerPeeled3)
    onionLayerPeeled1 = encryption.symmetricDecrypt(key1[0], key1[1], onionLayerPeeled2)

The debugger shows the state of the message after each decryption

我认为问题在于我将每个洋葱层编码为字节数组,并且每次解密后都需要解码回utf-8字符串才能读取信息。

但是在解密之间实现以下代码行

onionLayerPeeled3 = onionLayerPeeled3.decode('utf-8')

导致以下错误

UnicodeDecodeError:“ utf-8”编解码器无法解码位置0:无效的起始字节中的字节0x99

非常感谢任何帮助/建议

0 个答案:

没有答案