-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.py
More file actions
66 lines (62 loc) · 1.73 KB
/
Copy pathencrypt.py
File metadata and controls
66 lines (62 loc) · 1.73 KB
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
#!/usr/bin/python
from getpass import getpass
import os, random
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
def encrypt(key, filename):
chunksize = 64*1024
outputFile = '(encrypted)'+filename
filesize = str(os.path.getsize(filename)).zfill(16)
IV = ""
for i in range(16):
IV += chr(random.randint(0,0xFF))
encryptor = AES.new(key, AES.MODE_CBC, IV)
with open(filename, 'rb') as infile:
with open (outputFile, 'wb') as outfile:
outfile.write(filesize)
outfile.write(IV)
while True:
chunk = infile.read(chunksize)
if len (chunk) == 0:
break
elif len(chunk) % 16 != 0 :
chunk += ' ' * (16 - (len(chunk) % 16))
outfile.write(encryptor.encrypt(chunk))
def decrypt(key, filename):
chunksize = 64*1024
outputFile = filename[11:]
with open(filename, 'rb') as infile:
filesize = long(infile.read(16))
IV = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, IV)
with open(outputFile, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
outfile.write(decryptor.decrypt(chunk))
outfile.truncate(filesize)
def getKey(password):
hasher = SHA256.new(password)
return hasher.digest()
def main():
print '#'*30
print '\tencrypt files'
print '#'*30+'\n'
choice = raw_input('world you like to (E)ncrypt or (D)ecrypt ? ')
if choice == 'E':
filename = raw_input('File to encrypt : ')
passw = getpass('Password : ')
password = passw
encrypt(getKey(password), filename)
print "Done."
elif choice == 'D':
filename = raw_input('File to decrypt : ')
passw = getpass('Password : ')
password = passw
decrypt(getKey(password), filename)
print "Done."
else:
print "No option selected, closing..."
if __name__=='__main__':
main()