Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KenP97 committed Jun 14, 2018
0 parents commit 91d2ce4
Show file tree
Hide file tree
Showing 460 changed files with 127,302 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .idea/lsb_steganography.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"# lsb_steganography"
137 changes: 137 additions & 0 deletions lsb_steg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/bin/env python3

from PIL import Image, ImageFont
from PIL import ImageDraw
import textwrap
import sys


def __getitem__(item):
try:
img = Image.open(item)
except IOError as e:
print("[ ! ] error accessing file : {}".format(e))
sys.exit()
return img


def __setitem__(img, outfile):
try:
img.save(outfile)
except IOError as e:
print(" [ ! ] image could not be written : {}".format(e))
sys.exit()
except Exception as e:
print(" [ ! ] unable to save : {}".format(e))
sys.exit()


def create_text_image(text, img_size):
img_text = Image.new("RGB", img_size)
font = ImageFont.load_default().font
drawer = ImageDraw.Draw(img_text)
margin = offset = 10
for line in textwrap.wrap(text, width=int(img_size[0] / 9)):
drawer.text((margin, offset), line, font=font)
offset += 10
return img_text


def decode_text(steg_img_path, outfile):
steg_img = __getitem__(steg_img_path)
red_channel = steg_img.split()[0]

x_size = steg_img.size[0]
y_size = steg_img.size[1]

decoded_img = Image.new("RGB", steg_img.size)
pixels = decoded_img.load()

for i in range(x_size):
for j in range(y_size):
if bin(red_channel.getpixel((i, j)))[-1] == '0':
pixels[i, j] = (255, 255, 255)
else:
pixels[i, j] = (0, 0, 0)

__setitem__(decoded_img, outfile)


def encode_text(cover_path, outfile, secret_message):
cover = __getitem__(cover_path)
red_cover = cover.split()[0]
green_cover = cover.split()[1]
blue_cover = cover.split()[2]

x_size = cover.size[0]
y_size = cover.size[1]

img_text = create_text_image(secret_message, cover.size)
bw_encode = img_text.convert('1')

encoded_img = Image.new("RGB", (x_size, y_size))
pixels = encoded_img.load()
for i in range(x_size):
for j in range(y_size):
red_cover_pix = bin(red_cover.getpixel((i, j)))
old_pix = red_cover.getpixel((i, j))
tencode_pix = bin(bw_encode.getpixel((i, j)))

if tencode_pix[-1] == '1':
red_cover_pix = red_cover_pix[:-1] + '1'
else:
red_cover_pix = red_cover_pix[:-1] + '0'
pixels[i, j] = (int(red_cover_pix, 2), green_cover.getpixel((i, j)), blue_cover.getpixel((i, j)))

__setitem__(encoded_img, outfile)


def usage():
print('''
Usage and Requirements
Encoding Usage : python lsb_steg.py -e < cover_img > < outfile > < secret_message >
Decoding Usage : python lsb_steg.py -d < steg_img > < outfile >
For help : python lsb_steg.py -h or python lsb_steg.py --help
*** make sure to add the extension for the outfile except the jpg(jpeg) format.
> Embed a secret message into a cover image using LSB
Positional Arguments ->
cover_img : path to cover image
outfile : path to output file
secret_message : Enter your message that needs to be hidden
> Extract a secret from a steganographic image using LSB on the red channel
Positional Arguments ->
steg_img : path of the steg image
outfile : path to the output file
A basic Python 3 Script
Dependencies ->
sudo apt-get install python3-pip
sudo pip3 install Pillow
-> ( Pillow or PIL )
sudo pip3 install textwrap
-> if textwrap is not available''')


def main():
if sys.argv[1] == '-e' and len(sys.argv) > 4:
cover_img = sys.argv[2]
outfile = sys.argv[3]
secret_message = sys.argv[4:]
secret_message = ' '.join(secret_message)
encode_text(cover_img, outfile, secret_message)
elif len(sys.argv) == 4 and sys.argv[1] == '-d':
steg_img = sys.argv[2]
outfile = sys.argv[3]
decode_text(steg_img, outfile)
elif (sys.argv[1] == '-h' or '--help') and len(sys.argv) < 3:
usage()
else:
print(sys.argv)
print("[ ! ] lsb_steg.py error : {} \nInvalid command !!! Try '-h' or '--help' ".format(sys.argv[3:]))


