-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_encrypt_script.py
More file actions
105 lines (75 loc) · 2.75 KB
/
file_encrypt_script.py
File metadata and controls
105 lines (75 loc) · 2.75 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
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
# Author: Melan Rashitha Dias
# Contact: melan96@github.io
# import CORE Pycrypto module for AES encryptions
import os
# Import AES symmetric cipher - Maintained fixed data block chuncks 16K
from Crypto.Cipher import AES
# SHA256 Secure Hash Algorithm
from Crypto.Hash import SHA256
# Random file postfixes
from Crypto import Random
import logging
# Encryption of FILE (METHOD=AES MODE=CBC)
def encrypt_file(key, filename, pchunksize):
logging.info("Encryption started")
chunksize = pchunksize*1024
outputFileName = "(encypt)"+filename
filesize = str(os.path.getsize(filename)).zfill(16)
IV = Random.new().read(16)
logging.info("Encryption definitions configured")
encryptor = AES.new(key, AES.MODE_CBC, IV)
with open(filename, 'rb') as infile:
with open(outputFileName, 'wb') as outfile:
outfile.write(filesize.encode('utf-8'))
outfile.write(IV)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += b' '*(16-(len(chunk) % 16))
outfile.write(encryptor.encrypt(chunk))
# Decryption of FILE
def decrypt_file(key, filename, pchunksize):
chunksize = 64*pchunksize
outputfile = filename[11:]
with open(filename, 'rb') as infile:
filesize = int(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 getSecretKey(password):
hashKey = SHA256.new(password.encode('utf-8'))
return hashKey.digest()
def getUserInputs():
# Python dictionaries handlings
user_seq = {'filename': '', 'password': ''}
# assign values
user_seq['filename'] = raw_input("file: ")
user_seq['password'] = raw_input("password :")
# return dictionaries
return user_seq
def Main():
choice = raw_input("Do you wish (E)ncrypt or (D)ecrypt ?")
if (choice.upper() == 'D'):
# Prop to Decrypt
print("Preseed D >> Decryption Util")
payload = getUserInputs()
decrypt_file(getSecretKey(payload.get('password')),
payload.get('filename'), 1024)
elif(choice.upper() == 'E'):
# Prop to Encrypt
print("Preseed E >> Encryption Util")
payload = getUserInputs()
encrypt_file(getSecretKey(payload.get('password')),
payload.get('filename'), 1024)
else:
print("Invalid Argument : return 0")
if __name__ == '__main__':
Main()