【问题标题】:How to break repeating-key XOR Challenge using Single-byte XOR cipher如何使用单字节异或密码打破重复密钥异或挑战
【发布时间】:2018-06-04 17:33:58
【问题描述】:

本题是关于challenge number 6set number 1"the cryptopals crypto challenges"的挑战。


挑战是:

There's a file 在这里。在使用重复密钥 XOR 加密后,它是 base64 的。

解密它。

之后是解密文件的步骤说明,一共有8个步骤。你可以在the site找到他们。


一段时间以来,我一直在努力解决这个挑战,但我正在努力完成最后两个步骤。尽管我已经解决了第 3 个挑战,并且它包含了这些步骤的解决方案。

注意:当然,前 6 步中可能存在错误,但在每一步后查看 print 后,它们似乎运行良好。


我的代码:

写于Python 3.6

为了不处理网络请求,因为这不是本次挑战的目的。我只是将文件的内容复制到了begging中的一个字符串中,你也可以在运行代码之前这样做。

import base64

# Encoding the file from base64 to binary
file = base64.b64decode("""HUIfTQsP...JwwRTWM=""")
print(file)
print()

# Step 1 - guess key size
KEYSIZE = 4


# Step 2 - find hamming distance - number of differing bits
def hamming2(s1, s2):
    """Calculate the Hamming distance between two bit strings"""
    assert len(s1) == len(s2)
    return sum(c1 != c2 for c1, c2 in zip(s1, s2))


def distance(a, b):  # Hamming distance
    calc = 0
    for ca, cb in [(a[i], b[i]) for i in range(len(a))]:
        bina = '{:08b}'.format(int(ca))
        binb = '{:08b}'.format(int(cb))
        calc += hamming2(bina, binb)
    return calc

# Test step 2
print("distance: 'this is a test' and 'wokka wokka!!!' =", distance([ord(c) for c in "this is a test"], [ord(c) for c in "wokka wokka!!!"]))  # 37 - Working
print()


# Step 3
key_sizes = []
# For each key size
for KEYSIZE in range(2, 41):
    # take the first KEYSIZE worth of bytes, and the second KEYSIZE worth of bytes -
    # file[0:KEYSIZE], file[KEYSIZE:2*KEYSIZE]
    # and find the edit distance between them
    # Normalize this result by dividing by KEYSIZE
    key_sizes.append((distance(file[0:KEYSIZE], file[KEYSIZE:2*KEYSIZE]) / KEYSIZE, KEYSIZE))
key_sizes.sort(key=lambda a: a[0])


# Step 4
for val, key in key_sizes:
    print(key, ":", val)
KEYSIZE = key_sizes[0][1]
print()


# Step 5 + 6
# Each line is a list of all the bytes in that index
splited_file = [[] for i in range(KEYSIZE)]
counter = 0
for char in file:
    splited_file[counter].append(char)
    counter += 1
    counter %= KEYSIZE
for line in splited_file:
    print(line)
print()


# Step 7
# Code from another level
# Gets a string and a single char
# Doing a single-byte XOR over it
def single_char_string(a, b):
    final = ""
    for c in a:
        final += chr(c ^ b)
    return final


# Going over all the bytes and listing the result arter the XOR by number of bytes
def find_single_byte(in_string):
    helper_list = []
    for num in range(256):
        helper_list.append((single_char_string(in_string, num), num))
    helper_list.sort(key=lambda a: a[0].count(' '), reverse=True)
    return helper_list[0]

# Step 8
final_key = ""
key_list = []
for line in splited_file:
    result = find_single_byte(line)
    print(result)
    final_key += chr(result[1])
    key_list.append(result[1])
print(final_key)
print(key_list)

输出:

b'\x1dB\x1fM\x0b\x0f\x02\x1fO\x13N<\x1aie\x1fI...\x08VA;R\x1d\x06\x06TT\x0e\x10N\x05\x16I\x1e\x10\'\x0c\x11Mc'

distance: 'this is a test' and 'wokka wokka!!!' = 37

