Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Crypto.Cipher import DES
- import matplotlib.pyplot as plt
- from collections import defaultdict
- import random
- import numpy as np
- import math
- def DESEncrypt(key,plaintext):
- cipher = DES.new(key,DES.MODE_ECB)
- ciphertext = cipher.encrypt(plaintext)
- return ciphertext
- def DESDecrypt(key,ciphertext):
- cipher = DES.new(key,DES.MODE_ECB)
- plaintext = cipher.decrypt(ciphertext)
- return plaintext
- def int_to_bytes(x: int) -> bytes:
- #x = x.item()
- return x.to_bytes((x.bit_length() + 7) // 8, 'big', signed = False)
- def int_from_bytes(xbytes: bytes) -> int:
- return int.from_bytes(xbytes, 'big',signed = False)
- def intArrayToByteArray(intArray):
- tempByteArray = bytearray()
- for i in range(len(intArray)):
- if (intArray[i] == 0):
- tempByteArray += b'0'
- tempByteArray += int_to_bytes(intArray[i])
- return tempByteArray
- key = [0]*8
- keyTable = []
- #This is creating all key and set it into a table
- for firstByte in range(0,128):
- for secondByte in range(0,128):
- for thirdByte in range(0,64):
- key[0] = firstByte
- key[1] = secondByte
- key[2] = thirdByte
- tempArray = intArrayToByteArray(key)
- keyTable.append(tempArray)
- key = [0]*8
- print(len(keyTable))
- print("Key table generation done")
- plaintext_orig = bytearray(b'01234567')
- ciphertext = bytearray(b':\xae%I\xd2t\xf8>')
- EncTable = []
- for key in keyTable:
- tempCipher = DESEncrypt(key, plaintext_orig)
- EncTable.append(tempCipher)
- print()
- print(len(EncTable))
- print("All ciphertext calculated")
- restoredKeyPair = []
- for key in keyTable:
- tempDecryptedValue = DESDecrypt(key, ciphertext)
- if tempDecryptedValue in EncTable:
- key1 = EncTable.index(tempDecryptedValue)
- key2 = key
- restoredKeyPair.append((key1, key2))
- print(restoredKeyPair)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement