Advertisement
DigitalMag

finding the best combination of constants in Diffie-Hellman

Jul 4th, 2020
1,485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import random
  2. import itertools
  3. import functools
  4. from collections import OrderedDict
  5.  
  6. class Private(object):
  7.  
  8.     def __init__(self, n, rest):
  9.         self.rest = rest
  10.         self.n = n
  11.  
  12.     statement = lambda self, x: self.n**x % self.rest
  13.     post_statement = lambda self, a, b: a**b % self.rest
  14.  
  15.  
  16. REST = 9
  17.  
  18. def cry_test(secure):
  19.  
  20.     skey = random.randint(0 ,REST)
  21.     open_skey = secure.statement(skey)
  22.  
  23.     # client:
  24.     rkey = random.randint(0, REST)
  25.     r_open_key = secure.statement(rkey)
  26.  
  27.     psw = secure.post_statement(open_skey, rkey)
  28.  
  29.     # server
  30.     spsw = secure.post_statement(r_open_key, skey)
  31.     # print psw
  32.  
  33.     return psw, skey, rkey
  34.  
  35.  
  36. def test(i,j):
  37.  
  38.     secure = Private(i,j)
  39.  
  40.     l = []; sk = []; rk = []
  41.     for i in range(100):
  42.         a,b,c = cry_test(secure)
  43.         l.append(a)
  44.  
  45.     return [l.count(c) for c in range(100)]
  46.  
  47.  
  48.  
  49. if __name__ == '__main__':
  50.     dct = {}
  51.     for i in range(1,18):
  52.         for j in range(1,998):
  53.             dct[(i,j)] = test(i,j)
  54.             print  max(dct[(i,j)]).__str__() + ' - ' + (i,j).__str__()
  55.  
  56.     rez = {k:max(v) for k, v in dct.iteritems()}
  57.  
  58.     items = list(rez.iteritems())
  59.     items.sort(key=lambda i: i[1])
  60.  
  61.  
  62.     od = OrderedDict()
  63.     for item in items:
  64.         od[str(item[1]) + '%'] = item[0]
  65.  
  66.     print od
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement