--- Caesar Cipher---
Hi,
Today we will be implementing Caesar cipher algorithm for encryption. Caesar cipher is one of the basic encryption technique. that uses a make shift number to interchange the position of text.
for E.g.
if the shift value is 2,
A --> C, B--> D, c-->e, d-->f and so on...
and if shift value is -2
A --> Y, B--> Z, c-->a, d-->b and so on...
Code:
#getting input from use for text to encrypt and shift value
text = raw_input("Please enter the text to be converted..!! : \n");
change=input("Enter the shift number for encryption: \n")
#using list to save the conversion, for end result
new_text = []
#using Caesar cipher algo, we are converting upper and lower case alphabets separately, and ignoring others
for x in text:
if x.islower():
new_text.append(chr((ord(x) + change - 97) %26 + 97))
elif x.isupper():
new_text.append(chr((ord(x) + change - 65) % 26 + 65))
else:
new_text.append(x)
print "Text : \t\t"+text
#using join inbuilt function for text join from list
print "New Text :\t"+''.join(new_text)
E.g:
Please enter the text to be converted..!! :
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Enter the shift number for encryption:
2
Text : THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
New Text : VJG SWKEM DTQYP HQZ LWORU QXGT VJG NCBA FQI
We can use to decipher the encrypted code by same algorithm. we need the shift value or key by which it was encrypted earlier. for above example we use shift of 2. so, for decryption we need to use same algorithm with reverse shift value, for this case -2.
E.g:
Please enter the text to be converted..!! :
VJG SWKEM DTQYP HQZ LWORU QXGT VJG NCBA FQI
Enter the shift number for encryption:
-2
Text : VJG SWKEM DTQYP HQZ LWORU QXGT VJG NCBA FQI
New Text : THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
No comments:
Post a Comment