-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample2.py
100 lines (73 loc) · 2.31 KB
/
sample2.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
import stegwrapper
import math
import os
import argparse
MODE_SHOW = 'show'
MODE_HIDE = 'hide'
parser = argparse.ArgumentParser(description='Hide or extract a file in/from a jpg.')
parser.add_argument('mode', choices=[MODE_SHOW, MODE_HIDE], help="Hide or extract a file.")
parser.add_argument('-f', help="The file to hide or to write into.")
parser.add_argument('image', nargs=1, help='A jpg file')
arg = parser.parse_args()
def integer_to_binary(integer, bytes):
representation = ""
for i in range(bytes*8):
remainder = integer % (2**(bytes*8 - 1 - i))
bit = 0
if remainder != integer:
bit = 1
integer = remainder
representation = representation + str(bit)
return representation
def binary_to_hex(some_binary):
i = 0
hexer = ""
while i < len(some_binary)/8:
tmp = some_binary[i*8:(i*8)+8]
hexer = hexer + chr(int(tmp, 2))
i+=1
return hexer
def hex_to_integer(hexxer):
i = 0
inter = ""
while i < len(hexxer):
inter = inter + integer_to_binary(ord(hexxer[i]),1)
i+=1
return int(inter, 2)
imageFile = str(arg.image[0])
mode = arg.mode
datei = str(arg.f)
if mode == MODE_HIDE:
if not datei:
print "Please specify a file to hide."
sys.exit(1)
p = stegwrapper.picture.open(imageFile, "w")
# we need some bytes to store the length of the file
size_buffer = int(math.ceil((math.ceil(math.log(p.length * 8,2))) / 8))
max_size = p.length - size_buffer
size = os.path.getsize(datei)
filesize_as_hex = binary_to_hex(integer_to_binary(size, size_buffer))
if size > max_size:
raise Exception("File is too big to store in this image.")
#print repr(filesize_as_hex)
p.write(filesize_as_hex)
f = open(datei, "rb")
try:
byte = f.read(1)
while byte != "":
p.write(byte)
byte = f.read(1)
finally:
f.close()
p.save(imageFile + "_out")
else:
p = stegwrapper.picture.open(imageFile)
size_buffer = int(math.ceil((math.ceil(math.log(p.length * 8,2))) / 8))
filesize = hex_to_integer(p.read(size_buffer))
f = open(datei, "wb")
for i in range(filesize):
#print p.read()
f.write(bytearray(p.read()))
f.close()
#im = Image.open(imageFile)
#im.getdata()