-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyObfuscate.py
209 lines (184 loc) · 6.44 KB
/
keyObfuscate.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
import hashlib
import random
R1 = random.getrandbits(64)
R2 = random.getrandbits(64)
R3 = random.getrandbits(64)
def left_shift(data, shift_by):
return (data << shift_by) & 0xFFFFFFFFFFFFFFFF | (data >> (64 - shift_by))
def right_shift(data, shift_by):
return (data >> shift_by) | (data << (64 - shift_by)) & 0xFFFFFFFFFFFFFFFF
def bit_inversion(data):
return data ^ 0xFFFFFFFFFFFFFFFF
def hash_function(data):
hash_object = hashlib.sha256()
hash_object.update(data.to_bytes(8, byteorder='big'))
return int.from_bytes(hash_object.digest()[:8], byteorder='big')
def transform_4bit_segments(hex_number):
hex_string = f"{hex_number:016X}"
segments = [hex_string[i:i+1] for i in range(len(hex_string))]
new_segments = []
for i in range(0, len(segments), 4):
block = segments[i:i+4]
reordered_block = [block[2], block[3], block[0], block[1]]
new_segments.extend(reordered_block)
reordered_hex_string = ''.join(new_segments)
return int(reordered_hex_string, 16)
def inverse_transform_4bit_segments(hex_number):
hex_string = f"{hex_number:016X}"
segments = [hex_string[i:i+1] for i in range(len(hex_string))]
new_segments = []
for i in range(0, len(segments), 4):
block = segments[i:i+4]
inverse_block = [block[2], block[3], block[0], block[1]]
new_segments.extend(inverse_block)
reordered_hex_string = ''.join(new_segments)
return int(reordered_hex_string, 16)
def transform_number(data):
hex_string = f"{data:016X}"
parts = [hex_string[i:i+4] for i in range(0, len(hex_string), 4)]
reordered_parts = [parts[2], parts[0], parts[3], parts[1]]
reordered_hex_string = ''.join(reordered_parts)
return int(reordered_hex_string, 16)
def inverse_transform_number(data):
hex_string = f"{data:016X}"
parts = [hex_string[i:i+4] for i in range(0, len(hex_string), 4)]
reordered_parts = [parts[1], parts[3], parts[0], parts[2]]
reordered_hex_string = ''.join(reordered_parts)
return int(reordered_hex_string, 16)
#암호화
def key_encrypt(key, seed):
random.seed(seed)
H1 = hash_function(R1)
H2 = hash_function(R2)
H3 = hash_function(R3)
M1 = key ^ R1
S1 = left_shift(M1, 5) # 5비트 왼쪽으로 shift
X1 = S1 ^ H1
S2 = right_shift(X1, 3) # 3비트 오른쪽으로 shift
M2 = S2 ^ R2
B2 = bit_inversion(M2)
X2 = B2 ^ H2
E = (X2 ^ R3) + H3 # 최종 암호화된 키 생성
return E
#복호화
def key_decrypt(encrypted_key):
H1 = hash_function(R1)
H2 = hash_function(R2)
H3 = hash_function(R3)
X2 = (encrypted_key - H3) ^ R3
B2 = X2 ^ H2
M2 = bit_inversion(B2)
S2 = M2 ^ R2
B1 = left_shift(S2, 3) # 3비트 왼쪽으로 shift
S1 = B1 ^ H1
M1 = right_shift(S1, 5) # 5비트 오른쪽으로 shift
key = M1 ^ R1
return key
def key2_encrypt(key, seed):
random.seed(seed)
R4 = random.getrandbits(64)
R5 = random.getrandbits(64)
R6 = random.getrandbits(64)
H1 = hash_function(R4)
H2 = hash_function(R5)
H3 = hash_function(R6)
key = key ^ R1
key = left_shift(key,5)
key = key ^ H1
key = right_shift(key,3)
key = key ^ R2
key = bit_inversion(key)
key = key ^ H2
key = transform_number(key)
key = key ^ R3
key = transform_4bit_segments(key)
E = key ^ H3
return E, R4, R5, R6
def key2_decrypt(E, R4, R5, R6):
H4 = hash_function(R4)
H5 = hash_function(R5)
H6 = hash_function(R6)
key = E ^ H4
key = inverse_transform_4bit_segments(key)
key = key ^ R4
key = inverse_transform_number(key)
key = key ^ H5
key = bit_inversion(key)
key = key ^ R5
key = left_shift(key, 3)
key = key ^ H6
key = right_shift(key, 5)
key = key ^ R6
return key
def key_schedule(key, rounds):
schedule = [key]
for i in range(1, rounds):
new_key = hashlib.sha256(schedule[-1]).digest()
schedule.append(new_key)
return schedule
def feistel_network(block, round_key):
left, right = block[:8], block[8:]
f_result = bytes(a ^ b for a, b in zip(right, round_key[:8]))
new_right = bytes(a ^ b for a, b in zip(left, f_result))
return right + new_right
def inverse_feistel_network(block, round_key):
left, right = block[:8], block[8:]
f_result = bytes(a ^ b for a, b in zip(left, round_key[:8]))
new_left = bytes(a ^ b for a, b in zip(right, f_result))
return new_left + left
def str_encrypt(data, key, rounds=16):
key_sched = key_schedule(key, rounds)
encrypted = bytearray()
for i in range(0, len(data), 16):
block = data[i:i+16]
if len(block) < 16:
block = block.ljust(16, b'\x00')
for round_key in key_sched:
block = feistel_network(block, round_key)
encrypted.extend(block)
return bytes(encrypted)
def str_decrypt(data, key, rounds=16):
key_sched = key_schedule(key, rounds)
decrypted = bytearray()
for i in range(0, len(data), 16):
block = data[i:i+16]
for round_key in reversed(key_sched):
block = inverse_feistel_network(block, round_key)
decrypted.extend(block)
return bytes(decrypted).rstrip(b'\x00')
#키 파일 생성
def save_key_to_file(encrypted_key, filename):
with open(filename, 'w') as file:
encrypted_key = str(encrypted_key)
file.write(encrypted_key)
#키 파일 불러오기
def load_key_from_file(filename):
with open(filename, 'r') as file:
return file.read()
def build_def(key, seed, filename):
encrypted_key = key_encrypt(key, seed)
print(f"Encrypted key: {encrypted_key:016X}")
save_key_to_file(encrypted_key, filename)
def str_en(filename,seed,plaintext):
key = load_key_from_file(filename)
key = int(key)
decrypted_key = key_decrypt(key)
print(f"Decrypted key: {decrypted_key:016X}")
en_key,R4,R5,R6 = key2_encrypt(decrypted_key,seed)
en_key = en_key.to_bytes(8, byteorder='big')
encrypted = str_encrypt(plaintext, en_key)
print("Encrypted:", encrypted)
return en_key, encrypted
def str_de(en_key, encrypted):
decrypted = str_decrypt(encrypted, en_key)
print("Decrypted:", decrypted)
print("Decrypted (as text):", decrypted.decode('utf-8'))
# 테스트
key = 0x123456789ABCDEF0
seed = 12345 #key의 앞 5자리 예정
plaintext = b'This is a test message. It is longer than 16 bytes.'
filename = "key.txt"
build_def(key,seed,filename)
en_key, encrypted = str_en(filename,seed,plaintext)
str_de(en_key, encrypted)