-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar-cipher.py
More file actions
57 lines (46 loc) · 2.24 KB
/
caesar-cipher.py
File metadata and controls
57 lines (46 loc) · 2.24 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
57
# This code implements a simple Caesar cipher encryption algorithm.
def getDoubleAlphabet(alphabet):
doubleAlphabet = alphabet + alphabet
return doubleAlphabet
# This function just joins the alphabet with itself to enable the "shift" without giving an index error.
def getMessage():
stringToEncrypt = input("Please enter a message to encrypt: ")
return stringToEncrypt
# This function prompts the user to enter a message to encrypt and returns it.
def getCipherKey():
shiftAmount = input("Please enter a key (whole numbers from 1 to 25): ")
return shiftAmount
# Here the user will enter the number that will define how much the letters will be shifted.
def encryptMessage(message, cipherKey, alphabet):
encryptedMessage = ""
uppercaseAlphabet = message.upper()
for currentCharacter in uppercaseAlphabet:
position = alphabet.find(currentCharacter)
newPosition = position + int(cipherKey)
if currentCharacter in alphabet:
encryptedMessage = encryptedMessage + alphabet[newPosition]
else:
encryptedMessage = encryptedMessage + currentCharacter
return encryptedMessage
# This is the function that makes Caesar Cipher's magic!
def decryptMessage(message, cipherKey, alphabet):
decryptKey = -1 * int(cipherKey)
return encryptMessage(message, decryptKey, alphabet)
# Reuses the encryption function, but inverts the "key" value to decrypt.
# Create the main function runCaesarCipherProgram
def runCaesarCipherProgram():
myAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print(f'Alphabet: {myAlphabet}')
myAlphabet2 = getDoubleAlphabet(myAlphabet)
print(f'Aphabet2: {myAlphabet2}')
myMessage = getMessage()
print(myMessage)
myCipherKey = getCipherKey()
print(myCipherKey)
myEncryptedMessage = encryptMessage(myMessage, myCipherKey, myAlphabet2)
print(f'Encrypted Message: {myEncryptedMessage}')
myDecryptedMessage = decryptMessage(myEncryptedMessage, myCipherKey, myAlphabet2)
print(f'Decrypted Message: {myDecryptedMessage}')
# This function runs the entire Caesar Cipher program, calling all other functions in order.
# If you don’t do this… nothing happens!
runCaesarCipherProgram()