Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ic:labs:05 [2023/10/01 23:26]
razvan.smadu [Exercițiul 3 (5p)]
ic:labs:05 [2023/10/09 23:22] (current)
razvan.smadu
Line 3: Line 3:
 În acest laborator vom face niște exerciții ce folosesc algoritmul DES și variații ale acestuia discutate la [[https://​drive.google.com/​file/​d/​1Fjybv6k5QudRB1bkAi5shVUGlUyTO_0U/​view|curs]]. În acest laborator vom face niște exerciții ce folosesc algoritmul DES și variații ale acestuia discutate la [[https://​drive.google.com/​file/​d/​1Fjybv6k5QudRB1bkAi5shVUGlUyTO_0U/​view|curs]].
 Prezentarea PowerPoint pentru acest laborator poate fi găsită [[https://​drive.google.com/​file/​d/​1FU422fCHM24fRnMuzFd0OjhQqT6AQZ3X/​view?​usp=sharing|aici]]. Prezentarea PowerPoint pentru acest laborator poate fi găsită [[https://​drive.google.com/​file/​d/​1FU422fCHM24fRnMuzFd0OjhQqT6AQZ3X/​view?​usp=sharing|aici]].
-Puteți lucra acest laborator folosind ​și platforma Google Colab, accesând [[https://​colab.research.google.com/​drive/1Vi5V7mHYIau6vbGFiQITd2FRzYRT3o1j?​usp=sharing|acest]] link.+Puteți lucra acest laborator folosind platforma Google Colab, accesând [[https://​colab.research.google.com/​github/ACS-IC-labs/​IC-labs/​blob/​main/​labs/​lab05/​lab5.ipynb|acest]] link.
  
 +<​hidden>​
 <spoiler Click pentru a vedea utils.py>​ <spoiler Click pentru a vedea utils.py>​
 <file python utils.py>​ <file python utils.py>​
Line 501: Line 502:
  
 def des2_enc(k1:​ bytes, k2: bytes, m: bytes) -> bytes: def des2_enc(k1:​ bytes, k2: bytes, m: bytes) -> bytes:
-    # TODO 3.B: implement des2_enc+    # TODO B.1: implement des2_enc
     raise NotImplementedError("​Not implemented"​)     raise NotImplementedError("​Not implemented"​)
  
  
 def des2_dec(k1:​ bytes, k2: bytes, c: bytes) -> bytes: def des2_dec(k1:​ bytes, k2: bytes, c: bytes) -> bytes:
-    # TODO 3.B: implement des2_dec+    # TODO B.2: implement des2_dec
     raise NotImplementedError("​Not implemented"​)     raise NotImplementedError("​Not implemented"​)
  
Line 517: Line 518:
     c2 = "​54826ea0937a2c34d47f4595f3844445520c0995331e5d492f55abcf9d8dfadf"​     c2 = "​54826ea0937a2c34d47f4595f3844445520c0995331e5d492f55abcf9d8dfadf"​
  
-    # TODO: Convert k1, k2, c1, and c2 to bytes. It may make the exercise+    # TODO C: Decrypt c1 and c2 using k1 and k2, and make sure that 
 +    #           ​des2_dec(k1,​ k2, c1 || c2) == m1 || m2 
 + 
 +    # TODO C.1: Convert k1, k2, c1, and c2 to bytes. It may make the exercise
     # easier to implement. Use string_to_bytes() for plain texts (i.e., string     # easier to implement. Use string_to_bytes() for plain texts (i.e., string
     # in human-readable format), and bytes.fromhex() for hex strings.     # in human-readable format), and bytes.fromhex() for hex strings.
Line 525: Line 529:
     c2 = ...     c2 = ...
  
-    # TODO 3.C: Decrypt c1 and c2 using k1 and k2, and make sure that 
-    #           ​des2_dec(k1,​ k2, c1 || c2) == m1 || m2 
-    # 
     # NOTE: The code to decrypt c1 is already provided below. You **need**     # NOTE: The code to decrypt c1 is already provided below. You **need**
     # to decrypt c2 as well.     # to decrypt c2 as well.
-    # 
     m1 = bytes_to_string(des2_dec(k1,​ k2, c1))     m1 = bytes_to_string(des2_dec(k1,​ k2, c1))
     assert m1 == m1_given, f'​Expected "​{m1_given}",​ but got "​{m1}"'​     assert m1 == m1_given, f'​Expected "​{m1_given}",​ but got "​{m1}"'​
Line 538: Line 538:
     print("​plaintext in hexa:",​ str_2_hex(m1))     print("​plaintext in hexa:",​ str_2_hex(m1))
  
 +    # TODO C.2: Decrypt m2 similar to m1. Keep in mind that des_dec()
 +    # returns bytes
     m2 = ...     m2 = ...
  
Line 544: Line 546:
     print("​plaintext in hexa:",​ str_2_hex(m2))     print("​plaintext in hexa:",​ str_2_hex(m2))
  
 +    # TODO C.3: Just to make sure you implemented the task correctly:
 +    #           ​des2_dec(k1,​ k2, c1 || c2) == m1 || m2
     m12 = ...     m12 = ...
     assert m12 == m1 + m2, f'​Expected "​{m12}"​ to equal "{m1 + m2}"'​     assert m12 == m1 + m2, f'​Expected "​{m12}"​ to equal "{m1 + m2}"'​
Line 549: Line 553:
  
 def mitm() -> None: def mitm() -> None:
-    # TODO 3.D: run meet-in-the-middle attack for the following +    # TODO D: run meet-in-the-middle attack for the following plaintext/​ciphertext
-    # plaintext/​ciphertext+
     m1 = "​Pocainta"​     m1 = "​Pocainta"​
     c1 = "​9f98dbd6fe5f785d"​     c1 = "​9f98dbd6fe5f785d"​
Line 556: Line 559:
     c2 = "​6e266642ef3069c2"​     c2 = "​6e266642ef3069c2"​
  
-    # TODO: Convert m1, m2, c1, and c2 to bytes. It may make the exercise+    # TODO D.1: Convert m1, m2, c1, and c2 to bytes. It may make the exercise
     # easier to implement. Use string_to_bytes() for plain texts (i.e., string     # easier to implement. Use string_to_bytes() for plain texts (i.e., string
     # in human-readable format), and bytes.fromhex() for hex strings.     # in human-readable format), and bytes.fromhex() for hex strings.
Line 564: Line 567:
     c2 = ...     c2 = ...
  
-    # NOTE: you only need to search for the first 2 bytes of the each key (i.e., +    # NOTE: You only need to search for the first 2 bytes of the each key (i.e., 
-    # to find out what are the values for each `?`)+    # to find out what are the values for each `?`).
     k1 = "??​oIkvH5"​     k1 = "??​oIkvH5"​
     k2 = "??​GK4EoU"​     k2 = "??​GK4EoU"​
 +
 +    # TODO D.2: Generate the table containing (k2, DES(k2, m1)), for every
 +    # possible k2. Use a List[Tuple[bytes,​ bytes]] for the table (see task
 +    # description above).
 +
 +    # TODO D.3: Sort the table based on the ciphertexts. Extract the ciphertexts
 +    # in a list  (see task description above).
 +
 +    # TODO D.4: Perform binary search for all possible k1, such that .
 +    # Save the set of candidate keys (k1, k2) in a list.
 +
 +    # TODO D.5: From the set of candidate keys, print the ones matching
 +    # the second constraint: 2DES(k1, k2, m2) == c2
  
  
Line 576: Line 592:
  
 if __name__ == "​__main__":​ if __name__ == "​__main__":​
-    main() +    main()</​file>​
-</​file>​+
 </​spoiler>​ </​spoiler>​
 +</​hidden>​
ic/labs/05.1696192001.txt.gz · Last modified: 2023/10/01 23:26 by razvan.smadu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0