Sorting Encryption Systems CSC121, Introduction to Computer Programming We have seen earlier that Python supports the sorting of lists with the built- in.sort( ) method >>> a = [ 5, 2, 3, 1, 4 ] >>> a.sort( ) >>> a [ 1, 2, 3, 4, 5 ] Sorting There is also the sorted( ) function that builds a new sorted list from an iterable >>> d = { 4 : D, 3 : B, 1 : E, 5 : B, 2 : A } >>> d {5: 'B', 2: 'A', 3: 'B', 1: 'E', 4: 'D'} >>> sorted(d.items( )) [(1, 'E'), (2, 'A'), (3, 'B'), (4, 'D'), (5, 'B')] Reverse Sorting The operator module provides tools that permit more exotic forms of sorting the itemgetter( ) function can be used to pick out which item to sort the reverse flag can also be used to create descending sort orders 1
: print the list of students in descending order by age from operator import itemgetter tuples: Name, Grade, Age students = [ ("John", "A", 10), ("Jane", "A", 12), ("Dave", "B", 15) ] def reversesorttuples(tuples): list = sorted(tuples, key=itemgetter(2), reverse=true) print list from operator import itemgetter students = [ ("John", "A", 10), ("Jane", "A", 12), ("Dave", "B", 15) ] def reversesorttuples(tuples): list = sorted(tuples, key=itemgetter(2), reverse=true) print list identify which item to base the sort descending sort indicated >>> reversesorttuples(students) [('Dave', 'B', 15), ('Jane', 'A', 12), ('John', 'A', 10)] Cryptography and Cryptosystems Cryptography and Cryptosystems cryptography is the study of secret codes a cryptosystem is a system that provides the means for transmitting and receiving secure messages that are shared only by intended agents computer networks have made cryptosystems and cryptography a necessity for secure commercial transactions 2
Email SMTP is a messaging protocol (like other application layer protocols here it is simulated using a telnet session $ telnet example.org 25 S: 220 example.org ESMTP Sendmail 8.13.1/8.13.1; Wed, 30 Aug 20xx 07:36:42-0400 C: HELO mailout1.phrednet.com S: 250 example.org Hello ip068.subnet71.gci- net.com [216.183.71.68], pleased to meet you C: MAIL FROM:xxxx@example.com S: 250 2.1.0 <xxxx@example.com>... Sender ok C: RCPT TO:yyyy@example.com S: 250 2.1.5 <yyyy@example.com>... Recipient ok C: DATA S: 354 Enter mail, end with "." on a line by itself From: Dave\r\nTo: Test Recipient\r\nSubject: SPAM SPAM SPAM\r\n\r\nThis is message 1 from our test script.\r\n.\r\n S: 250 2.0.0 k7tkibyb024731 Message accepted for delivery C: QUIT S: 221 2.0.0 example.org closing connection Connection closed by foreign host. $ Cryptosystems Cryptosystems Symmetric Secret Key Cryptosystems encryption is the process that converts the plaintext message to a ciphertext using some key required by the encryption algorithm decryption is the process that converts the ciphertext to a plaintext message using some key required by the decryption algorithm for centuries, the only known type of cryptosystem was symmetric secret key cryptosystems symmetric key means that the sender and receiver must possess the same key in order to encrypt messages and/or decrypt codes secret key means that the encryption/decryption processes are compromised when the key is known by unauthorized agents 3
Caesar Cipher One of the earliest recorded symmetric secret key systems is known as the Caesar cipher the historian Suetonius tells us that Julius Caesar used this method with a key value of 3 each letter of the message is shifted three places in the alphabet to create the ciphertext ABCDEFGHIJKLMNOPQRSTUVWXYZ 3 DEFGHIJKLMNOPQRSTUVWXYZABC HELLO WORLD is encrypted as KHOOR ZRUOG Substitution Ciphers Caesar Cipher is an example of a simple substitution cipher whereby a individual symbol is assigned a different value (other than itself) for purposes of creating a ciphertext Caesar Cipher is a monoalphabetic substitution cipher each symbol has only one ciphertext assignment Polyalphabetic substitution ciphers use multiple monoalphabetic assignment for each symbol Vignere Cipher is an example Substitution Ciphers Vignere cipher can be considered an n- place Caesar cipher where n = the length or number of keys a b c d e f g h i j k l m n o p q r s t u v w x y z S T U V W X Y Z A B C D E F G H I J K L M N O P Q R O P Q R S T U V W X Y Z A B C D E F G H I J K L M N N O P Q R S T U V W X Y Z A B C D E F G H I J K L M G H I J K L M N O P Q R S T U V W X Y Z A B C D E F B C D E F G H I J K L M N O P Q R S T U V W X Y Z A I J K L M N O P Q R S T U V W X Y Z A B C D E F G H R S T U V W X Y Z A B C D E F G H I J K L M N O P Q D E F G H I J K L M N O P Q R S T U V W X Y Z A B C Substitution Ciphers the Caesar Cipher is both easy to implement and to break a brute force attack requires only 25 attempts general substitution ciphers are not so easy to break if each letter of the alphabet is encoded with a different symbol, then the total number of possible codes would be 25 24 23 23 2 1 = 25! or approximately 4 10 24 The plaintext Fight Furman Fight becomes XWTNU NLUEOA LJOYW 4
Letter Frequencies fortunately, brute force methods are not always required. natural languages have redundancies in coding that can be exploited. for e.g., single letter frequencies in English are highly skewed Letter Frequencies there are common two- letter frequencies Letter Frequencies there are common three- letter frequencies too 5
6
N- grams a bigram is an instance of an n- gram, where n = 1, 2, 3, 4, zip( ) makes it easy to create n- grams N- grams How could we use zip( ) to create n- grams for letter frequencies? >>>list=['now,'is,'the,'time,'for,'all,'good,'men,'to,'come,'to,'the,'aid,'of,'their,'country'] >>> zip(list, list[1:]) [('Now', 'is'), ('is', 'the'), ('the', 'time'), ('time', 'for'), ('for', 'all'), ('all', 'good'), ('good', 'men'), ('men', 'to'), ('to', 'come'), ('come', 'to'), ('to', 'the'), ('the', 'aid'), ('aid', 'of'), ('of', 'their'), ('their', 'country')] >>> zip(list, list[1:], list[2:]) [('Now', 'is', 'the'), ('is', 'the', 'time'), ('the', 'time', 'for'), ('time', 'for', 'all'), ('for', 'all', 'good'), ('all', 'good', 'men'), ('good', 'men', 'to'), ('men', 'to', 'come'), ('to', 'come', 'to'), ('come', 'to', 'the'), ('to', 'the', 'aid'), ('the', 'aid', 'of'), ('aid', 'of', 'their'), ('of', 'their', 'country')] 7