-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcript.py
55 lines (42 loc) · 2.34 KB
/
cript.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
import os
from rich import print
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
def Encryption(OrigFile):
# Открытие изображения в бинарном режиме
with open(OrigFile, "rb") as img_file:
binary_data = img_file.read()
# Преобразование байтов в двоичный код
binary_string = ''.join(format(byte, '08b') for byte in binary_data)
# Запись бинарного представления в новый файл
with open("binary_output.txt", "w") as output_file:
output_file.write(binary_string)
# Генерация случайного ключа и IV для AES
key = os.urandom(32) # 256-битный ключ
iv = os.urandom(16) # 128-битный IV
# Извлечение расширения файла
file_extension = os.path.splitext(OrigFile)[1].encode()
# Сохранение ключа и расширения в файл
with open("key.bin", "wb") as key_file:
# Запись расширения файла
key_file.write(file_extension + b'\n')
# Запись ключа
key_file.write(key)
# Создание объекта шифрования
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
# Паддинг для данных
padder = padding.PKCS7(algorithms.AES.block_size).padder()
# Чтение данных из файла для шифрования
with open("binary_output.txt", "rb") as f:
data = f.read()
# Добавление паддинга и шифрование
padded_data = padder.update(data) + padder.finalize()
encryptor = cipher.encryptor()
encrypted_data = encryptor.update(padded_data) + encryptor.finalize()
# Сохранение зашифрованных данных в новый файл
with open("encrypted.txt", "wb") as f:
f.write(iv + encrypted_data)
print("[bold green]Encryption successful! Encrypted data saved to[/bold green] [italic yellow]encrypted.txt[/italic yellow]")
# Удаление файла binary_output.txt
os.remove("binary_output.txt")