-
Notifications
You must be signed in to change notification settings - Fork 0
/
recovery.py
59 lines (51 loc) · 1.51 KB
/
recovery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 23 12:09:07 2019
@author: [email protected]
"""
import hashlib
import json
import matplotlib.pyplot as plt
import seaborn as sns
import random
cipherBook = 'pre_processed_table.json'
with open(cipherBook,'r',encoding = 'utf-8') as f:
vectorDict = json.load(f)
m = hashlib.sha256()
for key in vectorDict:
m.update(str(vectorDict[key]).encode('utf-8'))
vectorDict[key] = int(m.hexdigest(),16)
vectorList = list(vectorDict.values())
wordList = list(vectorDict.keys())
total = 100
wordChooseList = random.sample(wordList,total)
ratioList = []
for n in range(1,257):
correct = 0
for word in wordChooseList:
maxLap = 0
originalHash = vectorDict[word]
j = random.sample(range(256),n)
temper = ''
for i in range(256):
if i in j:
temper += '1'
else:
temper += '0'
temperdHash = originalHash ^ int(temper,2)
pos = 0
maxPos = 0
for value in vectorList:
lap = 256 - bin(temperdHash^value).count('1')
if maxLap < lap:
maxLap = lap
maxPos = pos
pos += 1
restoredWord = wordList[maxPos]
if restoredWord == word:
correct += 1
ratio = correct/total
print('n=',n,' ratio=',ratio)
ratioList.append(ratio)
with open('recover.json','w',encoding='utf-8') as f:
json.dump(ratioList, f)