-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesarCipher.py
53 lines (39 loc) · 1.31 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
from collections import deque
class caesarCipher(object):
def __init__(self, message, key):
self.message = message
self.key = key
def encrypt(self):
encryptedMessage = []
for letter in self.message.lower():
if letter.isalpha():
alphabet = deque('abcdefghijklmnopqrstuvwxyz')
pos = alphabet.index(letter)
alphabet.rotate(-pos)
encryptedMessage.append(alphabet[self.key-1])
else:
encryptedMessage.append(' ')
return ''.join(encryptedMessage)
def decrypt(self):
encryptedMessage = []
for letter in self.message.lower():
if letter.isalpha():
alphabet = deque('abcdefghijklmnopqrstuvwxyz')
pos = alphabet.index(letter)
alphabet.rotate(-pos)
alphabet = list(reversed(alphabet))
encryptedMessage.append(alphabet[self.key-2])
else:
encryptedMessage.append(' ')
return ''.join(encryptedMessage)
def main():
message = input("Input a message to encrypt or decrypt\n")
key = int(input("Input the key to be used for encryption/decryption\n"))
cipher = caesarCipher(message, key)
choice = input("What would you like to do with the message? 1. Encrypt 2. Decrypt\n")
if choice == '1':
print("Your encrypted message =", cipher.encrypt())
elif choice == '2':
print("Your decrypted message =", cipher.decrypt())
if __name__ == '__main__':
main()