import Crypto.Util.strxor as xo import libnum, codecs, numpy as np
defisChr(x): iford('a') <= x and x <= ord('z'): returnTrue iford('A') <= x and x <= ord('Z'): returnTrue returnFalse
definfer(index, pos): if msg[index, pos] != 0: return msg[index, pos] = ord(' ') for x inrange(len(c)): if x != index: msg[x][pos] = xo.strxor(c[x], c[index])[pos] ^ ord(' ')
defknow(index, pos, ch): msg[index, pos] = ord(ch) for x inrange(len(c)): if x != index: msg[x][pos] = xo.strxor(c[x], c[index])[pos] ^ ord(ch)
dat = []
defgetSpace(): for index, x inenumerate(c): res = [xo.strxor(x, y) for y in c if x!=y] f = lambda pos: len(list(filter(isChr, [s[pos] for s in res]))) cnt = [f(pos) for pos inrange(len(x))] for pos inrange(len(x)): dat.append((f(pos), index, pos))
#这里传密文 c = [codecs.decode(x.strip().encode(), 'hex') for x inopen('Problem.txt', 'r').readlines()]
msg = np.zeros([len(c), len(c[0])], dtype=int)
getSpace()
dat = sorted(dat)[::-1] for w, index, pos in dat: infer(index, pos)
know(10, 21, 'y') #如果需要修正, 传入所在被改字母所在行和列,再输入替代的字母,例如alwa s 改为always know(8, 14, 'n')
print('\n'.join([''.join([chr(c) for c in x]) for x in msg]))
key = xo.strxor(c[0], ''.join([chr(c) for c in msg[0]]).encode()) print(key)
import Crypto.Util.strxor as xo import libnum, codecs, numpy as np
defisChr(x): iford('a') <= x and x <= ord('z'): returnTrue iford('A') <= x and x <= ord('Z'): returnTrue returnFalse
definfer(index, pos): if msg[index, pos] != 0: return msg[index, pos] = ord(' ') for x inrange(len(c)): if x != index: msg[x][pos] = xo.strxor(c[x], c[index])[pos] ^ ord(' ')
dat = []
defgetSpace(): for index, x inenumerate(c): res = [xo.strxor(x, y) for y in c if x!=y] f = lambda pos: len(list(filter(isChr, [s[pos] for s in res]))) cnt = [f(pos) for pos inrange(len(x))] for pos inrange(len(x)): dat.append((f(pos), index, pos))
#c = 密文 #c = [codecs.decode(x.strip().encode(), 'hex') for x in open('Problem.txt', 'r').readlines()]
msg = np.zeros([len(c), len(c[0])], dtype=int)
getSpace()
dat = sorted(dat)[::-1] for w, index, pos in dat: infer(index, pos)
print('\n'.join([''.join([chr(c) for c in x]) for x in msg]))
执行代码,得到的结果是:
1 2 3 4 5 6 7 8 9 10 11
Dear Friend, T%is tim< I u nderstood my m$stake 8nd u sed One time p,d encr ptio n scheme, I he,rd tha- it is the only en.ryptio7 met hod that is ma9hemati:ally proven to be #ot cra:ked ever if the ke4 is ke)t se cure, Let Me k#ow if ou a gree with me t" use t1is e ncryption sche e alwa s...
显然这不是最终结果,我们得修正几项。把 k#now 修复成 know,把 alwa s 修复成 always. 代码如下:
1 2 3 4 5 6 7 8 9 10
defknow(index, pos, ch): msg[index, pos] = ord(ch) for x inrange(len(c)): if x != index: msg[x][pos] = xo.strxor(c[x], c[index])[pos] ^ ord(ch)
print('\n'.join([''.join([chr(c) for c in x]) for x in msg]))
注意:空格也占位
结果得到:
1 2 3 4 5 6 7 8 9 10 11
Dear Friend, This time I u nderstood my mistake and u sed One time pad encryptio n scheme, I heard that it is the only encryption met hod that is mathematically proven to be not cracked ever if the key is kept se cure, Let Me know if you a gree with me to use this e ncryption scheme always...
例2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from secret import secret, key from Crypto.Util.strxor import strxor
defENC(m, k): c = [] for i inrange(len(m) // len(k)): tmp = m[i * len(k):(i + 1) * len(k)] c.append(strxor(tmp, key)) return c
import Crypto.Util.strxor as xo import libnum, codecs, numpy as np
defisChr(x): iford('a') <= x and x <= ord('z'): returnTrue iford('A') <= x and x <= ord('Z'): returnTrue returnFalse
definfer(index, pos): if msg[index, pos] != 0: return msg[index, pos] = ord(' ') for x inrange(len(c)): if x != index: msg[x][pos] = xo.strxor(c[x], c[index])[pos] ^ ord(' ')
defknow(index, pos, ch): msg[index, pos] = ord(ch) for x inrange(len(c)): if x != index: msg[x][pos] = xo.strxor(c[x], c[index])[pos] ^ ord(ch)
dat = []
defgetSpace(): for index, x inenumerate(c): res = [xo.strxor(x, y) for y in c if x!=y] f = lambda pos: len(list(filter(isChr, [s[pos] for s in res]))) cnt = [f(pos) for pos inrange(len(x))] for pos inrange(len(x)): dat.append((f(pos), index, pos))
#这里传密文 # c = [codecs.decode(x.strip().encode(), 'hex') for x in open('Problem.txt', 'r').readlines()] c = [] msg = np.zeros([len(c), len(c[0])], dtype=int)
getSpace()
dat = sorted(dat)[::-1] for w, index, pos in dat: infer(index, pos)
#know(10, 21, 'y') #如果需要修正, 传入所在被改字母所在行和列,再输入替代的字母,例如alwa s 改为always #know(8, 14, 'n')
print('\n'.join([''.join([chr(c) for c in x]) for x in msg]))
#key = xo.strxor(c[0], ''.join([chr(c) for c in msg[0]]).encode()) #print(key)
这里是未修正的结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Sometimes fate is like a small tand6torm that keeps changing dir1 ctions. Flag is NowUKnowMTP. Yor ch$nge direction but the sandst; rm chases you. You turn again, eut 1he storm adjusts. Over and o" er you play this out, like some'omi+ous dance with death just be2 ore dawn. Why? Because this stoum i6n't something that blew in f& om far away, something that has'not-ing to do with you. This sto& m is you. Something inside of yhu. o all you can do is give in o it, step right inside the stoum, &losing your eyes and pluggin3 up your ears so the sand doesn t g t in, and walk through it, s ep by step. There's no sun therb, n* moon, no direction, no sens1 of time. Just fine white sand twir)ing up into the sky like pul" erized bones. That's the kind oa sa+dstorm you need to imagine. nd once the storm is over, you pon'1 remember how you made it th& ough, how you managed to survivb. Y*u won't even be sure, whethe& the storm is really over. But hne 1hing is certain. When you co9 e out of the storm, you won't bb th same person who walked in.
修正后
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Sometimes fate is like a small tandstorm that keeps changing dir1 ctions. Flag is NowUKnowMTP. Yor change direction but the sandst; rm chases you. You turn again, eut the storm adjusts. Over and o" er you play this out, like some'ominous dance with death just be2 ore dawn. Why? Because this stoum isn't something that blew in f& om far away, something that has'nothing to do with you. This sto& m is you. Something inside of yhu. So all you can do is give in o it, step right inside the stoum, closing your eyes and pluggin3 up your ears so the sand doesn t get in, and walk through it, s ep by step. There's no sun therb, no moon, no direction, no sens1 of time. Just fine white sand twirling up into the sky like pul" erized bones. That's the kind oa sandstorm you need to imagine. nd once the storm is over, you pon't remember how you made it th& ough, how you managed to survivb. You won't even be sure, whethe& the storm is really over. But hne thing is certain. When you co9 e out of the storm, you won't bb the same person who walked in.