-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathfileencrypt.py
73 lines (53 loc) · 1.9 KB
/
fileencrypt.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
# ---------------------------------------
#
# Sample application to encrypt and decrypt
# files using AES
#
# requires pycrypto
#
# ---------------------------------------
import os, random, struct
from Crypto.Cipher import AES
from Crypto import Random
import boto3
import base64
import hashlib
BLOCK_SIZE = 16
CHUNK_SIZE = 64 * 1024
def encrypt_file(password, in_filename, out_filename):
key = hashlib.sha256(password.encode("utf-8")).digest()
iv = Random.new().read(AES.block_size)
encryptor = AES.new(key, AES.MODE_CBC, iv)
filesize = os.path.getsize(in_filename)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(iv)
while True:
chunk = infile.read(CHUNK_SIZE)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' '.encode("utf-8") * (16 - len(chunk) % 16)
outfile.write(encryptor.encrypt(chunk))
def decrypt_file(password, in_filename, out_filename):
key = hashlib.sha256(password.encode("utf-8")).digest()
with open(in_filename, 'rb') as infile:
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
iv = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, iv)
with open(out_filename, 'wb') as outfile:
while True:
chunk = infile.read(CHUNK_SIZE)
if len(chunk) == 0:
break
outfile.write(decryptor.decrypt(chunk))
outfile.truncate(origsize)
# ------------------------
#
# Main program
#
# ------------------------
password = 'kitty and the kat'
encrypt_file(password,"afile1_dec.txt", out_filename="afile1_dec.txt.enc")
decrypt_file(password, "afile1_dec.txt.enc", out_filename="afilenew_dec.txt")