-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImNaza.py
243 lines (195 loc) · 6.75 KB
/
ImNaza.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python3
from utils import *
from PIL import Image
import random
import pgpy
import cv2
import traceback
# ENCRYPTED_MESSAGE_LENGTH = 1200
# change to 1 if not compressing at all, super fast too
# DUPLICATES = 50
# SELF HOSTING SRC CODE WITH VALUES:
ENCRYPTED_MESSAGE_LENGTH = 3800
DUPLICATES = 32 # 35 for logo img, 32 for Campanile
# change to 1 if not compressing at all, super fast too
BIT_IDX = 3 # 0 = MSB, 7 = LSB
zeroPadder = makeZeroPadder(8)
def get_val(orig, b):
bits = zeroPadder(bin(orig)[2:])
return int(bits[0:BIT_IDX] + str(b) + bits[BIT_IDX + 1 :], 2)
def get_modified_bit(orig):
bits = zeroPadder(bin(orig)[2:])
return bits[BIT_IDX]
"""
MAIN
"""
def sender_job(message, source_image_filepath, target_image_filepath, public_key_filepath):
encrypted_message = encrypt(message, public_key_filepath)
image = read_image(source_image_filepath)
shape = image_shape(image)
message_length = ENCRYPTED_MESSAGE_LENGTH * 8 * DUPLICATES
max_index = shape[0] * shape[1] * 3
transformed_image = transform(image)
locations = generate_locations(public_key_filepath, message_length, max_index)
transformed_encoded_image = encode(encrypted_message, transformed_image, locations)
encoded_image = inverse_transform(transformed_encoded_image)
write_image(encoded_image, target_image_filepath)
def receiver_job(encoded_image_filepath, public_key_filepath, private_key_filepath, passphrase):
encoded_image = read_image(encoded_image_filepath)
shape = image_shape(encoded_image)
message_length = ENCRYPTED_MESSAGE_LENGTH * 8 * DUPLICATES
max_index = shape[0] * shape[1] * 3
transformed_encoded_image = transform(encoded_image)
locations = generate_locations(public_key_filepath, message_length, max_index)
try:
encrypted_message = decode_transformed_image(transformed_encoded_image, locations)
message = decrypt(encrypted_message, private_key_filepath, passphrase)
except Exception as e:
traceback.print_exc()
if 'passphrase' not in str(e).lower():
raise Exception("{0} (image probably doesn't contain any data)".format(str(e)))
raise e
return message
"""
ENCRYPTION ABSTRACTIONS
"""
def encrypt(message, public_key_filepath):
"""Applies PGP encryption to message.
Params:
message - string
public_key_filepath - string
Returns:
encrypted_message - string
"""
key, _ = pgpy.PGPKey.from_file(public_key_filepath)
msg = pgpy.PGPMessage.new(message)
encrypted_message = key.encrypt(msg)
return str(encrypted_message)
def decrypt(encrypted_message, private_key_filepath, passphrase):
"""Decrypts encrypted message.
Params:
encrypted_message - string
private_key_filepath - string
Returns:
decrypted_message - string
"""
if 'PGP' not in encrypted_message:
raise Exception('Invalid PGP Message')
key, _ = pgpy.PGPKey.from_file(private_key_filepath)
msg = pgpy.PGPMessage.from_blob(encrypted_message)
if not key.is_unlocked:
with key.unlock(passphrase):
decrypted_message = key.decrypt(msg)
else:
decrypted_message = key.decrypt(msg)
msg = decrypted_message.message
return bytes(msg, 'utf-8').decode('unicode_escape') # unescape string
"""
IMAGE PROCESSING (ENCODE/DECODE/TRANSFORM)
"""
def encode(encrypted_msg, img, locs):
'''
encrypted_msg: encrypted message
img: cv img
locs: locations for changing the indexes
'''
# encrypted_msg = str(len(encrypted_msg)) + ":" + encrypted_msg
#converts the message into 1's and zeros.
if len(encrypted_msg) > ENCRYPTED_MESSAGE_LENGTH:
raise Exception('Encrypted message too long')
# length = ENCRYPTED_MESSAGE_LENGTH
padded_encrypted_msg = encrypted_msg + ' ' * (ENCRYPTED_MESSAGE_LENGTH - len(encrypted_msg))
# length = ENCRYPTED_MESSAGE_LENGTH * 8
bin_encrypted_msg = ''.join([zeroPadder(bin(ord(c))[2:]) for c in padded_encrypted_msg]) #"100100101001001"
shape = image_shape(img)
cols = shape[1]
# ENCRYPTED_MESSAGE_LENGTH * 8 * DUPLICATES
for i in range(len(bin_encrypted_msg) * DUPLICATES):
l = locs[i]
bit = int(bin_encrypted_msg[i % len(bin_encrypted_msg)])
row = l // (3 * cols)
col = (l // 3) % cols
val = l % 3
pixel_loc = (row, col)
pixel = get_pixel(img, pixel_loc)
pixel[val] = get_val(pixel[val], bit)
set_pixel(img, pixel_loc, pixel)
return img
def decode_transformed_image(transformed_image, locations):
bitstring_duplicates = ['' for _ in range(DUPLICATES)]
shape = image_shape(transformed_image)
cols = shape[1]
for i in range(ENCRYPTED_MESSAGE_LENGTH * 8 * DUPLICATES):
duplicate_idx = i // (ENCRYPTED_MESSAGE_LENGTH * 8)
l = locations[i]
row = l // (3 * cols)
col = (l // 3) % cols
val = get_pixel(transformed_image, (row, col))[l % 3]
bitstring_duplicates[duplicate_idx] += get_modified_bit(val)
encrypted_message = ""
curr_bitstring = ""
for i in range(ENCRYPTED_MESSAGE_LENGTH * 8):
bit_duplicates = [bitstring[i] for bitstring in bitstring_duplicates]
bit = max(bit_duplicates, key=bit_duplicates.count)
curr_bitstring += bit
if len(curr_bitstring) == 8:
# We've read a character.
char_code = int(curr_bitstring, 2)
c = chr(char_code)
encrypted_message += c
curr_bitstring = ""
return encrypted_message
def transform(image):
"""Applies discrete cosine transform to image.
Params:
image - 3 x n x m matrix representation of image
Returns:
transformed_image - 3 x n x m matrix representation of transformed image
"""
transformed_image = image
return transformed_image
def inverse_transform(transformed_image):
"""Applies discrete cosine transform to image.
Params:
transformed_image - 3 x n x m matrix representation of transformed image
Returns:
image - 3 x n x m matrix representation of image
"""
image = transformed_image
return image
def generate_locations(public_key_filepath, length, max_index):
with open(public_key_filepath, 'rb') as f:
public_key = f.read()
pubHash = hashing_function_that_goddamn_works_correctly(public_key)
random.seed(pubHash)
result = random.sample(range(max_index), length)
return result
### IMAGE DATA ABSTRACTIONS
def image_shape(image):
return image.shape[:-1]
# return image.size
def get_pixel(image, location):
return image[location[0], location[1]]
# return list(image.getpixel(location))
def set_pixel(image, location, rgb):
image[location[0], location[1]] = rgb
# image.putpixel(location, tuple(rgb))
def read_image(filepath):
"""Retrieves image.
Params:
filepath - string filepath
Returns:
image - PIL Image object
"""
return cv2.imread(filepath)
# return Image.open(filepath, 'r')
def write_image(image, filepath):
"""Writes image.
Params:
image - PIL Image object
filepath - string of target filepath
Returns:
None
"""
cv2.imwrite(filepath, image)
# image.save(filepath)