question:
A common security method used for online banking is to ask the user for three random characters from a passcode. For example, if the passcode was 531278, they may ask for the 2nd, 3rd, and 5th characters; the expected reply would be: 317.
The text file, keylog.txt, contains fifty successful login attempts.
Given that the three characters are always asked for in order, analyse the file so as to determine the shortest possible secret passcode of unknown length.
翻译后:
银行登陆时,需要输入一个类似验证码的字符串,这个字符串是从一个密码中产生的,如:密码为531278,其第2,3,5位产生的是317.现在有一个密码产生了keylog.txt中的50组正确的验证码,求满足此条件的最短密码是?
##
答案1 (纯python)
#!/usr/bin/env python import itertools s1 = open('keylog.txt').read().split('\n')[:-1] s2 = list(set(''.join(s1))) l = [''.join(i) for i in itertools.permutations(s2,len(s2))] for x in l: num = 0 for y in s1: tmp=[] for s in y: tmp.append(x.index(s)) if tmp == sorted(tmp): num += 1 else: break if num == len(s1): print x
##
答案2 (利用graphviz)
graphviz是一个神奇的工具,具体介绍请看http://wiki.woodpecker.org.cn/moin/GraphVizForMoin
步骤一:生成数据
f = open(‘keylog.txt’)
f2 = open(‘out.dot’,’a’)
f2.write(‘digraph G {\n’)
for line in f:
f2.write(‘->’.join(list(line.strip())) +’;\n’)
f2.write(‘}\n’)
f2.close()
f.close()
步骤二:画图
dot -Tpng out.dot -o out.png (需要安装graphviz)