-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonocrypt.py
67 lines (55 loc) · 1.64 KB
/
monocrypt.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
#!/usr/bin/python
import sys
from random import shuffle
from copy import copy
alphabet = map(chr, range(32, 126))
alphabet.append('\n')
def removeUnsupportedCharacters(text):
for char in text:
if char not in alphabet:
text = text.replace(char,'.')
return text
def swapCharactersInText(text, alphabet1, alphabet2):
convertedText = ""
for char in text:
index = alphabet1.index(char)
convertedText += alphabet2[index]
return convertedText
def encrypt(text):
text = removeUnsupportedCharacters(text)
key = copy(alphabet)
shuffle(key)
f = open('key.txt','w')
f.write(''.join(key))
cypher = swapCharactersInText(text, alphabet, key)
f = open("cypher.txt",'w')
f.write(cypher)
def decrypt(key, cypher):
key = list(key)
clearText = swapCharactersInText(cypher, key, alphabet)
f = open("clear.txt",'w')
f.write(clearText)
def readDecryptFiles():
with open(sys.argv[1], "r") as cypherFile:
cypher=cypherFile.read()
with open(sys.argv[2], "r") as keyFile:
key=keyFile.read()
decrypt(key,cypher)
def readEncryptFile():
with open(sys.argv[1], "r") as clearTextFile:
clearText=clearTextFile.read()
encrypt(clearText)
def main():
if len(sys.argv) == 2:
readEncryptFile()
elif len(sys.argv) == 3:
readDecryptFiles()
else:
print """
usage of this monoalphabetic encryption tool\n
encrypt:
python monoCrypt.py [/path/to/clearTextFile] \n
decryption:
python monoCrypt.py [/path/to/cypherFile] [/path/to/keyFile] \n"""
if __name__ == "__main__":
sys.exit(main())