# -- ceaser_cipher_1.py -- # Ceaser cipher encode and decode for problems 8 and 9, # page 199 of Zelle's "Python Programming". # # This version shifts any ascii character # in the range 0 to 127 by a constant amount, # wrapping around at 128. (For example, # 123 + 10 = 133 mod 128 = 5.) # # Running it looks like this: # # $ python ceaser_cipher_1.py # Enter plain text: Once through. # Enter numeric key: 6 # Encoded: Utik&znxu{mn4 # # Enter coded text: Utik&znxu{mn4 # Enter numeric key: 6 # Decoded: Once through. # $ # # Jim Mahoney, Sep 22 2008, GPL # def encode(): plain_text = raw_input('Enter plain text: ') key = input('Enter numeric key: ') coded_text = '' for char in plain_text: char_as_number = ord(char) shifted_number = (char_as_number + key) % 128 new_char = chr(shifted_number) coded_text = coded_text + new_char print "Encoded: " + coded_text def decode(): coded_text = raw_input('Enter coded text: ') key = input('Enter numeric key: ') plain_text = '' for char in coded_text: char_as_number = ord(char) shifted_number = (char_as_number - key) % 128 new_char = chr(shifted_number) plain_text = plain_text + new_char print "Decoded: " + plain_text encode() print "" decode()