In cryptography a Caesar cipher is a very simple encryption
In cryptography, a Caesar cipher is a very simple encryption technique in which each letter in the plain text is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, the letter ’A’ would be replaced by ’D’, ’B’ would become ’E’, and so on. The method is named after Julius Caesar, who used it to communicate with his generals. ROT-13 (“rotate by 13 places”) is a widely used example of a Caesar cipher where the shift is 13. In Python, the key for ROT-13 may be represented by means of the following dictionary: key = { ’ a ’ : ’ n ’ , ’ b ’ : ’ o ’ , ’ c ’ : ’ p ’ , ’ d ’ : ’ q ’ , ’ e ’ : ’ r ’ , ’ f ’ : ’ s ’ , ’ g ’ : ’ t ’ , ’ h ’ : ’ u ’ , ’ i ’ : ’ v ’ , ’ j ’ : ’ w’ , ’ k ’ : ’ x ’ , ’ l ’ : ’ y ’ , ’m’ : ’ z ’ , ’ n ’ : ’ a ’ , ’ o ’ : ’ b ’ , ’ p ’ : ’ c ’ , ’ q ’ : ’ d ’ , ’ r ’ : ’ e ’ , ’ s ’ : ’ f ’ , ’ t ’ : ’ g ’ , ’ u ’ : ’ h ’ , ’ v ’ : ’ i ’ , ’w’ : ’ j ’ , ’ x ’ : ’ k ’ , ’ y ’ : ’ l ’ , ’ z ’ : ’m’ , ’A’ : ’ N’ , ’B’ : ’ O’ , ’C’ : ’ P’ , ’D’ : ’ Q’ , ’E ’ : ’ R’ , ’F ’ : ’ S ’ , ’G’ : ’ T’ , ’H’ : ’ U’ , ’ I ’ : ’ V’ , ’ J ’ : ’W’ , ’K’ : ’ X’ , ’L ’ : ’ Y’ , ’M’ : ’ Z ’ , ’N’ : ’ A’ , ’O’ : ’ B’ , ’P ’ : ’ C’ , ’Q’ : ’ D’ , ’R’ : ’ E’ , ’ S ’ : ’ F’ , ’T’ : ’ G’ , ’U’ : ’ H’ , ’V’ : ’ I ’ , ’W’ : ’ J ’ , ’X’ : ’ K’ , ’Y’ : ’ L ’ , ’Z ’ : ’M’ }
Write a function decode that takes two arguments, a secret message stored as a string and a key like the one above stored as a dictionary, and returns the decoded string. Use your function to write a program that decodes the following secrete message: Pnrfne pvcure? V zhpu cersre Pnrfne fnynq! Note that since English has 26 characters, your ROT-13 program will be able to both encode and decode texts written in English. This should be written in python.
Solution
Here is the code for you:
#!/usr/bin/python
 def decode(secretMessage, shift):
 decryptMessage = \'\'
 for i in range(len(secretMessage)):
 if (secretMessage[i] >= 65 and secretMessage[i] <= 91) or (secretMessage[i] >= 97 and secretMessage[i] <= 123):
 decryptMessage += secretMessage[i] - shift
 if secretMessage[i] > 90 and secretMessage[i] < 94:
 decryptMessage += secretMessage[i] - 26
 elif secretMessage[i] > 122 and secretMessage[i] < 126:
 decryptMessage += secretMessage[i] - 26
 print decryptMessage
 return decryptMessage
 print decode(\'Hello\', 3)

