-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStegVidLib.py
176 lines (141 loc) · 5.84 KB
/
StegVidLib.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
#!/usr/bin/env python
import sys
import os
import binascii
import imageio
import moviepy.editor as mpy
import binascii
import base64
import argparse
from math import ceil
from Crypto.Cipher import AES
MASTER_KEY = "CorrectHorseBatteryStapleGunHead"
class SteganographyVideoException(Exception):
pass
class StegVid():
def __init__(self):
pass
def get_max_size(self, clip):
width, height = (clip.w, clip.h)
max_frames = clip.fps * clip.duration
return (width * height * 3 / 8 / 1024) * max_frames
def encrypt_val(self, text):
secret = AES.new(MASTER_KEY)
tag_string = (str(text) + (AES.block_size - len(str(text)) % AES.block_size) * "\0")
cipher_text = base64.b64encode(secret.encrypt(tag_string))
return cipher_text
def decrypt_val(self, cipher):
secret = AES.new(MASTER_KEY)
decrypted = secret.decrypt(base64.b64decode(cipher))
result = decrypted.rstrip("\0")
return result
def file_to_binary(self, file):
with file as f:
byte = f.read()
return list(bin(int('1'+binascii.hexlify(byte), 16))[3:].zfill(8))
def str_to_binary(self, string):
return ''.join(format(ord(c), 'b').zfill(8) for c in string)
def get_lsb(self, color):
if(color % 2 == 0):
return '0'
else:
return '1'
def change_lsb(self, color, binary, index):
if(self.get_lsb(color) != binary[index]):
modified = list(bin(color)[2:].zfill(8))
modified[-1] = binary[index]
modified = int(''.join(modified), 2)
return modified
else:
return color
def compare(self, clip1, clip2):
frames1 = []
frames2 = []
[frames1.append(frame) for frame in clip1.iter_frames(dtype="uint8")]
[frames2.append(frame) for frame in clip2.iter_frames(dtype="uint8")]
for i1, frame in enumerate(frames1):
for i2, pixels in enumerate(frame):
for i3, pixel in enumerate(pixels):
if(i3 == 100):
return
pixel1 = frames1[i1][i2][i3]
pixel2 = frames2[i1][i2][i3]
print("Original: {}, Modified: {}".format(pixel1, pixel2))
def process_pixel(self, pixel, file_binary, index, bits_left):
color = 0
rgb = [pixel[0], pixel[1], pixel[2]]
for color in range(3):
rgb[color] = self.change_lsb(pixel[color], file_binary, index + color)
bits_left -= 1
if(bits_left == 0):
break
return (rgb[0], rgb[1], rgb[2])
def get_header(self, string):
results = string.split('\0', 2)
if(len(results) != 3):
return 0
return results
def analyze_header(self, clip):
output = []
for frame in clip.iter_frames(dtype="uint8"):
for pixels in frame:
for pixel in pixels:
for colors in pixel:
output.append(self.get_lsb(colors))
first_frame = int(''.join(output), 2)
filename, filesize, data = self.get_header(binascii.unhexlify('%x' % first_frame))
frames_needed = ceil(float(filesize) / len(data))
return (filename, filesize, frames_needed)
def decode(self, clip):
output = []
filename, filesize, frames_needed = self.analyze_header(clip)
print(filename, filesize, frames_needed)
for num, frame in enumerate(clip.iter_frames(dtype="uint8")):
print("Processing frame {} of {}...".format(num+1, frames_needed))
for pixels in frame:
for pixel in pixels:
for color in pixel:
output.append(self.get_lsb(color))
if(num == frames_needed - 1):
n = int(''.join(output), 2)
return self.get_header(binascii.unhexlify('%x' % n))
def show(self, file_path):
clip = mpy.VideoFileClip(str(file_path))
filename, filesize, data = self.decode(clip)
with open(filename, 'wb') as f:
f.write(data[:int(filesize)])
with open('hide.txt', 'r') as myfile:
msg = myfile.read().replace('\n', '')
os.remove('hide.txt')
return msg
def encode(self, clip, file_binary):
frames = []
[frames.append(frame) for frame in clip.iter_frames(dtype="uint8")]
bits_left = len(file_binary)
# UMMMMM LOL THIS A TAKE LOOOOONG!!!!!
count = 0
for i1, frame in enumerate(frames):
for i2, pixels in enumerate(frame):
for i3, pixel in enumerate(pixels):
frames[i1][i2][i3] = self.process_pixel(pixel, file_binary, count, bits_left)
count += 3
bits_left -= 3
if(bits_left <= 0):
return frames
def hide(self, file_path, msg):
clip = mpy.VideoFileClip(str(file_path))
text_file = open("hide.txt", "w")
text_file.write(msg)
text_file.close()
file = open('hide.txt', 'rb')
filesize = os.path.getsize('hide.txt')
file_header = os.path.basename('hide.txt') + "\0" + str(filesize) + "\0"
file_binary = self.str_to_binary(file_header) + ''.join(self.file_to_binary(file))
# file_binary = self.str_to_binary(str(msg))
frames = self.encode(clip, file_binary)
output = mpy.ImageSequenceClip(frames, fps=clip.fps)
if output.write_videofile("output.avi", codec="png"):
os.remove('hide.txt')
return "Completed"
else:
return "Failed"