-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROTCrypt.py
75 lines (64 loc) · 2.37 KB
/
ROTCrypt.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
# Author: Shreyan Gupta
# Date Started: 02/09/2023
# Date Completed: 02/11/2023
def caesarCipher(i_passIn: str, i_shiftIn: int) -> str:
m_shifted = ""
for char in i_passIn:
mf_randNum = i_shiftIn
m_isInt = False
# Integer Validation
try:
int(char)
m_isInt = True
except ValueError:
m_isInt = False
# Letter Caesar Cipher
if char.isalpha():
m_caseCheck = 1 if char.isupper() else 2
char = char.upper()
m_charIndex = ord(char)
if m_charIndex + mf_randNum > 90:
m_charIndex -= 26
m_upperChar = chr(m_charIndex + i_shiftIn).upper()
m_newChar = m_upperChar if m_caseCheck == 1 else m_upperChar.lower()
m_shifted += m_newChar
# Number Reversing
elif m_isInt:
char = str(11 - int(char))
m_shifted += char
else:
m_shifted += char
return m_shifted
def reverseCaesar(i_cipherText, i_key):
m_plaintext = ""
for char in i_cipherText:
if char.isalpha():
m_caseCheck = 1 if char.isupper() else 2
char = char.upper()
m_charIndex = ord(char)
if m_charIndex - i_key < 65:
m_charIndex += 26
m_upperChar = chr(m_charIndex - i_key).upper()
m_newChar = m_upperChar if m_caseCheck == 1 else m_upperChar.lower()
m_plaintext += m_newChar
elif char.isdigit():
m_plaintext += str(11 - int(char))
else:
m_plaintext += char
return m_plaintext
if __name__ == "__main__":
i_checkCrypt = input("Do you wish to decrypt or encrypt your text?\n> ")
if i_checkCrypt.lower() == "decrypt":
i_messageIn = input("\nPlease enter your message here:\n> ")
i_shiftIn = input("\nPlease enter the shift you wish for:\n> ")
print(
f"\nThe decrypted message is: {reverseCaesar(i_messageIn, int(i_shiftIn))}"
)
elif i_checkCrypt.lower() == "encrypt":
i_messageIn = input("\nPlease enter your message here:\n> ")
i_shiftIn = input("\nPlease enter the shift you wish for:\n> ")
print(
f"\nThe encrypted message is: {caesarCipher(i_messageIn, int(i_shiftIn))}"
)
else:
print("\nWrong input entered, please run the program again.")