-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.py
More file actions
36 lines (28 loc) · 1.04 KB
/
encrypt.py
File metadata and controls
36 lines (28 loc) · 1.04 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
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import os, sys, stat
key = get_random_bytes(32) # Use a stored / generated key
key_slack_scan_file = 'key.bin'
my_protected_key = open(key_slack_scan_file, "wb")
my_protected_key.write(key)
my_protected_key.close()
os.chmod(key_slack_scan_file, stat.S_IREAD)
file_to_encrypt = 'File.csv'
buffer_size = 65536 # 64kb
# === Encrypt ===
# Open the input and output files
input_file = open(file_to_encrypt, 'rb')
output_file = open('encrypted.' + file_to_encrypt, 'wb')
# Create the cipher object and encrypt the data
cipher_encrypt = AES.new(key, AES.MODE_CFB)
# Initially write the iv to the output file
output_file.write(cipher_encrypt.iv)
# Keep reading the file into the buffer, encrypting then writing to the new file
buffer = input_file.read(buffer_size)
while len(buffer) > 0:
ciphered_bytes = cipher_encrypt.encrypt(buffer)
output_file.write(ciphered_bytes)
buffer = input_file.read(buffer_size)
# Close the input and output files
input_file.close()
output_file.close()