密文在onetimepad中,将二进制密文转换回文本

2024-04-27 09:52:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将二进制密文转换回文本,然后破译密文。但我一直得到一个错误: ValueError:基为2的int()的文本无效:'[0,'

你能看看我的代码,看看是怎么回事吗

    def onetimepad():
    plaintext = str(input("please input plaintext string: "))
    key = input("please input key: ")

    #convert string to binary
    # printing original string
    print("The original string is : " + str(plaintext))

    # using join() + ord() + format()
    # Converting String to binary
    plaintext_bi = ''.join(format(ord(i), '08b') for i in plaintext)

    # printing result
    print("The PlainText after binary conversion : " + str(plaintext_bi))

    

    if (len(plaintext_bi) != len(key)):
        #length of plaintext is not the same length of the key so break
        print("ERROR : Plaintext is not the same size of Key")
    
    else: 
        # if the length is correct then xor 
        cipher_bi = [ord(a) ^ ord(b) for a,b in zip(plaintext_bi,key)]
        print("Binary Ciphertext: "+ str(cipher_bi))

        #convert binary to ciphertext
        ascii_string = "".join([chr(int(binary, 2)) for binary in str(cipher_bi).split(" ")])
        print(ascii_string)
           
def main():
    onetimepad()
main()