-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode.py
55 lines (43 loc) · 1.2 KB
/
decode.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
import sys
import cv2
import math
import numpy as np
# Embed secret in the n least significant bit.
# Lower n make picture less loss but lesser storage capacity.
BITS = 2
LOW_BITS = (1 << BITS) - 1
BYTES_PER_BYTE = math.ceil(8 / BITS)
FLAG = '%'
def extract(path):
img = cv2.imread(path, cv2.IMREAD_ANYCOLOR)
data = np.reshape(img, -1)
total = data.shape[0]
res = ''
idx = 0
# Decode message length
while idx < total // BYTES_PER_BYTE:
ch = decode(data[idx*BYTES_PER_BYTE: (idx+1)*BYTES_PER_BYTE])
idx += 1
if ch == FLAG:
break
res += ch
end = int(res) + idx
assert end <= total // BYTES_PER_BYTE, "Input image isn't correct."
secret = ''
while idx < end:
secret += decode(data[idx*BYTES_PER_BYTE: (idx+1)*BYTES_PER_BYTE])
idx += 1
# print(secret)
return secret
def decode(block):
val = 0
for idx in range(len(block)):
val |= (block[idx] & LOW_BITS) << (idx * BITS)
return chr(val)
if __name__ == '__main__':
if len(sys.argv) == 2:
img_path = sys.argv[1]
else:
img_path = "./assets/ubuntu_lsb_embeded.png"
msg = extract(img_path)
print(msg)