Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. ##kode til hellman tables aflevering
  2.  
  3. from Crypto.Cipher import AES
  4. import matplotlib.pyplot as plt
  5. from collections import defaultdict
  6. import random
  7. import numpy as np
  8. import math
  9. import time
  10. plt.style.use("seaborn-darkgrid")
  11. start_time = time.time()
  12.  
  13. def AES128Encrypt(key,plaintext):
  14. if len(key) < 16:
  15. missing = 16-len(key)
  16. for i in range(missing):
  17. key+= b'0'
  18. #print("PADDED KEY ",len(key))
  19. cipher = AES.new(key,AES.MODE_ECB)
  20. ciphertext = cipher.encrypt(plaintext)
  21. return ciphertext[0:3] #kun 24 bits, i.e. 3 bytes
  22.  
  23. NrOfTables = 256 #2^8
  24. plaintext_orig = bytearray(b'0123456789abcdef')
  25.  
  26. m = [2**11,2**10,2**9,2**8,2**7,2**6,2**5,2**4,2**3,2**2] #m værdier vi vil køre for
  27. mstore = []
  28. domainval = pow(2,24)-1
  29.  
  30. def PH_hellman(num_tables, l, m, t):
  31. return 1 - math.exp(-math.sqrt((2 * m * num_tables ** 2) / (2 ** l)) * (math.exp(math.sqrt((2 * m * t ** 2) / (2 ** l))) - 1) / (math.exp(math.sqrt((2 * m * t ** 2) / (2 ** l))) + 1))
  32.  
  33. #source:
  34. #https://stackoverflow.com/questions/21017698/converting-int-to-bytes-in-python-3
  35. def int_to_bytes(x: int) -> bytes:
  36. #x = x.item()
  37. return x.to_bytes((x.bit_length() + 7) // 8, 'big', signed = False)
  38. #source:
  39. #https://stackoverflow.com/questions/21017698/converting-int-to-bytes-in-python-3
  40. def int_from_bytes(xbytes: bytes) -> int:
  41. return int.from_bytes(xbytes, 'big',signed = False)
  42.  
  43. def fi(ciphertext,index):
  44. ciphertext = AES128Encrypt(ciphertext, plaintext_orig)
  45. temp1 = int_from_bytes(ciphertext)
  46. f_k_i = (temp1 + index) % pow(2,24)
  47. #print("RESULT OF FKI ",f_k_i)
  48. return f_k_i # returns integer
  49.  
  50. columns = 1000
  51.  
  52. for mvalue in m:
  53. currentValues = np.empty((NrOfTables, mvalue), dtype=int)
  54. storedvalues = set()
  55. columncoverages = []
  56. #print("RUNNING FOR VALUE M: ",mvalue)
  57. for t in range(columns):
  58. for tableNr in range(0,NrOfTables):
  59. for rows in range(0,mvalue):
  60. if t == 0:
  61. temprows = int_to_bytes(rows)
  62. f_k_i_result = fi(temprows, tableNr)
  63. currentValues[tableNr][rows] = f_k_i_result
  64. else:
  65. cipher_temp = int_to_bytes(int(currentValues[tableNr][rows]))
  66. f_k_i_result = fi(cipher_temp, tableNr)
  67. currentValues[tableNr][rows] = f_k_i_result
  68.  
  69. storedvalues.add(f_k_i_result)
  70. columncoverages.append((len(storedvalues) / domainval)*100)
  71.  
  72. mstore.append(columncoverages)
  73.  
  74. print("--- %s seconds ---" % (time.time() - start_time))
  75.  
  76.  
  77. fig1, ax1 = plt.subplots(1)
  78. for idx, columncoveragesin in enumerate(mstore):
  79. ax1.plot(range(t+1),columncoveragesin, label = 'm = '+str(m[idx]))
  80.  
  81. plt.legend(loc='upper left')
  82. plt.xlabel('Columns')
  83. plt.ylabel('Coverage [%]')
  84. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement