-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcaesar-cipher.py
More file actions
56 lines (47 loc) · 1.86 KB
/
caesar-cipher.py
File metadata and controls
56 lines (47 loc) · 1.86 KB
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
# From: Python Programming: An Introduction to Computer Science
# Written by: John Zelle
# A caesar cipher is a simple substitution cipher based on the idea of
# shifting each letter of the plaintext message a fixed number
# (called the key) of positions in the alphabet.
# For example, if the key value is 2, the word "Sourpuss" would be
# Encoded as "Uqwtrwuu". The original message can be recovered
# by "reencoding" it using the negative of the key.
# Write a program to encode and decode Caesar ciphers, being sure
# to deal with long keys in a circular fashion. The input will only be
# letters and spaces.
# ANSWER BELOW:
from string import ascii_lowercase, ascii_uppercase
def ccipher(string, key):
# Shifts in a circular fashion
new = ""
for ch in string:
if ch in ascii_lowercase:
add = ascii_lowercase.index(ch)
if add + key > 25:
new += ascii_lowercase[add+key-24]
else:
new += ascii_lowercase[add+key]
elif ch in ascii_uppercase:
add = ascii_uppercase.index(ch)
if add + key > 25:
new += ascii_uppercase[add+key-24]
else:
new += ascii_uppercase[add+key]
else:
new += ch
return new
def ccipherAnsCirc(string, key):
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz"
new = ""
for ch in string:
pos = chars.find(ch)
# Use remainder division to take care of circular shift
newpos = (pos + key) % len(chars)
cipher = cipher + chars[newpos]
def main():
print("This program will cipher your message.")
string = input("Please enter a short text to encode: ")
key = int(input("Please enter the key to shift letters: "))
print("The cipher of your text is: {0}".format(ccipher(string, key)))
if __name__ == "__main__":
main()