-
Notifications
You must be signed in to change notification settings - Fork 0
/
vigenereCipher.py
76 lines (49 loc) · 1.98 KB
/
vigenereCipher.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
import pyperclip
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def main():
myMessage="Qwtguhexcymwlhtzltjiwyuxethizyetcowqbpgzcwb"
#myMessage="uye bv gmp"
myKey= "STASSINOPOULOS"
print decryptMessage(myKey,myMessage)
#translated=decryptMessage(myKey,myMessage)
#print translated
"""
fo=open('randomphrases.txt')
words=fo.readlines()
fo.close()
for word in words:
#print word.split()[1:2]
#mykey=word.split()[1:2]
#print mykey[0]
print decryptMessage(,myMessage)
"""
def encryptMessage(key, message):
return translateMessage(key, message, 'encrypt')
def decryptMessage(key, message):
return translateMessage(key, message, 'decrypt')
def translateMessage(key, message, mode):
translated = [] # stores the encrypted/decrypted message string
keyIndex = 0
key = key.upper()
for symbol in message: # loop through each character in message
num = LETTERS.find(symbol.upper())
if num != -1: # -1 means symbol.upper() was not found in LETTERS
if mode == 'encrypt':
num += LETTERS.find(key[keyIndex]) # add if encrypting
elif mode == 'decrypt':
num -= LETTERS.find(key[keyIndex]) # subtract if decrypting
num %= len(LETTERS) # handle the potential wrap-around
# add the encrypted/decrypted symbol to the end of translated.
if symbol.isupper():
translated.append(LETTERS[num])
elif symbol.islower():
translated.append(LETTERS[num].lower())
keyIndex += 1 # move to the next letter in the key
if keyIndex == len(key):
keyIndex = 0
else:
# The symbol was not in LETTERS, so add it to translated as is.
translated.append(symbol)
return ''.join(translated)
if __name__ == '__main__':
main()