0

我正在尝试将二进制密文转换回文本,然后解密密文。但我不断收到以下错误: ValueError: invalid literal for int() with base 2: '[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()
4

0 回答 0