Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Aqcurate/lsb-steganography
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.0.0
Choose a base ref
...
head repository: Aqcurate/lsb-steganography
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 9 commits
  • 3 files changed
  • 2 contributors

Commits on Jan 4, 2017

  1. Create LICENSE.txt

    Otakar Andrysek authored Jan 4, 2017
    Copy the full SHA
    64f2650 View commit details

Commits on May 14, 2018

  1. Copy the full SHA
    cadf0f3 View commit details
  2. Update README

    Aqcurate committed May 14, 2018
    Copy the full SHA
    3e1c7a7 View commit details
  3. README fix

    Aqcurate authored May 14, 2018
    Copy the full SHA
    ae23123 View commit details
  4. Copy the full SHA
    92a2aa4 View commit details
  5. Copy the full SHA
    19358d1 View commit details
  6. Fix error handling

    Aqcurate committed May 14, 2018
    Copy the full SHA
    ba58e37 View commit details
  7. Add CMYK note

    Aqcurate committed May 14, 2018
    Copy the full SHA
    0b1926c View commit details
  8. Delete outdated information

    Aqcurate authored May 14, 2018
    Copy the full SHA
    4380260 View commit details
Showing with 143 additions and 134 deletions.
  1. +21 −0 LICENSE.txt
  2. +33 −23 README.md
  3. +89 −111 steglsb.py
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 SST CTF

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
56 changes: 33 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
# Least Significant Bit Steganography


**Version:** 2.0.0
**Version:** 3.0.0

**Authors:** Andrew Quach and Stanislav Lyakhov

*All rights reserved.*

## Introduction

Steglsb allows for two functions - encoding and decoding.
Encoding masks a secret image in a cover image using LSB.
Decoding recovers a secret image from an encoded image using LSB.
Steglsb has two functions: Stenographic LSB encoding and decoding.
Encoding embeds a secret image in the last significant bits of a cover image.
Decoding extracts a secret image from an steganographic image using LSB.

Each mode of steglsb [-d/-e] requires an argument detailing the number of bits.
In other words, the number of rightmost bits of each pixel used in encoding
and decoding.

Each mode of steglsb [-d/-e] asks the user for a number of bits.
This number of bits corresponds to the amount of least significant
bits.
EX: If bits = 3, the LSB of 11011010 is 010.

EX: If the user enters '# of bits' as 3, the LSB of 11011010 is 010.
A larger number of bits specified means a higher steganographic capacity. However,
it also means a more visible secret.

## Usage

##### Encoding:
Usage: steglsb -e [cover_image] [secret_image] [output_image_name]
Usage: steglsb.py -e cover_img secret_img bits outfile [mode]

Notes: The images need to have a file extension [.jpg/.jpeg/.png].
The cover image should be larger than the secret image.
> Embed a secret image into a cover image using LSB

Positional Arguments:
cover_img - path to cover image
secret_img - path to secret image
bits - number of rightmost bits to use (between 0-8)
outfile - path to output file
Optional Arguments:
mode - image mode to use 'RGB', 'RGBA', 'L', 'CMYK'

Notes: The cover image should be larger than the secret image.
The mode defaults to the mode of the cover image.
CMYK LSB is not optimal. It is recommended to force CMYK to RGB.

##### Decoding:
Usage: steglsb -d [encoded_image] [output_image_name]
Usage: steglsb.py -d steg_img bits outfile

Notes: The images need to have a file extension [.jpg/.jpeg/.png].
> Extract a secret image from a steganographic image using LSB

Positional Arguments:
steg_img - path to steg image
bits - number of rightmost bits to use (between 0-8)
outfile - path to output file

## Usage Examples:
Encoding Example: Insert 'secret.jpg' into the image 'cover.png' located in the 'example' folder
Type the following command: './steglsb -e example/cover.png example/secret.jpg output.png'
The program will ask you to input the desired amount of Least Significant Bits
Enter an integer between 1 and 8 (Recommended: 2)
Type the following command: './steglsb.py -e example/cover.png example/secret.jpg 2 output.png'
The program will run and create the file 'output.png'
'output.png' will look like 'cover.png' but contain 'secret.jpg'

