-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_caesar.py
57 lines (44 loc) · 1.57 KB
/
test_caesar.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
# -*- coding: utf-8 -*-
"""
Caesar cypher script
Encode and decode messages by scrambling the letters in your message
Created on Fri Feb 1 23:06:50 2019
@author: shakes
"""
import string
letters = (
string.ascii_letters
) # contains 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
def generateCypher(offset):
offset = offset
totalLetters = 26
keys = {" ": " "} # Caesar Cypher
invKeys = {" ": " "} # Inverse Caesar Cypher
for index, letter in enumerate(letters):
if index < totalLetters: # lowercase
keys[letter] = letters[(index + offset) % 26]
else: # uppercase
keys[letter] = letters[(index + offset) % 26 + 26]
invKeys[keys[letter]] = letter
return keys, invKeys
def encrypt(message, keys):
encryptedMessage = []
for letter in message:
encryptedMessage.append(keys[letter])
encryptedMessage = "".join(encryptedMessage)
return encryptedMessage
def decrypt(encryptedMessage, invKeys):
decryptedMessage = []
for letter in encryptedMessage:
decryptedMessage.append(invKeys[letter])
decryptedMessage = "".join(decryptedMessage)
return decryptedMessage
if __name__ == "__main__":
message = input("Enter a message to encypher: ")
print("Message:", message)
offset = int(input("Enter an offset: "))
keys, invKeys = generateCypher(offset)
encryptedMessage = encrypt(message, keys)
print("Encrypted message:", encryptedMessage)
decryptedMessage = decrypt(encryptedMessage, invKeys)
print("Decrypted message:", decryptedMessage)