5 : 1.2
3 : 2.0
2 : 2.5
.
.
.
26 : 3.5
28 : 3.5357142857142856
9 : 3.5555555555555554
22 : 3.727272727272727
6 : 4.0

[29, 15, 78, 31, 19, 27, 0, 32, ... 17, 26, 78, 38, 28, 2, 1, 65, 6, 78, 16, 99]
[66, 2, 60, 73, 1, 1, 30, 3, 13, ... 26, 14, 0, 26, 79, 99, 8, 79, 11, 4, 82, 59, 84, 5, 39]
[31, 31, 19, 26, 79, 47, 17, 28, ... 71, 89, 12, 1, 16, 45, 78, 3, 120, 11, 42, 82, 84, 22, 12]
[77, 79, 105, 14, 7, 69, 73, 29, 101, ... 54, 70, 78, 55, 7, 79, 31, 88, 10, 69, 65, 8, 29, 14, 73, 17]
[11, 19, 101, 78, 78, 54, 100, 67, 82, ... 1, 76, 26, 1, 2, 73, 21, 72, 73, 49, 27, 86, 6, 16, 30, 77]

('=/n?3; \x00\x13&-,>1...r1:n\x06<"!a&n0C', 32)
('b"\x1ci!!>ts es(ogg ...5i<% tc:. :oC(o+$r\x1bt%\x07', 32)
('??:<+6!=ngm2i4\x0byD...&h9&2:-)sm.a)u\x06&=\x0ct&~n +=&*4X:<(3:o\x0f1<mE gy,!0\rn#X+\nrt6,', 32)
('moI.\'ei=Et\'\x1c:l ...6k=\x1b m~t*\x155\x1ei+=+ts/e*9$sgl0\'\x02\x16fn\x17\'o?x*ea(=.i1', 32)
('+3Enn\x16Dcr<$,)\x01...i5\x01,hi\x11;v&0>m', 32)
 
[32, 32, 32, 32, 32]

请注意,在将密钥打印为字符串时,您看不到它,但其中有 5 个字符。


这不是正确的答案,因为您可以在第四部分看到 - 在 XOR 之后,结果看起来不像单词......可能是最后两个函数的问题,但我无法弄清楚。

我也尝试了一些其他的长度,这似乎不是问题。


所以我要的不是修复我的代码,我想自己解决这个挑战:)。我想让你告诉我我错在哪里?为什么?我应该如何继续?

感谢您的帮助。

【问题讨论】:

  • 这里有一个文件。它在使用重复密钥 XOR 加密后进行了 base64 处理。 — 请指出您的代码中删除 base64 编码的部分。
  • 谢谢,现在已修复,我对文件进行了编码,但还不能正常工作。 @squeamishossifrage
  • 对密文进行简单的频率分析应该可以正常工作。频率最高的字母是 e。将密文中出现频率最高的字符与e 进行异或运算,这就是你的异或密钥。
  • 好吧,我做了一些与第 7 部分非常相似的事情,但使用 char ' ' 并且它有效,所以这不是问题。但是谢谢,这听起来像是一个更好的方法,也许我会在编辑下添加它。 @jww

标签: python-3.x cryptography xor


【解决方案1】:

经过大量思考和检查得出的结论是问题出在第 3 步。结果不够好,因为我只看了前两个块。

我修复了代码,因此它将根据所有块计算KEYSIZE

步骤 3 的代码现在如下所示:

# Step 3
key_sizes = []
# For each key size
for KEYSIZE in range(2, 41):
    running_sum = []
    for i in range(0, int(len(file) / KEYSIZE) - 1):
        running_sum.append(distance(file[i * KEYSIZE:(i + 1) * KEYSIZE],
                                     file[(i + 1) * KEYSIZE:(i + 2) * KEYSIZE]) / KEYSIZE)
    key_sizes.append((sum(running_sum)/ len(running_sum), KEYSIZE))
key_sizes.sort(key=lambda a: a[0])

感谢任何试图提供帮助的人。

【讨论】:

    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    相关资源
    最近更新 更多