from utils import * from operator import itemgetter import bisect from Crypto.Cipher import DES def get_index(a, x): """Locate the leftmost value exactly equal to x in list a""" i = bisect.bisect_left(a, x) if i != len(a) and a[i] == x: return i else: return -1 def des_enc(k, m): """ Encrypt a message m with a key k using DES as follows: c = DES(k, m) Args: m should be a bytestring (i.e. a sequence of characters such as 'Hello' or '\x02\x04') k should be a bytestring of length exactly 8 bytes. Note that for DES the key is given as 8 bytes, where the last bit of each byte is just a parity bit, giving the actual key of 56 bits, as expected for DES. The parity bits are ignored. Return: The bytestring ciphertext c """ d = DES.new(k, DES.MODE_ECB) c = d.encrypt(m) return c def des_dec(k, c): """ Decrypt a message c with a key k using DES as follows: m = DES(k, c) Args: c should be a bytestring (i.e. a sequence of characters such as 'Hello' or '\x02\x04') k should be a bytestring of length exactly 8 bytes. Note that for DES the key is given as 8 bytes, where the last bit of each byte is just a parity bit, giving the actual key of 56 bits, as expected for DES. The parity bits are ignored. Return: The bytestring plaintext m """ d = DES.new(k, DES.MODE_ECB) m = d.decrypt(c) return m def main(): # Exercitiu pentru test des2_enc key1 = 'Smerenie' key2 = 'Dragoste' m1_given = 'Fericiti cei saraci cu duhul, ca' c1 = 'cda98e4b247612e5b088a803b4277710f106beccf3d020ffcc577ddd889e2f32' # TODO: implement des2_enc and des2_dec m1 = des2_dec(key1, key2, hex_2_str(c1)) print('ciphertext: ' + c1) print('plaintext: ' + m1) print('plaintext in hexa: ' + str_2_hex(m1)) # TODO: run meet-in-the-middle attack for the following plaintext/ciphertext m1 = 'Pocainta' c1 = '9f98dbd6fe5f785d' # in hex string m2 = 'Iertarea' c2 = '6e266642ef3069c2' # Note: you only need to search for the first 2 bytes of the each key: k1 = '??oIkvH5' k2 = '??GK4EoU' if __name__ == "__main__": main()