-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcrypt.py
166 lines (146 loc) · 3.79 KB
/
pcrypt.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/python
from point import Point
from getpoly import getpoly
from getKeyFromPoly import getKeyFromPoly
from getpoints import getpoints
from genpoly import genpoly
from Crypto.Cipher import AES
import sys
import pickle
import re
def usage():
print """
USAGE: pcrypt.py [OPTIONS] file
-m <number> Minimum number of keys required to decrypt file
-n <number> Number of keys to generate
-d <filelist> Decrypt the file
<filelist> is a list of semicolon seperated
point files, such as file.p1;file.p2;file.p3
-o <file> The file to output to
By default, the file will be output to file.pcrypt
if encrpyting, and file.pdecrypt if decrypting
-k <keylength> The length of the key, must be 16, 24, or 32.
Default is 16
--help Display this help message
"""
def clerror():
usage()
sys.exit(2)
if (len(sys.argv) < 2):
clerror()
i = 1
file = ""
outfile = ""
minKeys = 2
numKeys = 2
keylength = 16
decrypting = False
try:
while (i < len(sys.argv)):
if (sys.argv[i][0] != '-'):
if (file != ""):
clerror()
else:
file = sys.argv[i]
else:
if (sys.argv[i] == '-m'):
minKeys = int(sys.argv[i+1])
i += 1
elif (sys.argv[i] == '-n'):
numKeys = int(sys.argv[i+1])
i += 1
elif (sys.argv[i] == '-d'):
decrypting = True
pointFileString = sys.argv[i+1]
i += 1
elif (sys.argv[i] == '-o'):
outfile = sys.argv[i+1]
i += 1
elif (sys.argv[i] == '-k'):
keylength = int(sys.argv[i+1])
i += 1
elif(sys.argv[i] == '--help'):
raise
else:
clerror()
i+=1
except:
clerror()
if (file == ""):
clerror()
if (numKeys < minKeys):
print "Error: Number of keys generated is less than the \
\nminimum number of keys required to decrypt"
sys.exit(2)
if(decrypting):
pointFileList = pointFileString.split(';')
if(outfile == ""):
if(not decrypting):
outfile = file + ".pcrypt"
else:
outfile = file + ".pdecrypt"
if(keylength != 16 and keylength != 24 and keylength != 32):
print keylength
print "Error: Keylength must be 16, 24 or 32"
sys.exit(2)
#Done with command line analysis, now on to the actual work:
if(not decrypting):
#generate a polynomial
polynomial = genpoly(minKeys - 1, keylength * 8)
#generate points from the polynomial, and save them to files:
i = 1
points = getpoints(polynomial, numKeys)
for point in points:
f = open(file + '.p' + str(i), 'w')
pickle.dump(point, f)
i += 1;
f.close()
#encrypt the file using the polynomial's key
key = str(getKeyFromPoly(polynomial))
try:
f = open(file, 'r')
except IOError:
print "Cannot open file: " + file
sys.exit(1)
plaintext = f.read()
f.close()
obj = AES.new(key)
#make the plaintext a multiple of the encryption algorithm's blocksize:
while(len(plaintext) % obj.block_size != 0):
plaintext = plaintext + " "
ciphertext = obj.encrypt(plaintext)
#write the ciphertext to file:
f = open(outfile, 'w')
f.write(ciphertext)
f.close()
else:
#we are decrypting
#load the points:
pointList = []
try:
for pointFile in pointFileList:
f = open(pointFile, 'r')
pointList.append(pickle.load(f))
f.close()
except:
print "Error loading point file"
sys.exit(1)
#get the polynomial:
polynomial = getpoly(pointList)
key = str(getKeyFromPoly(polynomial))
try:
f = open(file, 'r')
except IOError:
print "Cannot open file: " + file
sys.exit(1)
ciphertext = f.read()
f.close()
obj = AES.new(key)
plaintext = obj.decrypt(ciphertext)
#remove the padding from the end of the plaintext:
p = re.compile(r"^ +\Z", re.MULTILINE)
plaintextF = p.sub('', plaintext)
#output the plaintext to file:
f = open(outfile, 'w')
f.write(plaintextF)
f.close()