题目:
https://cryptopals.com/sets/1/challenges/6
思路:
Here‘s how:
this is a testand
wokka wokka!!!is 37. Make sure your code agrees before you proceed.
解:
def mdecode(x, key):
y = bytearray([ord(x[i]) ^ ord(key[i % len(key)]) for i in range(len(x))]).hex()
return y
def calc_bit_diff(s1, s2):
def calc_bit_diff_sub(i1, i2):
cnt = 0
for i in range(8):
if (1<<i) & (i1 ^ i2) != 0:
cnt += 1
return cnt
ans = 0
for i in range(len(s1)):
ans += calc_bit_diff_sub(ord(s1[i]), ord(s2[i]))
return ans
def calc_bit_diff2(s1, s2):
def calc_bit_diff_sub(i1, i2):
cnt = 0
for i in range(8):
if (1<<i) & (i1 ^ i2) != 0:
cnt += 1
return cnt
ans = 0
for i in range(len(s1)):
ans += calc_bit_diff_sub(s1[i], s2[i])
return ans
import base64
with open("f:\\6.txt") as fin:
txt = "".join([line.strip() for line in fin.readlines()])
bx = base64.b64decode(txt)
for keysz in range(2, 40):
print(keysz, calc_bit_diff2(bx[:keysz], bx[keysz: 2 * keysz]) * 1.0 / keysz, sum([calc_bit_diff2(bx[keysz * i:keysz * i + keysz], bx[: keysz]) for i in range(10)]) * 1.0 / keysz)
def try_solve(barr):
legal_chars = "01234567890abcdefghijklmnopgrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ ,.?!~‘><@\n#\r+=-*`_^)(:;\"[]{}\\%$&|Jjq/"
ans = []
for i in range(256):
y = [chr(i^sx) for sx in barr]
if len([c for c in y if c in legal_chars]) > 0.99 * len(barr):
ans.append((chr(i), len([c for c in y if c == ‘e‘])))
mx = max([a[1] for a in ans])
ans.sort(key=lambda x:-x[1])
print(ans)
return [ans[i][0] for i in range(len(ans)) if ans[i][1] == mx][0]
for keysz in range(29, 30):
keyarr = []
print(‘keysz‘, keysz)
for keyi in range(keysz):
subxt = [bx[i] for i in range(keyi, len(bx), keysz)]
keyarr.append(try_solve(subxt))
print(‘‘.join(keyarr))
感想:
keysz=29,这个实际上是硬暴力来的
其实还不完全,直接按照E的数目最多来拼是Terminator X: Br,ng thm noise
但实际上应该是Bring the noise
原文:https://www.cnblogs.com/xuesu/p/12000373.html