if __name__ == '__main__':
main()
133 changes: 133 additions & 0 deletions venv/Lib/site-packages/PIL/BdfFontFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#
# The Python Imaging Library
# $Id$
#
# bitmap distribution font (bdf) file parser
#
# history:
# 1996-05-16 fl created (as bdf2pil)
# 1997-08-25 fl converted to FontFile driver
# 2001-05-25 fl removed bogus __init__ call
# 2002-11-20 fl robustification (from Kevin Cazabon, Dmitry Vasiliev)
# 2003-04-22 fl more robustification (from Graham Dumpleton)
#
# Copyright (c) 1997-2003 by Secret Labs AB.
# Copyright (c) 1997-2003 by Fredrik Lundh.
#
# See the README file for information on usage and redistribution.
#

from __future__ import print_function

from . import Image, FontFile


# --------------------------------------------------------------------
# parse X Bitmap Distribution Format (BDF)
# --------------------------------------------------------------------

bdf_slant = {
"R": "Roman",
"I": "Italic",
"O": "Oblique",
"RI": "Reverse Italic",
"RO": "Reverse Oblique",
"OT": "Other"
}

bdf_spacing = {
"P": "Proportional",
"M": "Monospaced",
"C": "Cell"
}


def bdf_char(f):
# skip to STARTCHAR
while True:
s = f.readline()
if not s:
return None
if s[:9] == b"STARTCHAR":
break
id = s[9:].strip().decode('ascii')

# load symbol properties
props = {}
while True:
s = f.readline()
if not s or s[:6] == b"BITMAP":
break
i = s.find(b" ")
props[s[:i].decode('ascii')] = s[i+1:-1].decode('ascii')

# load bitmap
bitmap = []
while True:
s = f.readline()
if not s or s[:7] == b"ENDCHAR":
break
bitmap.append(s[:-1])
bitmap = b"".join(bitmap)

[x, y, l, d] = [int(p) for p in props["BBX"].split()]
[dx, dy] = [int(p) for p in props["DWIDTH"].split()]

bbox = (dx, dy), (l, -d-y, x+l, -d), (0, 0, x, y)

try:
im = Image.frombytes("1", (x, y), bitmap, "hex", "1")
except ValueError:
# deal with zero-width characters
im = Image.new("1", (x, y))

return id, int(props["ENCODING"]), bbox, im


##
# Font file plugin for the X11 BDF format.

class BdfFontFile(FontFile.FontFile):

def __init__(self, fp):

FontFile.FontFile.__init__(self)

s = fp.readline()
if s[:13] != b"STARTFONT 2.1":
raise SyntaxError("not a valid BDF file")

props = {}
comments = []

while True:
s = fp.readline()
if not s or s[:13] == b"ENDPROPERTIES":
break
i = s.find(b" ")
props[s[:i].decode('ascii')] = s[i+1:-1].decode('ascii')
if s[:i] in [b"COMMENT", b"COPYRIGHT"]:
if s.find(b"LogicalFontDescription") < 0:
comments.append(s[i+1:-1].decode('ascii'))

# font = props["FONT"].split("-")

# font[4] = bdf_slant[font[4].upper()]
# font[11] = bdf_spacing[font[11].upper()]

# ascent = int(props["FONT_ASCENT"])
# descent = int(props["FONT_DESCENT"])

# fontname = ";".join(font[1:])

# print("#", fontname)
# for i in comments:
# print("#", i)

while True:
c = bdf_char(fp)
if not c:
break
id, ch, (xy, dst, src), im = c
if 0 <= ch < len(self.glyph):
self.glyph[ch] = xy, dst, src, im
Loading

0 comments on commit 91d2ce4

Please sign in to comment.