-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt_hybrid.py
74 lines (52 loc) · 1.73 KB
/
encrypt_hybrid.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import hashlib
from key_gen import key_generation
from Crypto.Cipher import AES
def read_file(file_path):
with open(file_path, 'r') as file:
return file.read()
def divide_text(text):
total_length = len(text)
midpoint = total_length // 2
part1 = text[:midpoint]
part2 = text[midpoint:]
return part1, part2
def save_to_file(data, file_path):
with open(file_path, 'w') as f:
f.write(data)
if __name__ == "__main__":
# Generate keys using the function from key_gen.py
key_generation()
# Divide text
file_path = os.path.join("text.txt")
file_content = read_file(file_path)
part1, part2 = divide_text(file_content)
# Save part1 and part2 to separate files
save_to_file(part1, 'part1.txt')
save_to_file(part2, 'part2.txt')
# Display the results
print("\nDivided Parts:")
print("Part 1:", part1)
print("Part 2:", part2)
def get_part1():
return part1
def get_part2():
return part2
# Generating hash using SHA-256 algorithm
def generate_file_hash(file_path):
# Open the file in binary mode and read its contents
with open(file_path, 'rb') as f:
data = f.read()
# Create a new SHA256 hash object
hash_object = hashlib.sha256()
# Update the hash object with the data
hash_object.update(data)
# Get the hexadecimal representation of the hash
hash_hex = hash_object.hexdigest()
return hash_hex
# Usage
hash_value = generate_file_hash('text.txt')
print('SHA256 hash of the file:', hash_value)
# Save the hash to a file
with open("hash_value.txt", "w") as hash_file:
hash_file.write(hash_value)