-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caesar Cipher - Task 2.py
executable file
·154 lines (136 loc) · 10.5 KB
/
Caesar Cipher - Task 2.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import re
import math
#This is to import the required library so that it could function for this program
def getSelection():
#This code defines and stores the code i have written underneath and this stores the code for selection.
while True:
selection = input("Enter your selection: ").lower()
if selection == ("encrypt") or selection == ("decrypt"):
return selection
#This code will ask the user to enter their selection and will check if they wrote encrypt or decrypt if they have then it will accept it.
else:
print("Enter either Encrypt or Decrypt")
#If the user hasnt entered the required selection then this part will be printed out
def getKeyword():
#This stores the 'Keyword' part of my code which is underneath
while True:
Keyword = input("Enter your keyword: ")
#This will ask the user enter their Keyword
if Keyword.isalpha() == True:
#This will check if the user has entered atleast one letter
return Keyword
else:
print("Keyword must only contain atleast one letter")
#If they didnt enter atleast one letter then this will be printed out
def getMessage():
while True:
message = input("Enter your message: ")
#This will ask the user to enter their message
if bool(re.search("[a-zA-Z]",message)) == True:
#This will search through the message to see if their is atleast one character
return message
#Then the next code will check through their message and will accept their message if they have typed in atleast one character.
else:
print("Enter atleast one character")
#If they havnt wrote atleast one letter then this will be printed out
def translatedMessage(Choice, Keyword, Message):
letterCount = 0
#This variable will store how many character there are in my message.
newMessage = ""
#This variable will store each character one by one from my message once its proccessed from the below code.
Keyword = Keyword * math.ceil(len(Message)/len(Keyword))
symbolcount = 0
#This will store the amount of symbols in the message
if Choice.lower() == "encrypt":
#This shows that the underneath code is going to programmed for encrypt and will run the code if the selection equals to encrypt.
while letterCount < len(Message):
#This will check if there is any character counted and stored in the variable 'letterCount', and the length of the message.
currentLetter = Message[letterCount]
#This variable will store the current letter counted.
isCurrentLetterALetter = bool(re.search("[a-zA-Z]",currentLetter))
#This will check if the variable 'currentLetter' has letters stored or symbols, if it has symbol stored then it will ignore the bottom code and will go straight to .isAlpha().
if isCurrentLetterALetter == True:
#If the variable 'isCurrentLetterALetter' is a letter then it will carry on.
ASCIILetter = ord(currentLetter)
#Then it will convert the variable 'currentLetter' into an ASCII number and will be stored in the 'ASCIILetter' variable.
if bool(re.search("[A-Z]", currentLetter)) == True:
#This will check through 'currentLetter' if it has capital letters through A-Z
keywordASCIIvalue = ord(Keyword[letterCount-symbolcount].upper())
#This will convert the keyword, the lettercount and symbolcount this will be done for uppercase also the letter count will minus symbolcount
newASCIIvalue = ASCIILetter + keywordASCIIvalue - 64
#The ASCII value will store the code which will plus the ASCII letter with keywordASCIIvalue and then minus 64 because we are doing this for uppercase letter of ASCII value
else:
keywordASCIIvalue = ord(Keyword[letterCount].lower())
#Else this part of the code will be done for the lower case letters
newASCIIvalue = ASCIILetter + keywordASCIIvalue - 96
#And this will plus the ASCII letter with the ASCII value of keyword then -96, this is going to be done for lower case letters for ASCII letter in encrypt as its different to upper case
newLetter = chr(newASCIIvalue)
#Then it will convert the ASCII value back into its letter and it will store it in the newLetter variable
if newLetter.isupper() == False and currentLetter.isupper() == True:
#This will check if the new converted letter is not upper and the original current letter is upper
newLetter = chr(newASCIIvalue - 26)
#Then this will -26 as theres 26 letters in the alphabet if we do not - 26 then it will show symbols or numbers
elif newLetter.islower() == False and currentLetter.islower() == True:
#This will check if the new converted letter is not lower and the original current letter is lower
newLetter = chr(newASCIIvalue - 26)
#Then this will -26 as theres 26 letters in the alphabet if we do not - 26 then it will show symbols or numbers
newMessage = newMessage + newLetter
#The newMessage variable will store the new encrypted letter and will add the proccessed letter to newMessage
else:
newMessage = newMessage + currentLetter
#This will add the non converted ASCII symbol directly to newMessage
symbolcount = symbolcount + 1
#This will add 1 each time there is a symbol to symbolcount, in the message
letterCount = letterCount + 1
#Finally it will accept when all the letters in the message has been proccessed and carry on to output the final message.
else:
while letterCount < len(Message):
#This will check if there is any character counted and stored in the variable 'letterCount', and the length of the message.
currentLetter = Message[letterCount]
#This variable will store the current letter counted.
isCurrentLetterALetter = bool(re.search("[a-zA-Z]",currentLetter))
#This will check if the variable 'currentLetter' has letters stored or symbols, if it has symbol stored then it will ignore the bottom code and will go straight to .isAlpha().
if isCurrentLetterALetter == True:
#If the variable 'isCurrentLetterALetter' is a letter then it will carry on.
ASCIILetter = ord(currentLetter)
#Then it will convert the variable 'currentLetter' into an ASCII number and will be stored in the 'ASCIILetter' variable.
if bool(re.search("[A-Z]", currentLetter)) == True:
#This will check through 'currentLetter' if it has capital letters through A-Z
keywordASCIIvalue = ord(Keyword[letterCount-symbolcount].upper())
#This will convert the keyword, the lettercount and symbolcount this will be done for uppercase also the letter count will minus symbolcount
newASCIIvalue = ASCIILetter - keywordASCIIvalue + 64
#The ASCII value will store the code which will minus the ASCII letter with keywordASCIIvalue and then plus 64 because we are doing this for uppercase letter of ASCII value
else:
keywordASCIIvalue = ord(Keyword[letterCount].lower())
#Else this part of the code will be done for the lower case letters
newASCIIvalue = ASCIILetter - keywordASCIIvalue + 96
#And this will minus the ASCII letter with the ASCII value of keyword then +96, this is going to be done for lower case letters in decrypt for ASCII letter as its different to upper case
newLetter = chr(newASCIIvalue)
#Then it will convert the ASCII value back into its letter and it will store it in the newLetter variable
if newLetter.isupper() == False and currentLetter.isupper() == True:
#This will check if the new converted letter is not upper and the original current letter is upper
newLetter = chr(newASCIIvalue + 26)
#Then this will +26 as theres 26 letters in the alphabet if we do not +26 then it will show symbols or numbers
elif newLetter.islower() == False and currentLetter.islower() == True:
#This will check if the new converted letter is not lower and the original current letter is lower
newLetter = chr(newASCIIvalue + 26)
#Then this will +26 as theres 26 letters in the alphabet if we do not +26 then it will show symbols or numbers
newMessage = newMessage + newLetter
#The newMessage variable will store the new encrypted letter and will add the proccessed letter to newMessage
else:
newMessage = newMessage + currentLetter
#This will add the non converted ASCII symbol directly to newMessage
symbolcount = symbolcount + 1
#This will add 1 each time there is a symbol to symbolcount, in the message
letterCount = letterCount + 1
#Finally it will accept when all the letters in the message has been proccessed and carry on to output the final message.
return newMessage
#Finally it will accept when all the letters in the message has been proccessed and carry on to output the finall message.
Choice = getSelection()
keyW = getKeyword()
Msg = getMessage()
translatedMSG = translatedMessage(Choice,keyW,Msg)
#These variables will store the 'def' variables so it could be printed out when referred.
print("Output result: " + translatedMSG)
#This will print out the proccessed message for encrypt or decrypt depending on the users choice
input()