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

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

问题描述

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

  def xor_attmpt():
message = raw_input(输入要加密的消息: )
cipher = []
为消息中的i
cipher.append(bin(ord(i))[2 ::])#添加字母/字符$ b的转换$ b#在你的消息从ascii到二进制的前面的0b到你的加密的消息列表
cipher =.join(cipher)
privvyKey = raw_input(输入私钥:)
keydecrypt = []
在privvyKey中的j:
keydecrypt.append(bin(ord(j))[2 ::])#same
keydecrypt =。 (keydecrypt)#same

打印键是'{0}'.format(keydecrypt)#substitute值在字符串
打印加密的文本是{0}.format (密码)
从操作符导入xor
消息中的字母:
print xor(bool(cipher),bool(keydecrypt))
pre>

This:

 >消息中的信件:
print xor(bool(cipher),bool(keydecrypt))

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



ouput看起来像这样

 输入要加密的消息:hello 
输入私钥:\ @ 154>
键是'10111001000000110001110101110100111110'
加密文本是'11010001100101110110011011001101111'
False
False
False
False
False

我正在搞砸的是试图让这两个二进制(密钥和加密)进行比较,并给出真(1 )或假(为0)。那么xor应该给我一个比较两个的结果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 ='shared secret'
>>>> plaintext ='unencrypted'
>>>> ciphertext = xor(plaintext,one_time_pad)
>>>密文
bytearray(b'\x06\x06\x04\x1c\x06\x16Y\x03\x11\x06\x16')
>>> ; decryptpted = xor(ciphertext,one_time_pad)
>>>
bytearray(b'unencrypted')
>>>明文== str(解密)
True


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'\x06\x06\x04\x1c\x06\x16Y\x03\x11\x06\x16') 
>>> decrypted = xor(ciphertext, one_time_pad) 
>>> decrypted
bytearray(b'unencrypted')
>>> plaintext == str(decrypted)
True

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

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