-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathLSBDecrypter.py
76 lines (60 loc) · 2.04 KB
/
LSBDecrypter.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
__author__ = 'omrih'
import binascii
# Consts
HEADER_SIZE = 54 # Header size of BMP
DELIMITER = "$" # Separator between number of characters and text
# User Configurations
StegImageFile = "hidden_cat.bmp"
class LSBDecrypter:
def __init__(self):
self.fh = open(StegImageFile, 'rb')
self.number_of_chars_in_text = 0
self.original_text = ''
def read_header(self):
# Reading Header - text is not encoded in it
for i in range(0, HEADER_SIZE):
byte = self.fh.read(1)
# Takes the LSB of the next 8 bytes and assemble a byte from them,
# Returns the ASCII representation of the byte created
def get_char(self):
new_byte = ''
# get lsb of next 8 bytes
for bit in range(0, 8):
byte = self.fh.read(1)
# Taking only the LSB
new_byte += str(ord(byte) & 0x01)
# Converting binary value to ASCII
n = int(new_byte, 2)
desteg_char = binascii.unhexlify('%x' % n)
return desteg_char
# Gets the length of the hidden text,
# It was inserted before the delimiter
def get_text_size(self):
curr_ch = self.get_char()
s_sz = ''
# loop while we haven't reached the separator
while curr_ch != DELIMITER:
s_sz += curr_ch
curr_ch = self.get_char()
if (s_sz != ''):
self.number_of_chars_in_text = int(s_sz)
# Reads the entire text hidden in the image
def read_stega_text(self):
decoded_chars = 0;
while decoded_chars < self.number_of_chars_in_text:
self.original_text += self.get_char()
decoded_chars += 1
def close_file(self):
self.fh.close();
def get_text(self):
self.read_header()
self.get_text_size()
self.read_stega_text()
self.close_file()
return self.original_text
def main():
destag_insta = LSBDecrypter()
text = destag_insta.get_text()
print "Successfully decoded, text is: {}".format(text)
if __name__ == '__main__':
main()