-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathencoder.py
executable file
·91 lines (67 loc) · 2.33 KB
/
encoder.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
#!/usr/bin/python
import sys
import argparse
from string import ascii_letters
from random import choice
import pprint
try:
from itertools import zip_longest
except Exception as e:
from itertools import izip_longest as zip_longest
parser = argparse.ArgumentParser(prog="enc", add_help=True, description='Encrypt text with VernamCipher')
parser.add_argument('--method', default="encrypt", type=str)
parser.add_argument('--text', '-s', metavar='text', default="hello", type=str, help="Message to encrypt/decrypt")
parser.add_argument('--key', '-k', metavar='key', default="", type=str, help="Encrypt/Decrypt key")
class VernamCipher:
alphabet = list("abcdefghijklmnopqrstuvwxyz !,.")
def chunk(self, str, size):
seq = list(str)
x = [iter(seq)] * size
l = zip_longest(*x, fillvalue='0')
return [''.join(tups) for tups in l]
def encrypt(self, st, key):
if len(st) != len(key):
return "Text and Key have to be the same length."
nText = []
kText = []
for i in range(len(st)):
nText.append(self.alphabet.index(st[i].lower()))
kText.append(self.alphabet.index(key[i].lower()))
res = {"encode":"", "indexs":""}
for i in range(len(nText)):
index = (nText[i] + kText[i]) % len(self.alphabet)
res["encode"] += self.alphabet[index]
res["indexs"] += str(index)
return res;
def decrypt(self, st, key):
if len(st) != len(key):
return "Text and Key have to be the same length."
alphabet = list("abcdefghijklmnopqrstuvwxyz")
nText = []
kText = []
for i in range(len(st)):
nText.append(self.alphabet.index(st[i].lower()))
kText.append(self.alphabet.index(key[i].lower()))
out = ""
for i in range(len(nText)):
op = (nText[i] - kText[i])
if op < 0:
x = len(self.alphabet) + op
else:
x = op % len(self.alphabet)
out += alphabet[x]
return out;
if __name__ == "__main__":
args = parser.parse_args()
args.key = "".join([choice(ascii_letters).lower() for char in args.text]) if len(args.key) < 1 else args.key
if len(args.key) < len(args.text):
raise Exception("Error: key MUST be same length of text")
cipher = VernamCipher()
if (args.method == "encrypt"):
res = cipher.encrypt(args.text, args.key)
text = " ".join(cipher.chunk(res["indexs"], 5))
else:
text = cipher.decrypt(args.text, args.key)
print("key = %s" % args.key)
sys.stdout.write( text + "\n" )
sys.exit(0)