Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/coder-not-a-virus/main.py
5925 views
1
remove this line to prevent run without your config!!! удалите эту строку для предотвращения запуска без вашей настройки!!!
2
3
import os
4
# in terminal run command: pip install cryptography
5
from cryptography.fernet import Fernet
6
7
scan_dir_to_encrypt_decrypt = 'm:\\my_super_files\\'
8
9
key = Fernet.generate_key()
10
if not os.path.exists('my_key.txt'):
11
with open('my_key.txt', 'wb') as f:
12
f.write(key)
13
else:
14
key = open('my_key.txt', 'rb').read()
15
print(key)
16
cipher = Fernet(key)
17
18
encrypt_yes = True
19
20
if encrypt_yes:
21
with os.scandir(path=scan_dir_to_encrypt_decrypt) as it:
22
for entry in it:
23
if not entry.is_file():
24
print("dir:\t" + entry.name)
25
else:
26
read_file = open(scan_dir_to_encrypt_decrypt+entry.name, 'rb').read()
27
encrypted_file_content = cipher.encrypt(read_file)
28
with open(scan_dir_to_encrypt_decrypt+entry.name, 'wb') as f:
29
f.write(encrypted_file_content)
30
print("file encrypted:\t" + entry.name)
31
else:
32
with os.scandir(path=scan_dir_to_encrypt_decrypt) as it:
33
for entry in it:
34
if not entry.is_file():
35
print("dir:\t" + entry.name)
36
else:
37
encrypted_file_content = open(scan_dir_to_encrypt_decrypt+entry.name, 'rb').read()
38
file_content = cipher.decrypt(encrypted_file_content)
39
with open(scan_dir_to_encrypt_decrypt+entry.name, 'wb') as f:
40
f.write(file_content)
41
print("file decrypted:\t" + entry.name)
42
43
44