-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile Encryption-Decryption Tool.py
More file actions
95 lines (78 loc) · 3.19 KB
/
Copy pathFile Encryption-Decryption Tool.py
File metadata and controls
95 lines (78 loc) · 3.19 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
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
import os
# Function to derive a key from a password
def derive_key(password, salt):
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32, # 256-bit key
salt=salt,
iterations=100000,
backend=default_backend()
)
return kdf.derive(password.encode())
# Function to encrypt a file
def encrypt_file(file_path, password):
# Generate a random salt
salt = os.urandom(16)
# Derive the key from the password
key = derive_key(password, salt)
# Generate a random IV (Initialization Vector)
iv = os.urandom(16)
# Read the file data
with open(file_path, "rb") as file:
plaintext = file.read()
# Pad the plaintext to match block size
padder = padding.PKCS7(algorithms.AES.block_size).padder()
padded_data = padder.update(plaintext) + padder.finalize()
# Encrypt the data
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
ciphertext = encryptor.update(padded_data) + encryptor.finalize()
# Save the encrypted file
encrypted_file_path = file_path + ".enc"
with open(encrypted_file_path, "wb") as file:
file.write(salt + iv + ciphertext)
print(f"[*] File encrypted successfully: {encrypted_file_path}")
# Function to decrypt a file
def decrypt_file(encrypted_file_path, password):
# Read the encrypted file
with open(encrypted_file_path, "rb") as file:
data = file.read()
# Extract salt, IV, and ciphertext
salt = data[:16]
iv = data[16:32]
ciphertext = data[32:]
# Derive the key from the password
key = derive_key(password, salt)
# Decrypt the data
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
decryptor = cipher.decryptor()
padded_plaintext = decryptor.update(ciphertext) + decryptor.finalize()
# Unpad the plaintext
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
plaintext = unpadder.update(padded_plaintext) + unpadder.finalize()
# Save the decrypted file
decrypted_file_path = encrypted_file_path[:-4] # Remove .enc extension
with open(decrypted_file_path, "wb") as file:
file.write(plaintext)
print(f"[*] File decrypted successfully: {decrypted_file_path}")
# Main function
def main():
print("File Encryption/Decryption Tool")
choice = input("Choose an option:\n1. Encrypt a file\n2. Decrypt a file\nEnter your choice (1 or 2): ")
if choice == "1":
file_path = input("Enter the file path to encrypt: ")
password = input("Enter a password: ")
encrypt_file(file_path, password)
elif choice == "2":
encrypted_file_path = input("Enter the file path to decrypt: ")
password = input("Enter the password: ")
decrypt_file(encrypted_file_path, password)
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()