Decoding Example: There is a secret image in hidden.png
Type the following command: './steglsb -d example/hidden.png secret.png'
The program will ask to input a desired amount of Least Significant Bits
Enter an integer between 1 and 8 (Recommended: 2)
Type the following command: './steglsb.py -d example/hidden.png 2 secret.png'
The secret image is hidden in the last 2 LSBs.
A 'secret.png' file will be created containing the secret image.

@@ -56,4 +67,3 @@ This program depends on Python3 Pillow.
##### Installation Using Pip:
sudo apt-get install python3-pip
sudo pip3 install Pillow

200 changes: 89 additions & 111 deletions steglsb.py
Original file line number Diff line number Diff line change
@@ -2,139 +2,117 @@

# -*= coding: utf-8 -*-
# @author: Andrew Quach and Stanislav Lyakhov
# @website: http://sstctf.org
# @version: 2.0.0
# @version: 3.0.0
#
# Basic LSB Encoder / Decoder
#
# TODO: Comments

import sys
from PIL import Image, ImageMath

def get_image_data(image_name):
try:
image = Image.open(image_name)
except IOError:
print("Error processing images. Please check to see valid images was provided.")
sys.exit()

red, green, blue, *alpha = image.split()
return (red, green, blue)

def encode_data(cover_rgb, secret_rgb, bits):
encoded_rgb = []

for k in range(3):
encoded_rgb.append(ImageMath.eval("convert((cover_rgb & (256 - 2**bits)) + ((secret_rgb & (256 - 2**(8 - bits)) - 1) >> 8 - bits), 'L')", cover_rgb = cover_rgb[k], secret_rgb = secret_rgb[k], bits = bits))

return tuple(encoded_rgb)

def decode_data(rgb, bits):
decoded_rgb = []

for k in range(3):
decoded_rgb.append(ImageMath.eval("convert((rgb & 2**bits - 1) << 8 - bits, 'L')", rgb = rgb[k], bits = bits))

return tuple(decoded_rgb)

def reassemble_image(rgb):
reassembled_image = Image.merge("RGB", (rgb[0], rgb[1], rgb[2]))
return reassembled_image

def encode(cover, secret, output):
bits = get_number_bits()

cover_rgb = get_image_data(cover)
secret_rgb = get_image_data(secret)

encoded_rgb = encode_data(cover_rgb, secret_rgb, bits)
encoded_image = reassemble_image(encoded_rgb)
original_image = reassemble_image(cover_rgb)

original_image.paste(encoded_image, (0,0))
original_image.save(output)

def decode(encoded, output):
bits = get_number_bits()

rgb = get_image_data(encoded)
decoded_rgb = decode_data(rgb, bits)
decoded_image = reassemble_image(decoded_rgb)

decoded_image.save(output)
class LSB:
SUPPORTED = ['RGB', 'RGBA', 'L', 'CMYK']
def _set_bits(self, bits):
self.bits = int(bits)
if not 0 <= self.bits <= 8:
print('[!] Number of bits needs to be between 0-8.')
sys.exit()

def get_number_bits():
try:
bits = int(input('How many bits do you wish to use?\n> '))
except ValueError:
print("Please enter a number between 0-8")
sys.exit()
else:
if 0 <= bits <= 8:
return bits
else:
print("Please enter a number between 0-8")
def _get_image(self, path, itype):
try:
img = Image.open(path)
except IOError as e:
print('[!] {} image could not be opened.'.format(itype.title()))
print('[!] {}'.format(e))
sys.exit()

def check_extension(argv):
for args in argv[2:]:
if not args.lower().endswith(('.png','.jpg','.jpeg')):
print("Error processing image names. Please check to see valid extensions were provided.")
print('[*] {} image mode: {}'.format(itype.title(), img.mode))
if img.mode not in self.SUPPORTED:
print('[!] Nonsupported image mode.')
sys.exit()
return img

def _save_img(self, img, outfile):
try:
img.save(outfile)
except IOError as e:
print('[!] {} image could not be written.'.format(outfile))
print('[!] {}'.format(e))
sys.exit()
except Exception as e:
print('[!] Unable to save file.')
print('[!] {}'.format(e))
sys.exit()

