0% found this document useful (0 votes)
31 views

Tutorial Python 02

The document discusses implementing Caesar cipher encryption and decryption in Python. It includes tasks to write functions for the cipher, extend the program to take command line arguments, and allow a custom alphabet mapping for more secure encryption.

Uploaded by

anidcohen9058
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Tutorial Python 02

The document discusses implementing Caesar cipher encryption and decryption in Python. It includes tasks to write functions for the cipher, extend the program to take command line arguments, and allow a custom alphabet mapping for more secure encryption.

Uploaded by

anidcohen9058
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Technische Universit at M unchen Institut f ur Informatik Tobias Neckel

GRS, June 2013

Python course - Tutorial 2


Greatest Common Divisor
To get more familiar with functions, write a function that computes the greates common divisor for two integers a, b N. Using a loop, determine the gcd. Now write a function that recursively determines the solution. You can make use of the following recursive formulation gcd(a, b) = a, if b=0 = gcd(b, a mod b), else

Caesars Cipher
One of the oldest known cryptographic schemes is called Caesars Cipher. It is named after Julius Caesar, as he has reportedly used this scheme to encrypt condential (military) messages. To this end, he shifted all letters in the alphabet three positions to the right. I.e., A is replaced by D, B by E, . . . Therefore, the alphabet is wrapped around: at the end of the alphabet, the next letter is A again. To conceal the regular structure of words in a text, whitespaces have to be deleted.

Implement the encryption and decryption algorithm for Caesars shift pattern. Delete all whitespaces in the original text and take care to output only either lowercase or uppercase letters. To this end, you might nd the following two functions useful: ord(c) returns the index of the character c in the ASCII-table, chr(n) the character at index n; chr(ord(c))==c.

2 Extend your Python program so that it has command-line parameters for a lename of a text le to encrypt/decrypt, an output lename to which the result is saved, the shift (allowing for arbitrary shifts), assuming 3 as default, and so that it provides help on the command-line parameters with -h and --help. If no lename is specied, read a text from standard input and write the result to standard output. This allows, e.g., the following usage: $ echo H e l l o Caesar | . / c a e s a r s . py KHOORFDHVDU Extend your program once more to a more secure encryption scheme. Allow a user to specify a custom (bijective) mapping from a-z to an arbitrary permutation of a-z. For example, one mapping could be source: ABCDEFGHIJKLMNOPQRSTUVWXYZ target: BSVIHJZDRGPKTQUWXLNYACOEMF With this mapping, a shift of 3 for character a leads to n by ABSN. Therefore, let the user specify the target permutation (in the example "BSVI...EMF") as a command line parameter. Hint: one possible way is to use the sequence types (lists, dictionaries, . . . ) to achieve this. Chocolate option! Extend your previous program so that it automatically tries to decrypt an encrypted text by nding out which shift to use. You can assume: That the text has been encrypted using the procedure mentioned above using Caesars Cipher with an arbitrary shift. That the text is in English! That the text contains no whitespaces. Send your result until 13:30 to [email protected] (or hand it in in some other way). The result working best on some texts we have gets a bar of chocolate. (Optional, if we already covered modules in the lecture:) If you like to practise how modules work, extend/rewrite your Python program, so that it can be both executed as a program and used as a module, providing the basic functionality such as encryption and decryption of a text.

You might also like