-
Notifications
You must be signed in to change notification settings - Fork 8
/
CaesarCipher.py
71 lines (57 loc) · 2 KB
/
CaesarCipher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
written by : Srinivas Avireddy
Date : 11/25/2013
This python program is used to implement encryption and decryption of given text using
Caesar cipher algorithm
"""
VALID_STRING = 'abcdefghijklmnopqrstuvwxyz'
def encrypt(text,key):
# used to encrypt a given input text
# 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
encrypt_string = 'abcdefghijklmnopqrstuvwxyz' * 2
output = ''
for char in text:
if char in VALID_STRING:
index = encrypt_string.index(str(char))
output += encrypt_string[index+key]
else:
output += char
return output
def decrypt(text,key):
# used to decrypt a given encrypted text
# 'zyxwvutsrqponmlkjihgfedcbazyxwvutsrqponmlkjihgfedcba'
decrypt_string = 'zyxwvutsrqponmlkjihgfedcba' * 2
output = ''
for char in text:
if char in VALID_STRING:
index = decrypt_string.index(str(char))
output += decrypt_string[index+key]
else:
output += char
return output
if __name__ == '__main__':
print """
1. Press 1 to Encrypt text \n
2. Press 2 to Decrypt text \n
3. Press 3 to do both \n
"""
choice = input('Enter a choice: ')
if choice == 1:
input_txt = raw_input('Enter the text to encrypt:')
key = input('Enter Key between 1 to 25 :')
print "The encrypted text is : "
print encrypt(input_txt, key)
elif choice == 2:
input_txt = raw_input('Enter the text to decrypt:')
key = input('Enter Key between 1 to 25 :')
print "The decrypted text is : "
print decrypt(input_txt, key)
elif choice == 3:
input_txt = raw_input('Enter the text:')
key = input('Enter Key between 1 to 25 :')
print "The encrypted text is : "
print encrypt(input_txt, key)
print "The decrypted text is : "
print decrypt(input_txt, key)
else:
print 'Invalid choice'