XOR Python 文本加密/解密 [英] XOR Python Text Encryption/Decryption

查看:28
本文介绍了XOR Python 文本加密/解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一个可以在 Python 中导入的内置 xor 运算符.我正在尝试执行异或加密/解密.到目前为止,我有:

 def xor_attmpt():message = raw_input("请输入要加密的消息:")密码 = []对于我在消息中:cipher.append(bin(ord(i))[2::])#添加字母/字符的转换#in 你的消息从 ascii 到二进制,没有前面的 0b 到你的加密消息列表密码 = "".join(cipher)privvyKey = raw_input("请输入私钥:")密钥解密 = []对于 privvyKey 中的 j:keydecrypt.append(bin(ord(j))[2::]) #samekeydecrypt = "".join(keydecrypt )#sameprint "key is '{0}'" .format(keydecrypt) #substitute 字符串中的值打印加密文本为‘{0}’" .format(cipher)从运算符导入异或对于消息中的信件:打印异或(布尔(密码),布尔(密钥解密))

这个:

>对于消息中的信件:打印异或(布尔(密码),布尔(密钥解密))

是我的 python 开始出错的地方.

输出看起来像这样

 输入要加密的消息:hello输入私钥:@154>关键是'10111001000000110001110101110100111110'密文是 '11010001100101110110011011001101111'错误的错误的错误的错误的错误的

我搞砸的是试图比较这两个二进制文件(密钥和加密)并给出真(1)或假(为 0).然后,异或应该通过比较两者给我一个结果 1 和 0 二进制列表.任何输入?

解决方案

这是 的代码示例的变体XOR 密码维基百科文章:

def xor(data, key):return bytearray(a^b for a, b in zip(*map(bytearray, [data, key])))

示例(Python 2):

<预><代码>>>>one_time_pad = '共享秘密'>>>明文 = '未加密'>>>密文=异或(明文,one_time_pad)>>>密文bytearray(b'x06x06x04x1cx06x16Yx03x11x06x16')>>>解密 = xor(密文,one_time_pad)>>>解密bytearray(b'未加密')>>>明文== str(解密)真的

I know there is a built in xor operator that can be imported in Python. I'm trying to execute the xor encryption/decryption. So far I have:

 def xor_attmpt():
    message = raw_input("Enter message to be ciphered: ")
    cipher = []
    for i in message:
        cipher.append(bin(ord(i))[2::])#add the conversion of the letters/characters
#in your message from ascii to binary withoout the 0b in the front to your ciphered message list
    cipher = "".join(cipher) 
    privvyKey = raw_input("Enter the private key: ")
    keydecrypt = []
    for j in privvyKey:
        keydecrypt.append(bin(ord(j))[2::]) #same
    keydecrypt = "".join(keydecrypt )#same

    print "key is '{0}'" .format(keydecrypt) #substitute values in string
    print "encrypted text is '{0}'" .format(cipher)
    from operator import xor
    for letter in message:
        print xor(bool(cipher), bool(keydecrypt))

This:

>  for letter in message:
    print xor(bool(cipher), bool(keydecrypt))

is where my python starts to go wrong.

The ouput looks like this

    Enter message to be ciphered: hello
Enter the private key: @154>
key is '10111001000000110001110101110100111110'
encrypted text is '11010001100101110110011011001101111'
False
False
False
False
False

What I'm messing up on is trying to get these two binary(key and encrypted) to be compared and give true(1) or false(being 0). The xor should then give me a resulting 1 and 0 binary list from comparing the two. Any input?

解决方案

Here's a variation of the code example from XOR Cipher Wikipedia article:

def xor(data, key): 
    return bytearray(a^b for a, b in zip(*map(bytearray, [data, key]))) 

Example (Python 2):

>>> one_time_pad = 'shared secret' 
>>> plaintext = 'unencrypted' 
>>> ciphertext = xor(plaintext, one_time_pad) 
>>> ciphertext 
bytearray(b'x06x06x04x1cx06x16Yx03x11x06x16') 
>>> decrypted = xor(ciphertext, one_time_pad) 
>>> decrypted
bytearray(b'unencrypted')
>>> plaintext == str(decrypted)
True

这篇关于XOR Python 文本加密/解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