Code for Caesar Cipher

This is a simple python code that can be used to encrypt a message using caesar cipher method.
We can specify the number of shifts we need. Numbers and special characters are not encrypted.


text=raw_input("Enter the text that you want to encrypt")
for i in range(1,26):         # 'i' specifies the number of shifts
newtext=""                    #define newtext

for ch in text:               #for each character in the text
newval=ord(ch)+i              #ord(ch)=>gives the ascii value of the character. newval=ascii val + shift
if ch.islower():              #check whether the given character is lower case alphabet
if newval > ord('z'):         # If the newval > 122
newval-=(ord('z')-ord('a'))+1 # we need to do a rotation back. So newval=newval-26
newtext=newtext+chr(newval)   #newtext=character of the newval

elif  ch.isupper():           #Check whether the character is uppercase alphabet
if msg>ord('Z'):              #check if new ascii>ascii of Z
msg-=(ord('Z')-ord('A'))+1    # If greater, new ascii-26
newtext=newtext+chr(msg)      #newtext=character of the newval

else:                         # if it is not alphabet
newtext=newtext+ch            # newtext = the character itself
print newtext                 # prints the encrypted text

Output

Enter the text that you want to encrypt: 123 abc  !@#
123 bcd  !@#
123 cde  !@#
123 def  !@#
123 efg  !@#
123 fgh  !@#
123 ghi  !@#
123 hij  !@#
123 ijk  !@#
123 jkl  !@#
123 klm  !@#
123 lmn  !@#
123 mno  !@#
123 nop  !@#
123 opq  !@#
123 pqr  !@#
123 qrs  !@#
123 rst  !@#
123 stu  !@#
123 tuv  !@#
123 uvw  !@#
123 vwx  !@#
123 wxy  !@#
123 xyz  !@#
123 yza  !@#
123 zab  !@#

Note: While doing, please make sure that the indentation is correct

Advertisements

Tagged:

One thought on “Code for Caesar Cipher

  1. […] to get the password. The file Krypton3 contains the encrypted password: OMQEMDUEQMEK Use the code https://sowmyaravidas.wordpress.com/2012/09/15/encryption-code-for-caesar-cipher/ here with proper indentation to decrypt it. This helps you generate all the possibilities: […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s