-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaes_file_encryption.py
More file actions
50 lines (41 loc) · 1.99 KB
/
Copy pathaes_file_encryption.py
File metadata and controls
50 lines (41 loc) · 1.99 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
# Script for file encryption
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
def encrypt_file(key, input_file_path, output_file_path):
# Generate a random IV (Initialization Vector)
iv = os.urandom(16)
# Create an AES cipher object with the provided key and mode
cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())
encryptor = cipher.encryptor()
with open(input_file_path, 'rb') as f_in, open(output_file_path, 'wb') as f_out:
# Write the IV to the output file
f_out.write(iv)
# Process the file in chunks and encrypt each chunk
chunk_size = 16 # You can adjust the chunk size as needed
chunk = f_in.read(chunk_size)
while chunk:
encrypted_chunk = encryptor.update(chunk)
f_out.write(encrypted_chunk)
chunk = f_in.read(chunk_size)
def decrypt_file(key, input_file_path, output_file_path):
with open(input_file_path, 'rb') as f_in, open(output_file_path, 'wb') as f_out:
# Read the IV from the input file
iv = f_in.read(16)
# Create an AES cipher object with the provided key, mode, and IV
cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())
decryptor = cipher.decryptor()
# Process the file in chunks and encrypt each chunk
chunk = f_in.read(chunk_size)
while chunk:
encrypted_chunk = encryptor.update(chunk)
f_out.write(encrypted_chunk)
chunk = f_in.read(chunk_size)
# Example usage:
key = b'ffffffff' # Replace with your 16, 24, or 32-byte key
# key = '00112233445566778899aabbccddeeff'
input_file_path = 'test_files/example_1.txt'
encrypted_file_path = 'test_files/example_1.txt.enc'
decrypted_file_path = 'test_files/example_decrypted.txt'
encrypt_file(key, input_file_path, encrypted_file_path)
decrypt_file(key, encrypted_file_path, decrypted_file_path)