SHOW:
|
|
- or go back to the newest paste.
1 | import sys | |
2 | import numpy as np | |
3 | import random | |
4 | import os.path | |
5 | from simplecrypt import encrypt,decrypt | |
6 | from Crypto.Hash import SHA | |
7 | from Crypto.Util import number | |
8 | ||
9 | prime_value_q = number.getPrime(160) | |
10 | maxFeatures=0 | |
11 | stDev=2 | |
12 | mean=10 | |
13 | history_file_name="" | |
14 | newUser = True | |
15 | h = 5 | |
16 | ti =10 | |
17 | ||
18 | class Poly: | |
19 | coeff = [] | |
20 | def __init__(self, coeff): | |
21 | self.coeff = coeff | |
22 | ||
23 | def val(self, x): | |
24 | sum_val = 0 | |
25 | for i, value in enumerate(self.coeff): | |
26 | sum_val = sum_val + value*pow(x, i) | |
27 | return sum_val | |
28 | ||
29 | def poly_gen(m_minustwo,hpwd): | |
30 | coefficient = [hpwd] + random.sample(xrange(100), m_minustwo) | |
31 | return Poly(coefficient) | |
32 | ||
33 | def alphaTrue(pswd, i, coeffList): | |
34 | i=i+1 | |
35 | #Get SHA | |
36 | sha = SHA.new(str(2*i) + pswd).hexdigest() | |
37 | #Format SHA from str to long | |
38 | fmtSHA = long(''.join([str(ord(h)) for h in sha])) | |
39 | #calc Alpha Value | |
40 | A=coeffList.val(2*i) + (fmtSHA % prime_value_q) | |
41 | #print "ALPHA ", coeffList.val(2*i) | |
42 | #print "ALPHA %s %s %s %s" % (i, (2 * i), pswd, fmtSHA) | |
43 | return A | |
44 | ||
45 | def alphaFalse(pswd, i): | |
46 | pswd=pswd + str(random.randrange(0, 1000)) | |
47 | i=i + 1 | |
48 | coeffList = poly_gen(maxFeatures - 5, random.randrange(0, prime_value_q - 1)) | |
49 | # Get SHA | |
50 | sha = SHA.new(str(2 * i) + pswd).hexdigest() | |
51 | # Format SHA from str to long | |
52 | fmtSHA = long(''.join([str(ord(h)) for h in sha])) | |
53 | # calc Alpha Value | |
54 | A = coeffList.val(2 * i) + (fmtSHA % prime_value_q) | |
55 | return A | |
56 | ||
57 | def betaTrue(pswd, i, coeffList): | |
58 | i=i+1 | |
59 | #Get SHA | |
60 | sha = SHA.new(str(2*i+1) + pswd).hexdigest() | |
61 | #print "SHA:",sha | |
62 | #Format SHA from str to long | |
63 | fmtSHA = long(''.join([str(ord(h)) for h in sha])) | |
64 | #calc Beta Value | |
65 | B=coeffList.val(2*i+1) + (fmtSHA % prime_value_q) | |
66 | #print "BETA ", coeffList.val(2*i+1) | |
67 | #print "BETA %s %s %s %s" % (i, (2 * i + 1), pswd, fmtSHA) | |
68 | return B | |
69 | ||
70 | def betaFalse(pswd, i): | |
71 | pswd=pswd + str(random.randrange(0, 1000)) | |
72 | i=i + 1 | |
73 | coeffList = poly_gen(maxFeatures - 5, random.randrange(0, prime_value_q - 1)) | |
74 | # Get SHA | |
75 | sha = SHA.new(str(2 * i+1) + pswd).hexdigest() | |
76 | # Format SHA from str to long | |
77 | fmtSHA = long(''.join([str(ord(h)) for h in sha])) | |
78 | # calc Beta Value | |
79 | B = coeffList.val(2 * i+1) + (fmtSHA % prime_value_q) | |
80 | return B | |
81 | ||
82 | def instrTblGenerator(pswd, featuresList): | |
83 | instrTbl=[] | |
84 | avgFeatureSpeed = np.mean(featuresList, axis=0) | |
85 | sigma = np.std(featuresList, axis=0) | |
86 | hpwd = random.randrange(0, prime_value_q - 1) | |
87 | #print "encrypt " , hpwd | |
88 | coeffList = poly_gen(maxFeatures - 1, hpwd) | |
89 | ||
90 | ||
91 | ||
92 | for i in range(maxFeatures): | |
93 | ||
94 | ||
95 | if ((i < len(avgFeatureSpeed)) and ((abs(avgFeatureSpeed[i] - mean) - 0.0001) > (stDev * sigma[i]))): | |
96 | #print "mean:%s avg spd:%s"%(mean,avgFeatureSpeed[i]) | |
97 | if (avgFeatureSpeed[i] < mean): | |
98 | # Fast: beta=Random alpha=TRUE | |
99 | #print "ALPHA FAST" | |
100 | ||
101 | A=alphaTrue(pswd, i, coeffList) | |
102 | B = betaFalse(pswd, i) | |
103 | instrTbl.append([A, B]) | |
104 | # print "Alpha:%s Beta: %s"%(A,B) | |
105 | ||
106 | else:# SLOW: beta=TRUE alpha=Random | |
107 | ||
108 | #print "BETA FAST" | |
109 | ||
110 | A = alphaFalse(pswd, i) | |
111 | B = betaTrue(pswd, i, coeffList) | |
112 | instrTbl.append([A, B]) | |
113 | # print "Alpha:%s Beta: %s" % (A, B) | |
114 | ||
115 | ||
116 | else:# Neither: beta=TRUE alpha=TRUE | |
117 | ||
118 | #print "mean:%s avg spd:%s" % (mean, avgFeatureSpeed[i-1]) | |
119 | #print "SAME" | |
120 | A = alphaTrue(pswd, i, coeffList) | |
121 | B = betaTrue(pswd, i, coeffList) | |
122 | instrTbl.append([A, B]) | |
123 | #print "Alpha:%s Beta: %s\n" % (A, B) | |
124 | ||
125 | ||
126 | return [hpwd, instrTbl] | |
127 | ||
128 | #CHANGE UP | |
129 | def DecryptFromFile(key): | |
130 | with open(history_file_name+'.hf', 'rb') as f: | |
131 | cipher_text = f.read() | |
132 | pad, plain_text = decrypt(key, cipher_text).split("````") | |
133 | return plain_text | |
134 | ||
135 | def hist_gen(hpwd,featuresList): | |
136 | features2str = '' | |
137 | ||
138 | for i in xrange(1, len(featuresList)): | |
139 | features2str += ','.join([str(j) for j in featuresList[i]]) + '\n' | |
140 | paddedfeatures2str = "````" + features2str | |
141 | strhpwd=SHA.new(str(hpwd)).hexdigest() | |
142 | ciphertext = encrypt(strhpwd, paddedfeatures2str.rjust(800, '^')) | |
143 | file = open((history_file_name+".hf"), "w") | |
144 | file.write(ciphertext) | |
145 | file.close() | |
146 | ||
147 | ||
148 | '''def file_parser(inFile): | |
149 | global maxFeatures | |
150 | i=0 | |
151 | featuresList =[] | |
152 | while i < len(inFile): | |
153 | pswd=inFile[i] | |
154 | i = i + 1 | |
155 | #convert features from string to int | |
156 | features = map(int,inFile[i].split(',')) | |
157 | ||
158 | if (len(pswd) - 2 != len(features)): | |
159 | ||
160 | print 'Feature and password do not match!' | |
161 | sys.exit() | |
162 | maxFeatures = len(pswd) - 1 | |
163 | featuresList.append(features) | |
164 | i = i + 1 | |
165 | return pswd,featuresList''' | |
166 | #CHANGE UP | |
167 | def lamb(x_array,i): | |
168 | mult = 1 | |
169 | for j in range(len(x_array)): | |
170 | if j!=i: | |
171 | mult = mult*(x_array[j]/(x_array[j]-x_array[i])) | |
172 | return mult | |
173 | ||
174 | #CHANGE UP | |
175 | #this is our Lagrange function, which takes x,y pairs and q to recreate hpwd | |
176 | def Lagrange(x_array, y_array): | |
177 | sum = 0 | |
178 | for i in range(len(y_array)): | |
179 | #this calls the lambda function which creates uses the x values to create the coefficient to multiply y with | |
180 | lam = lamb(x_array,i) | |
181 | #this is where we sum the array | |
182 | sum = sum + (lam * y_array[i]) | |
183 | return sum | |
184 | ||
185 | # lagrange interpolation to get h_pwd from xy values | |
186 | def h_pwdLagrange(x,y, feature_num): | |
187 | h_pwd = 0 | |
188 | nums = [] | |
189 | dens = [] | |
190 | dens_sum = 1 | |
191 | for i in xrange(0, feature_num): | |
192 | lambda_num = 1 | |
193 | lambda_den = 1 | |
194 | for j in xrange(0, feature_num): | |
195 | if (i != j): | |
196 | lambda_num *= x[j] | |
197 | lambda_den *= x[j] - x[i] | |
198 | nums.append(lambda_num * y[i]) | |
199 | dens.append(lambda_den) | |
200 | for i in xrange(0, len(nums)): | |
201 | h_pwd += get_Num(i, nums, dens) | |
202 | dens_sum *= dens[i] | |
203 | return h_pwd/dens_sum # floor division to avoide float conversion | |
204 | ||
205 | #used to minimize the divisions | |
206 | def get_Num(index, nums, dens): | |
207 | num = 1 | |
208 | for i in xrange(0, len(nums)): | |
209 | if i == index: | |
210 | num *= nums[i] | |
211 | else: | |
212 | num *= dens[i] | |
213 | return num | |
214 | ||
215 | ||
216 | def login(features,instrTbl,password): | |
217 | ys=[] | |
218 | alphas = [] | |
219 | betas = [] | |
220 | xs = [] | |
221 | # parse the instruction table to list of alpha and list of beta | |
222 | i = 1 | |
223 | j = 0 | |
224 | # password = alpha if features < ti , password = beta if features>=ti | |
225 | ||
226 | ||
227 | for f in features: | |
228 | if f < ti: | |
229 | sha = SHA.new(str(2 * i) + password).hexdigest() | |
230 | sha = long(''.join([str(ord(h)) for h in sha])) | |
231 | p = instrTbl[j][0] - sha % prime_value_q | |
232 | #print "Decrypt alph ", p | |
233 | #print "Decrypt alph %s %s %s %s" %( i, (2 * i), password, sha) | |
234 | ys.append(p) | |
235 | xs.append(2*i) | |
236 | ||
237 | else: | |
238 | sha = SHA.new(str(2 * i + 1) + password).hexdigest() | |
239 | sha = long(''.join([str(ord(h)) for h in sha])) | |
240 | p = instrTbl[j][1] - sha % prime_value_q | |
241 | #print "Decrypt beta ", p | |
242 | #print "Decrypt beta %s %s %s %s" % (i, (2 * i+1), password, sha) | |
243 | ys.append(p) | |
244 | xs.append(2*i+1) | |
245 | i = i + 1 | |
246 | j = j + 1 | |
247 | hpwd = h_pwdLagrange(xs, ys,maxFeatures) | |
248 | #print "decreypt " , hpwd | |
249 | featuresList = [] | |
250 | try: | |
251 | inst = DecryptFromFile(SHA.new(str(hpwd)).hexdigest()).strip() | |
252 | ||
253 | instList = inst.split("\n") | |
254 | #print "hist ", instList | |
255 | if len(instList) == 6: | |
256 | instList = instList[1:] | |
257 | #print instList | |
258 | for i in instList: | |
259 | features = i.split(',') | |
260 | features = map(int, features) | |
261 | featuresList.append(features) | |
262 | #print featuresList | |
263 | return featuresList | |
264 | #features = content[i].split(',') | |
265 | # print features | |
266 | #features = map(int, features) | |
267 | ||
268 | except: | |
269 | return featuresList | |
270 | ||
271 | ||
272 | ||
273 | ||
274 | ||
275 | ||
276 | ||
277 | '''def main(inFileName,hisfilename): | |
278 | global history_file_name, newUser | |
279 | history_file_name=hisfilename | |
280 | ||
281 | if os.path.exists((hisfilename+".hf")): | |
282 | newUser=False | |
283 | else: | |
284 | with open(inFileName, 'r') as inFile: | |
285 | ||
286 | pswd, featuresList = file_parser(inFile.readlines()) | |
287 | print pswd, featuresList | |
288 | if not newUser: | |
289 | ||
290 | login(pswd) | |
291 | else: | |
292 | hpwd, instrTbl = instrTblGenerator(pswd, featuresList) | |
293 | hist_gen(hpwd, featuresList)''' | |
294 | ||
295 | ||
296 | def main(filename): | |
297 | global maxFeatures, history_file_name | |
298 | featuresList=[] | |
299 | instrTbl=[] | |
300 | try: | |
301 | with open(filename, 'r') as f: | |
302 | content = f.read() | |
303 | #this is to handle the header!!! delete this if its not neccessary | |
304 | #content = content.split("\outl0\strokewidth0 \strokec2 ")[1][:-1] | |
305 | ######### | |
306 | content = content.split() | |
307 | except: | |
308 | print("filename: " + filename + " is incorrect!") | |
309 | return | |
310 | i = 0 | |
311 | j = 1 | |
312 | while i < len(content): | |
313 | password = content[i] | |
314 | maxFeatures = len(password) - 1 | |
315 | i = i + 1 | |
316 | features = content[i].split(',') | |
317 | #print features | |
318 | features = map(int, features) | |
319 | ||
320 | #print "ooooo ", j | |
321 | #print "features ", features | |
322 | #print "Feature new ", featuresList | |
323 | if j <= h: | |
324 | #print "features lest ", len(featuresList) | |
325 | featuresList.append(features) | |
326 | ||
327 | print "1" | |
328 | if j == h: | |
329 | #print "i equals to h " | |
330 | #create_inst | |
331 | hpwd, instrTbl = instrTblGenerator(password, featuresList) | |
332 | #creat_hist | |
333 | history_file_name = filename.split(".")[0] | |
334 | hist_gen(hpwd, featuresList) | |
335 | if j > h: | |
336 | #print "feature list size ", len(featuresList) | |
337 | #print "i is fuck " ,i | |
338 | # if length of login return m_features is not 0 | |
339 | #and then we are going to pass m_features to make a new hist and inst | |
340 | newFeaturesList = login(features,instrTbl,password) | |
341 | ||
342 | if len(newFeaturesList) != 0: | |
343 | print("1") | |
344 | featuresList = newFeaturesList | |
345 | featuresList.append(features) | |
346 | hpwd, instrTbl = instrTblGenerator(password, featuresList) | |
347 | hist_gen(hpwd, featuresList) | |
348 | else: | |
349 | print("0") | |
350 | ||
351 | i = i + 1 | |
352 | j = j + 1 | |
353 | ||
354 | if __name__ == '__main__': | |
355 | main(sys.argv[1:][0]) |