-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt_file.py
More file actions
51 lines (37 loc) · 1.42 KB
/
decrypt_file.py
File metadata and controls
51 lines (37 loc) · 1.42 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
import sys
import base64
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
def decrypt(encrypted_data: bytes, key: bytes) -> bytes:
"""Decrypt file using AES-256-GCM"""
iv = encrypted_data[:12]
tag = encrypted_data[12:28]
ciphertext = encrypted_data[28:]
cipher = Cipher(algorithms.AES(key), modes.GCM(iv, tag))
decryptor = cipher.decryptor()
return decryptor.update(ciphertext) + decryptor.finalize()
def main():
if len(sys.argv) != 4:
print("Usage: python decrypt_file.py <key_b64> <encrypted_file> <output_file>")
print(
"Example: python decrypt_file.py 'your_base64_key' encrypted.bin decrypted.jpg")
sys.exit(1)
key_b64 = sys.argv[1]
encrypted_file_path = sys.argv[2]
output_file_path = sys.argv[3]
try:
key = base64.b64decode(key_b64)
with open(encrypted_file_path, 'rb') as f:
encrypted_data = f.read()
decrypted_data = decrypt(encrypted_data, key)
with open(output_file_path, 'wb') as f:
f.write(decrypted_data)
print(
f"Successfully decrypted {encrypted_file_path} to {output_file_path}")
print(f"Original size: {len(encrypted_data)} bytes")
print(f"Decrypted size: {len(decrypted_data)} bytes")
except Exception as e:
print(f"Decryption failed: {e}")
sys.exit(1)
if __name__ == "__main__":
main()