-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencipher.py
202 lines (132 loc) · 5.12 KB
/
encipher.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import os
import sys
import zipfile
from Crypto import Random
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Random import random
from Crypto.Signature import PKCS1_v1_5
# Define public and private key names for faster usage
# Sender's private key:
priKey = "A_PrivateKey.pem"
# Receiver's public key:
pubKey = "B_PublicKey.pem"
# File name to encrypt
f_name = ""
# Private key password: (Given keys have no password)
priPass = ""
def usage():
print "python encipher.py <file_name>"
sys.exit(-1)
def sigGenerator(priKey_fname, f_name, priPass):
# Opening and reading file to encrypt
f = open(f_name, "r")
buffer = f.read()
f.close()
# Creating hash of the file. Using SHA-256 (SHA-512 rose problems)
h = SHA256.new(buffer)
# Reading private key to sign file with
keyPair = RSA.importKey(open(priKey_fname, "r").read(), passphrase=priPass)
keySigner = PKCS1_v1_5.new(keyPair)
# Saving signature to *.sig file
f = open(f_name.split('.')[0] + ".sig", "w")
f.write(keySigner.sign(h))
f.close()
def keyGenerator(pubKey_fname, f_name, iv):
# Generating 1024 random bits, and creating SHA-256 (for 32 bits compatibility with AES)
h = SHA256.new(str(random.getrandbits(1024)))
# Reading public key to encrypt AES key with
keyPair = RSA.importKey(open(pubKey_fname, "r").read())
keyCipher = PKCS1_OAEP.new(keyPair.publickey())
# Saving encrypted key to *.key file
f = open(f_name.split('.')[0] + ".key", "w")
f.write(iv + keyCipher.encrypt(h.digest()))
f.close()
# Returning generated key to encrypt file with
return h.digest()
def encipher(keyA_fname, keyB_fname, f_name, priPass):
# Opening file to encrypt in binary reading mode
f = open(f_name, "rb")
buffer = f.read()
f.close()
# Generating file's signature (and saving it)
sigGenerator(keyA_fname, f_name, priPass)
# Generating initializing vector for AES Encryption
iv = Random.new().read(AES.block_size)
# Generating symmetric key for use (and saving it)
k = keyGenerator(keyB_fname, f_name, iv)
# Encrypting and saving result to *.bin file. Using CFB mode
keyCipher = AES.new(str(k), AES.MODE_CFB, iv)
f = open(f_name.split('.')[0] + ".bin", "wb")
f.write(keyCipher.encrypt(buffer))
f.close()
def auxFilesZip(sig, key, bin):
# Opening file to contain all bin, sig and key files
f = zipfile.ZipFile(bin.split('.')[0] + ".all", "w")
# Writing each of the arguments to the created file
f.write(sig)
f.write(key)
f.write(bin)
# Closing the file
f.close()
# Running clean up to the bin, sig and key files
cleanUp(sig, key, bin)
def cleanUp(sig, key, bin):
# Deleting each of the files generated during ciphering
os.remove(sig)
os.remove(key)
os.remove(bin)
def checkFiles(f_name, pubKey, priKey):
# Checking for encrypting file's existence and access
if not os.path.isfile(f_name) or not os.access(f_name, os.R_OK):
print "Invalid file to encrypt. Aborting..."
sys.exit(1)
# Checking for each of the files to create existence and, in case they exist, if they are writable
else:
s = f_name.split('.')[0]
if os.path.isfile(s + ".sig") and not os.access(s + ".sig", os.W_OK):
print "Can't create temporary file: *.bin. Aborting..."
sys.exit(2)
if os.path.isfile(s + ".key") and not os.access(s + ".key", os.W_OK):
print "Can't create temporary file: *.key. Aborting..."
sys.exit(3)
if os.path.isfile(s + ".bin") and not os.access(s + ".bin", os.W_OK):
print "Can't create temporary file: *.bin. Aborting..."
sys.exit(4)
if os.path.isfile(s + ".all") and not os.access(s + ".all", os.W_OK):
print "Can't create output file. Aborting..."
sys.exit(5)
# Checking for public key's existence and access
if not os.path.isfile(pubKey) or not os.access(pubKey, os.R_OK):
print "Invalid public key file. Aborting..."
sys.exit(6)
# Checking for private key's existence and access
if not os.path.isfile(priKey) or not os.access(priKey, os.R_OK):
print "Invalid private key file. Aborting..."
sys.exit(7)
# Gathering encrypting file name
if len(sys.argv) > 2:
usage()
elif len(sys.argv) == 1:
print "File name:"
f_name = raw_input(">>> ")
else:
f_name = sys.argv[1]
# Gathering names of keys
if priKey == "":
print "Sender's private key file name:"
priKey = raw_input(">>> ")
if pubKey == "":
print "Receiver's public key file name:"
pubKey = raw_input(">>> ")
# Running checks to files
#checkFiles(f_name, pubKey, priKey)
# Reading password if not assigned:
if priPass == "":
print "Private key password (ENTER for empty value):"
priPass = raw_input(">>> ")
# Ciphering file (and generating all auxiliary files)
encipher(priKey, pubKey, f_name, priPass)
# Generating output file and clean up
auxFilesZip(f_name.split('.')[0] + ".sig", f_name.split('.')[0] + ".key", f_name.split('.')[0] + ".bin")