XOR编码实现

The cryptopals crypto challenges

可以使用大多数主流语言,解决问题
环境:Python3.7.0
项目地址:http://cryptopals.com/sets/1/challenges/2

Fixed XOR

1.png
例如:EVA and LAN

  • 1 - 将字符串–>二进制

    1
    2
    3
    4
    5
    6
    7
    ---ASCII/hex中直接到二进制---
    E = 69d = 0x45 = 01000101
    V = 86d = 0x56 = 01010110
    A = 65d = 0x41 = 01000001
    L = 76d = 0x4c = 01001100
    A = 65d = 0x41 = 01000001
    N = 78d = 0x4e = 01001110
  • 2 - XOR操作逻辑表如下:
    2.png
    总结XOR(异或)一种逻辑运算,仅当输入不同时才输出ture

    1
    2
    3
    4
    5
    6
    7
    8
    9
    ---对两对字符串进行异或处理---
    01000101 01010110 01000001 XOR'd against
    01001100 01000001 01001110
    ---结果---
    00001001 00010111 00001111
    ---转换回ascii对应纯文本结果---
    00001001 = 0x09 = HT(水平定位符号)
    00010111 = 0x23 = ETB(区块传输结束)
    00001111 = 0x15 = SI(启用变换(Shift in))
  • 3 - —测试—

    1
    2
    3
    4
    >>> ord(b'E') ^ ord(b'L')
    9
    >>> chr(ord(b'E') ^ ord(b'L')).encode()
    b'\t'

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import binascii

def XOR_byte_date(byte_1,byte_2):
XOR_bytes = b''

for b1,b2 in zip(byte_1,byte_2):
XOR_bytes += (bytes([b1 ^ b2]))
return XOR_bytes
def main():
byte_table_1 = bytes.fromhex('1c0111001f010100061a024b53535009181c')
byte_table_2 = bytes.fromhex('686974207468652062756c6c277320657965')
print(binascii.hexlify(XOR_byte_date(byte_table_1,byte_table_2)).decode())

if __name__ == '__main__':
main()

3.png

到此结束

如有错误请联系lanbaidetanlang@qq.com

文章目录
  1. 1. The cryptopals crypto challenges
    1. 1.1. Fixed XOR
  2. 2. 到此结束