-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesarCipher.py
71 lines (49 loc) · 1.58 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
#Encrypts character using the Caesar Cipher by an arbitrary number of shifts
def encrypt(char, shift):
#convert character to ordinal
ordinal = ord(char)
#If an upper case letter...
if ordinal < 91:
#Find place in alphabet
ordinal = ordinal - 65
#shift it
ordinal = ordinal + shift
#if overflows...
if ordinal > 25:
#wrap back around
ordinal = ordinal - 26
#return result
return(chr(ordinal + 65))
#if lowercase letter...
if ordinal > 96:
#Find place in alphabet
ordinal = ordinal - 97
#shift it
ordinal = ordinal + shift
#if overflows
if ordinal > 25:
#wrap back around
ordinal = ordinal - 26
#return result
return(chr(ordinal + 97))
#Start of Main
#Get needed input
message = input("Please enter you message to encrypt: ")
shift = input("Please enter the amount to shift: ")
shift = int(shift)
#empty encoded message to be displayed to user
encoded_message = ""
#Validate shift amount, exit if not
if shift > 25 or shift < 1:
print("Invalid shift amount")
exit()
#Loop through each character in user message
for char in message:
#If letter, shift using encrypt function
if char.isalpha():
encoded_message += encrypt(char, shift)
#else a number/whitespace, leave it alone
else:
encoded_message += char
#print the encoded message back to user
print("Encoded Message = " + encoded_message)