【问题标题】:XOR Python Text Encryption/DecryptionXOR Python 文本加密/解密
【发布时间】:2020-12-24 17:05:59
【问题描述】:

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

 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))

这个:

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

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

输出看起来像这样

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

我正在搞砸的是试图让这两个二进制文件(密钥和加密)进行比较并给出真(1)或假(0)。然后 xor 应该通过比较两者给我一个结果 1 和 0 二进制列表。有什么意见吗?

【问题讨论】:

  • 你的私钥在 Python 中是如何表示的?整数,字节串,还有别的吗?最终,您正在查看^ 运算符进行按位异或,但您需要确保您的参数是兼容的。

标签: python python-2.7 encryption


【解决方案1】:

这是来自XOR Cipher Wikipedia article 的代码示例的变体:

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) 
>>> 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

【讨论】:

  • 这将截断数据,因此您需要像 itertools.cycle 这样的密钥。
  • @HTF 没有。想想,为什么这个变量叫one_time_pad。明确地说,如果 len(data)>len(key) 可能会引发 ValueError
【解决方案2】:

下面的代码可以双向工作,不需要长度检查,因为使用了循环。

from itertools import cycle, izip
cryptedMessage = ''.join(chr(ord(c)^ord(k)) for c,k in izip(message, cycle(key)))

【讨论】:

  • 使用 Python 3 的内置 zip
【解决方案3】:
somecode = 'asdfln3j34tnonfdkjnflksdfnla'
message = 'this is my message'

def str_xor(s1, s2):
 return "".join([chr(ord(c1) ^ ord(c2)) for (c1,c2) in zip(s1,s2)])

encoded = str_xor(message, somecode)
# encoded == '\x15\x1b\r\x15L\x07@J^MT\x03\n\x1d\x15\x05\x0c\x0f'
decoded = str_xor(encoded, somecode)
# decoded == 'this is my message'

这是一个没有错误检查的幼稚/简约的实现。这里需要 len(somecode) >= len(message)。

【讨论】:

  • 谢谢。即使这没有错误检查,在我看来也是一个解决方案! :D 一个问题,"".join 到底是做什么的?
  • "".join(['a','b','c']) == 'abc'
  • 哦。关于python,我在多个地方都看到了这一点。谢谢
  • 吹毛求疵,但我会使用"".join(chr(ord(s1) ^ ord(s2)) for (s1, s2) in zip(somecode, message)) - zip 通常比range(len(...)) 更适合于锁步迭代两个序列。尽管使用ziprange 调用,请注意如果序列长度不同,您希望发生什么。
  • 彼得:是的,我同意。答案固定。
【解决方案4】:

对于 Python 3

from itertools import cycle

cryptedMessage = ''.join(chr(ord(c)^ord(k)) for c,k in zip(message, cycle(key)))

【讨论】:

    猜你喜欢
    • 2014-03-10
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    相关资源
    最近更新 更多