forked from Moocooman/Encryptor-Decryptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decryptor.py
48 lines (39 loc) · 1.37 KB
/
Decryptor.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
charSource = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,!?"()&-0123456789'
## Functions to convert from letters to numbers and reverse
def getLetterIndex(let):
try:
return charSource.index(let)
except:
print("Invalid character inserted")
def getIndexLetter(num):
return charSource[num]
# User input
encryptedText = input("Decrypt: ")
key = input("Key: ")
# Variable setup
newKey = []
decryptedText = ""
try:
if len(encryptedText) == len(key):
# Covert key to numbers
for letter in key:
newKey.append(getLetterIndex(letter))
for letter in encryptedText:
# Convert letter to number
letterID = getLetterIndex(letter)
# Offset letter using key
letterID -= newKey[0]
if letterID < 0:
letterID += 72
# Remove used key letter
newKey.remove(newKey[0])
# Convert back to text and add to output variable
decryptedText += getIndexLetter(letterID)
else:
# Error message
decryptedText = "Decrypt and Key must be same length\nCheck for misplaced characters violating this rule"
# Output decrypted message
print(decryptedText)
input("Press Enter to exit")
except:
print("Valid Input Required")