Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. from Crypto.Cipher import DES
  2. import matplotlib.pyplot as plt
  3. import time
  4.  
  5. def DESEncrypt(key,plaintext):
  6. cipher = DES.new(key,DES.MODE_ECB)
  7. ciphertext = cipher.encrypt(plaintext)
  8. return ciphertext
  9.  
  10. def DESDecrypt(key,ciphertext):
  11. cipher = DES.new(key,DES.MODE_ECB)
  12. plaintext = cipher.decrypt(ciphertext)
  13. return plaintext
  14.  
  15. #source
  16. #https://stackoverflow.com/questions/21017698/converting-int-to-bytes-in-python-3
  17. def int_to_bytes(x: int) -> bytes:
  18. return x.to_bytes((x.bit_length() + 7) // 8, 'big', signed = False)
  19. #source
  20. #https://stackoverflow.com/questions/21017698/converting-int-to-bytes-in-python-3
  21. def int_from_bytes(xbytes: bytes) -> int:
  22. return int.from_bytes(xbytes, 'big',signed = False)
  23.  
  24. def intArrayToByteArray(intArray):
  25. tempByteArray = bytearray()
  26. for j in range(len(intArray)):
  27. if intArray[j] == 0:
  28. tempByteArray += b'0'
  29. tempByteArray += int_to_bytes(intArray[j])
  30.  
  31. return tempByteArray
  32.  
  33.  
  34.  
  35. keyTable = []
  36.  
  37. start_time = time.time()
  38.  
  39. #Creates a table with all possible keys.
  40. for firstByte in range(0,128):
  41. for secondByte in range(0,128):
  42. for thirdByte in range(0,64):
  43. key = [0] * 8
  44. key[0] = firstByte << 1
  45. key[1] = secondByte << 1
  46. key[2] = thirdByte << 1
  47. tempArray = intArrayToByteArray(key)
  48. keyTable.append(tempArray)
  49.  
  50.  
  51.  
  52. print("Key Table Gen. Finished: Exec. Time: %s" % (time.time() - start_time))
  53.  
  54.  
  55. #byte1 = [1,1,1,1,1,1,1,0] # 0 (parity bit)
  56. byte1_int = 254
  57. byte2_int = 254
  58. byte3_int = 126
  59. key1 = bytearray(b'')
  60. key1.append(byte1_int)
  61. key1.append(byte2_int)
  62. key1.append(byte3_int)
  63. for i in range(5):
  64. key1 += b'0'
  65. print("key1 bytearray: {} \nkey1 hex: {}".format(key1,key1.hex()))
  66.  
  67. key2 = bytearray(b'')
  68. byte1_int_key2 = 170
  69. byte2_int_key2 = 170
  70. byte3_int_key2 = 42
  71. key2.append(byte1_int_key2)
  72. key2.append(byte2_int_key2)
  73. key2.append(byte3_int_key2)
  74. for i in range(5):
  75. key2 += b'0'
  76.  
  77. print("key2 bytearray: {} \nkey2 hex: {}".format(key2,key2.hex()))
  78.  
  79. plaintext_orig = bytearray(b'01234567')
  80. print("Plaintext: ",plaintext_orig.hex())
  81. ciphertext = DESEncrypt(key2,DESEncrypt(key1,plaintext_orig))
  82.  
  83. EncTable = dict()
  84. for key in keyTable:
  85. tempCipher = DESEncrypt(key, plaintext_orig)
  86. EncTable[tempCipher] = key
  87.  
  88. print("All ciphertext calculated!")
  89.  
  90. restoredKeyPair = []
  91.  
  92. for key in keyTable:
  93. tempDecryptedValue = DESDecrypt(key, ciphertext)
  94. if tempDecryptedValue in EncTable.keys():
  95. key1 = EncTable[tempDecryptedValue]
  96. key2 = key
  97. restoredKeyPair.append((key1, key2))
  98. key1 = 0
  99. key2 = 0
  100.  
  101. print("EXECUTION DONE --- TIME: --- %s seconds ---" % (time.time() - start_time))
  102. print("RETRIEVED KEY PAIR: ",restoredKeyPair)
  103. print("RETRIVEVED KEY PAIR: ",restoredKeyPair[0][0].hex(),restoredKeyPair[0][1].hex())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement