-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugger-cipher.py
More file actions
60 lines (49 loc) · 2.47 KB
/
debugger-cipher.py
File metadata and controls
60 lines (49 loc) · 2.47 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
58
59
60
# this is a test Caesar Cipher program to run with debugging in mind
# funccion to get a double alphabet
def getDoubleAlphabet(alphabet):
doubleAlphabet = alphabet + alphabet
return doubleAlphabet
# funccion to get a message from the user
def getMessage():
stringToEncrypt = input("Please, enter a message to encrypt: ")
return stringToEncrypt
# funccion to get a cipher key from the user
def getCipherKey():
shiftAmount = input("Please, enter a cipher key (number of 1 to 25): ")
return shiftAmount
# funccion to ENCRYPT a message
def encryptMessage(message, cipherKey, alphabet):
encryptedMessage = ""
uppercaseMessage = ""
uppercaseMessage = message.upper() # convert the message to uppercase
for currentCharacter in uppercaseMessage: # for each character in the message
position = alphabet.find(currentCharacter) # find the position of the character in the alphabet
# this is the line with the defect 1: it should be int(cipherKey) but it is not
newPosition = position + int(cipherKey)
if currentCharacter in alphabet:
encryptedMessage = encryptedMessage + alphabet[newPosition] # take a new character from the alphabet
else:
encryptedMessage = encryptedMessage + currentCharacter # keep the character as is if not in alphabet
# if the new position is out of bounds, wrap around
return encryptedMessage
# function to DECRYPT a message
def decryptMessage(message, cipherKey, alphabet):
# to decrypt, we need to reverse the cipher key
decryptKey = -1 * int(cipherKey)
return encryptMessage(message, decryptKey, alphabet) # reuse the encrypt function with a negative key
# the main function to run the caesar cipher program
def runCaesarCipherProgram():
myAlphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print(f'Alfabeto: {myAlphabet}')
myAlphabet2 = getDoubleAlphabet(myAlphabet)
print(f'Alfabeto Duplicado: {myAlphabet2}')
myMessage = getMessage()
print(f'Mensagem original: {myMessage}')
myCipherKey = getCipherKey()
print(f'Chave informada: {myCipherKey}')
myEncryptedMessage = encryptMessage(myMessage, myCipherKey, myAlphabet2)
print(f'Mensagem Criptografada: {myEncryptedMessage}')
myDecryptedMessage = decryptMessage(myEncryptedMessage, myCipherKey, myAlphabet2)
print(f'Mensagem Descriptografada: {myDecryptedMessage}')
# here is where the real program starts
runCaesarCipherProgram()