class LSBEncode(LSB):
def __init__(self, cover, secret, bits, outfile, mode=None):
print('[*] Attempting LSB Encoding with bits = {}'.format(bits))
self._set_bits(bits)
self.outfile = outfile
self.cover = self._get_image(cover, 'cover')
if mode != None:
self.cover = self.cover.convert(mode.upper())
print('[*] Converted cover image mode to {}.'.format(self.cover.mode))
self.secret = self._get_image(secret, 'secret').convert(self.cover.mode)
print('[*] Converted secret image mode to {}.'.format(self.cover.mode))
self._encode_img()

def _encode_img(self):
c = self.cover.split()
s = self.secret.split()
expr = 'convert((c & (256 - 2**bits)) + ((s & (256 - 2**(8 - bits)) - 1) >> (8 - bits)), "L")'
out = [ImageMath.eval(expr, c = c[k], s = s[k], bits = self.bits) for k in range(len(c))]
out = Image.merge(self.cover.mode, out)
self.cover.paste(out, (0, 0))
self._save_img(self.cover, self.outfile)
print('[*] Created outfile at {}'.format(self.outfile))

class LSBDecode(LSB):
def __init__(self, steg, bits, outfile):
print('[*] Attempting LSB Decoding with bits = {}'.format(bits))
self._set_bits(bits)
self.outfile = outfile
self.steg = self._get_image(steg, 'steg')
self._decode_img()

def _decode_img(self):
s = self.steg.split()
expr = 'convert((s & 2**bits - 1) << (8 - bits), "L")'
out = [ImageMath.eval(expr, s = s[k], bits = self.bits) for k in range(len(s))]
out = Image.merge(self.steg.mode, out)
self._save_img(out, self.outfile)
print('[*] Created outfile at {}'.format(self.outfile))

def usage():
print("""
Usage:
Encoding:
steglsb -e [cover_image] [secret_image] [output_image_name]
print('''Encoding Usage: steglsb -e cover_img secret_img bits outfile [mode]
Decoding:
steglsb -d [encoded_image] [output_image_name]
> Embed a secret image into a cover image using LSB
Help:
steglsb -h
Positional Arguments:
cover_img - path to cover image
secret_img - path to secret image
bits - number of rightmost bits to use (between 0-8)
outfile - path to output file
Optional Arguments:
mode - image mode to use 'RGB', 'RGBA', 'L', 'CMYK'
Valid File Formats:
JPG, PNG
""")
def help():
print("""
Steglsb allows for two functions - encoding and decoding.
Decoding Usage: steglsb -d steg_img bits outfile
Encoding:
Usage: steglsb -e [cover_image] [secret_image] [output_image_name]
> Extract a secret image from a steganographic image using LSB
Notes: The images need to have a file extension [.jpg/.jpeg/.png].
The images should be the same dimensions.
(The program only takes the overlapping dimensions.)
Decoding:
Usage: steglsb -d [encoded_image] [output_image_name]
Notes: The images need to have a file extension [.jpg/.jpeg/.png].
Each mode of steglsb [-d/-e] asks the user for a number of bits.
This number of bits corresponds to the amount of least significant
bits.
EX: If the user enters '# of bits' as 3, the LSB of 11011010 is 010.
""")
Positional Arugments:
steg_img - path to steg image
bits - number of rightmost bits to use (between 0-8)
outfile - path to output file
''')

def main():
check_extension(sys.argv)

if len(sys.argv) == 5 and sys.argv[1] == '-e':
encode(sys.argv[2], sys.argv[3], sys.argv[4])
elif len(sys.argv) == 4 and sys.argv[1] == '-d':
decode(sys.argv[2], sys.argv[3])
elif len(sys.argv) > 1 and sys.argv[1]=='-h':
help()
if len(sys.argv) in (6, 7) and sys.argv[1] == '-e':
LSBEncode(*sys.argv[2:])
elif len(sys.argv) == 5 and sys.argv[1] == '-d':
LSBDecode(*sys.argv[2:])
else:
usage()

if __name__ == '__main__':
main()