Differences

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

Link to this comparison view

ic [2016/10/03 23:47]
dan.dragan
ic [2020/09/26 18:32] (current)
acosmin.maria
Line 1: Line 1:
-===== Lab 01 - Introduction ​=====+===== Introducere în Criptologie ​=====
  
-==== Python Crash Course ====+Bine ați venit pe pagina cursului de Introducere în Criptologie.
  
-=== Python Interactive Mode === +În aceste pagini veți găsi resursele necesare pentru laborator
- +Găsiți videoclip-urile foarte utile ale lui Dan Boneh 
-Python is an easy to learn interactive programming language. Throughout this lab, we'll be using Python 2.7+[[https://www.youtube.com/watch?v=1bSjcU2GeG0&​list=PL58C6Q25sEEHXvACYxiav_lC2DqSlC7Og | aici]], 
- +[[https://www.youtube.com/watch?v=Rn7Y8kS7O7c | aici]] ș
-You can run Python code in two ways: +[[https://www.youtube.com/watch?v=40m3gcdGDu0 ​aici]].
-  * by writing a source file and running it (eventually,​ with some command line arguments) +
-  * by entering commands in interactive mode +
- +
-To start interactive mode, simply open a terminal window and run ''​python'':​ +
- +
-<​code>​ +
-linux$ python +
-</code> +
- +
-You can now write Python code just as you would in an actual program: +
- +
-<​code>​ +
->>>​ print "​Hello,​ SASC"​ +
-</code> +
- +
-To quit interactive mode either press ''​Ctrl+D''​ or use: +
- +
-<​code>​ +
->>>​ quit() +
-</​code>​ +
- +
-Interactive mode is useful when you want to quickly try something out (for example, testing decryption for a certain key) without going through the hassle of writing a new source file and running it. +
- +
- +
-=== Python Recipe #1 - Hail, Caesar! === +
- +
-In Python, indentation is important; it is used to organize code in blocksFor example, the following C code: +
- +
-<code C> +
-int foo(int a, int b) { +
-    int x; +
-    if (a > b) { +
-        x = a; +
-    } else { +
-        x = b; +
-    } +
-    return x +
-+
-</code> +
- +
-is (approximately) the same as the following in Python: +
- +
-<code Python>​ +
-def foo(a, b): +
-    if a > b: +
-        x +
-    else: +
-        x +
-    return x +
-</​code>​ +
- +
-In this first recipe we'll see how to encrypt a single letter using Caesar'​s cipher. +
- +
-<code Python>​ +
-alphabet='​ABCDEFGHIJKLMNOPQRSTUVWXYZ'​ +
-def caesar_enc(letter):​ +
-  if letter < '​A'​ or letter > '​Z':​ +
-    print '​Invalid letter'​ +
-    return +
-  else: +
-    return alphabet[(ord(letter) - ord('​A'​) + 3) % len(alphabet)] +
-</​code>​ +
- +
-Create a new file named caesar.py containing the code above. To test the code, open the interpreter and try the following:​ +
- +
-<code python>​ +
-linux$ python +
->>>​ from caesar import * +
->>>​ print alphabet +
->>>​ alphabet[0] +
->>>​ ord('​A'​) +
->>>​ len(alphabet) +
->>>​ ord('​D'​) - ord('​A'​) +
->>>​ 28 % 26 +
->>>​ -1 % 26 +
->>>​ caesar_enc('​D'​) +
->>>​ caesar_enc('​Z'​) +
->>>​ caesar_enc('​B'​) +
-</​code>​ +
- +
-=== Python Exercise #1 - Decrypting a letter === +
-Add a ''​caesar_dec''​ function to ''​caesar.py''​which decrypts a single letter encrypted using Caesar'​s cipher. +
- +
-=== Python Recipe #2 - Encrypting a string === +
- +
-We'll now expand our function to take string as input. First, a little intro to Python ''​for''​ loops. Some examples: +
- +
-<code python>​ +
-for i in [1, 2, 3]: +
-    print i +
-</​code>​ +
- +
-In this first example, ''​i''​ iterates through the values in list ''​[1, 2, 3]''​. The code is similar to the classical **C** code: +
- +
-<code C> +
-for (int i = 1; i <= 3; i++) { +
-    printf("​%d\n",​ i); +
-+
-</code> +
- +
-To create lists of integers use the ''​range''​ function: +
- +
-<code python>​ +
-for i in range(3): +
-    print i +
-</code> +
- +
-Execute the code above in the interpreterUsing the range function with two parameters, change the above to print integers 1 through 10. +
- +
-In Python, ''​for''​ can iterate through multiple types of objects, including strings. Try the below: +
- +
-<code python>​ +
-for letter in '​ABCD':​ +
-    print letter +
-</code> +
- +
-We will now use the ''​for''​ instruction to expand our ''​caesar_enc''​ function: +
- +
-<code Python>​ +
-alphabet='​ABCDEFGHIJKLMNOPQRSTUVWXYZ'​ +
-def caesar_enc_string(plaintext):​ +
-    ciphertext = ''​ +
-    for letter in plaintext:​ +
-        ciphertext = ciphertext + caesar_enc(letter) +
-    return ciphertext +
-     +
-</​code>​ +
- +
-Test the above by starting a new interpreter;​ this time, we'll skip the ''​from caesar import *''​ part by running the source file before starting interactive mode: +
- +
-<code Python>​ +
-linux$ python -caesar.py +
->>>​ test = '​HELLO'​ +
->>>​ test + '​WORLD'​ +
->>>​ caesar_enc_string(test) +
-</​code>​ +
- +
-Another way to run things, which can be very useful in general is to use a main() function and write your program script as follows: +
- +
-<code Python '​test_caesar.py'>​ +
-import sys +
-import random +
-import string +
-import operator +
- +
-alphabet='​ABCDEFGHIJKLMNOPQRSTUVWXYZ'​ +
- +
-def caesar_enc(letter):​ +
-  if letter < '​A'​ or letter > '​Z':​ +
-    print '​Invalid letter'​ +
-    return +
-  else: +
-    ​return alphabet[(ord(letter) - ord('​A'​) + 3) % len(alphabet)] +
- +
-def caesar_enc_string(plaintext): +
-    ciphertext = ''​ +
-    for letter in plaintext:​ +
-        ciphertext = ciphertext + caesar_enc(letter) +
-    return ciphertext +
- +
-def main(): +
-  m = '​BINEATIVENIT'​ +
-  c = caesar_enc_string(m) +
-  print c +
-   +
- +
-if __name__ == "​__main__":​ +
-  main() +
-</code> +
- +
-Then you can simply run the program, or type the following in a terminal: +
-<code Python>​ +
-python test_caesar.py +
-</code> +
- +
- +
-=== Python Exercise #2 - Decrypting a string === +
- +
-Add the corresponding ''​caesar_dec_string''​ function. +
- +
-=== Python Recipe #3 - Shift ciphers === +
- +
-Python allows passing default values to parameters:​ +
- +
-<code Python>​ +
-def foo(a, b = 3): +
-    print a, b +
-</​code>​ +
-<code Python>​ +
->>>​ foo(1) +
->>>​ foo(1, 2) +
-</​code>​ +
- +
-We can use default parameter values to expand our ''​caesar_enc''​ function to take the key as an additional parameter, without breaking compatibility with our previous code. +
- +
-<code Python>​ +
-def caesar_enc(letter,​ k = 3): +
-    if letter < '​A'​ or letter > '​Z':​ +
-        print '​Invalid letter'​ +
-        return None +
-    else: +
-        return alphabet[(ord(letter) - ord('​A'​) + k) % len(alphabet)] +
- +
-def caesar_enc_string(plaintext,​ k = 3): +
-    ciphertext = ''​ +
-    for letter in plaintext:​ +
-        ciphertext = ciphertext + caesar_enc(letter,​ k) +
-    return ciphertext +
-</code> +
- +
-To test the new functions, try the below: +
-<code Python>​ +
-linux$ python -i caesar.py +
->>>​ caesar_enc_string('​HELLO'​) +
->>>​ caesar_enc_string('​HELLO',​ 0) +
->>>​ caesar_enc_string('​HELLO',​ 1) +
-</​code>​ +
- +
- +
-=== Python Exercise #3 - Shift ciphers === +
- +
-Using default parameters, expand your shift cipher decryption functions to support arbitrary keys. +
- +
-==== Exercise 1 (5p) ==== +
- +
-Alice sends Bob the following ciphertexts:​ +
- +
-<​code>​ +
-LDPWKHORUGBRXUJRG +
-XNTRGZKKGZUDMNNSGDQFNCRADENQDLD +
-DTZXMFQQSTYRFPJDTZWXJQKFSDLWFAJSNRFLJ +
-SIOMBUFFHINNUEYNBYHUGYIZNBYFILXSIOLAIXCHPUCH +
-ERZRZOREGURFNOONGUQNLGBXRRCVGUBYL +
-CJIJPMTJPMAVOCZMVIYTJPMHJOCZM +
-DTZXMFQQSTYRZWIJW +
-ZPVTIBMMOPUDPNNJUBEVMUFSZ +
-FVBZOHSSUVAZALHS +
-KAGETMXXZAFSUHQRMXEQFQEFUYAZKMSMUZEFKAGDZQUSTNAGD +
-MCIGVOZZBCHRSGWFSOBMHVWBUHVOHPSZCBUGHCMCIFBSWUVPCIF +
-</​code>​ +
- +
-Charlie manages to capture the ciphertexts and he finds that the cipher used for +
-encryption is the shift cipher (each message possibly encrypted with a different +
-key). Can you decrypt the messages ​? +
- +
-Charlie also knows that the plaintext consists only of the English letters A to +
-Z (all capitals, no punctuation). +
- +
-==== Exercise 2 (5p) ==== +
- +
-Alice sends Bob another ciphertext, but much longer this time: +
- +
-{{:​sasc:​laboratoare:​sasc_msg_lab1.txt|Download message file}} +
- +
-Charlie needs to decrypt this as wellSome colleagues tell him this is encrypted +
-using the substitution cipher, and that again the plaintext consists only of the English letters **A** to **Z** (all capitals, no punctuation). Try to help Charlie to decrypt this. +
- +
-Hint: use the frequency analysis mechanisms we discussed in class. Note that the frequency of each letter does not map precisely. In particular, the most frequent two letters do match well with the given table, but the others are sometimes mixed. However, Charlie knows that the most frequent bi-grams are the following (from most frequent to less frequent):​ +
-**TH**, **HE**, **IN**, **OR**, **HA**, **ET**, **AN**, **EA**, **IS**, **OU**, **HI**, **ER**, **ST**, **RE**, **ND** +
- +
-With this information,​ can you tell what the ciphertext is about?+
  
ic.1475527625.txt.gz · Last modified: 2016/10/03 23:47 by dan.dragan
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