Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Lesson Outcomes

Week 2

Basics of LSB and Matlab


CSM25 Secure Information Hiding
After this lesson, everyone should
be able to write readable and reusable Matlab code, using
Dr Hans Georg Schaathun comments, inline help text, and functions.
understand how any piece of information can be represented in
different ways.
University of Surrey
In particular, convert text to binary.
have implemented LSB embedding using the principles above
Spring 2008

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 1 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 2 / 36

Image and Data representations The image in the spatial domain Image and Data representations The image in the spatial domain

A tiny grayscale example The image file: pgm/pnm (spatial domain)

P2
24 24
255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 49 97 86 26 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 36 171 85 54 35 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 22 0 0 52 26
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 146 0 0 0 52 26
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 146 0 0 0 52 26
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 146 0 0 0 12 77 26
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 146 0 0 0 49 139 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 36 146 132 0 0 0 32 94 15
0 0 0 0 0 0 0 0 0 0 0 0 12 111 122 65 79 99 0 0 0 0 47 23
0 0 0 0 0 0 0 0 0 0 0 73 111 128 88 22 0 79 0 0 0 0 97 49
0 0 0 0 0 0 0 0 0 36 146 132 102 26 17 0 0 19 59 0 0 0 97 49
0 0 0 0 0 0 12 111 122 65 79 19 0 0 0 0 0 0 66 13 0 77 81 0
0 0 0 0 0 73 111 128 88 22 0 0 0 0 0 0 0 0 26 101 97 103 15 0
0 0 0 36 146 132 102 26 17 0 0 0 0 0 0 0 0 0 6 37 49 11 0 0
12 49 142 149 79 31 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
49 173 77 47 41 58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12 37 6 57 99 71 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 128 137 70 45 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 171 49 0 45 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 33 36 0 0 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 45 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 22 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 4 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 5 / 36
Image and Data representations The image in the spatial domain Image and Data representations The image in the spatial domain

Image formats Images in Matlab

Distinguish between
Load pixmap image: I = imread (filename)
Image format e.g. pixmap (spatial domain)
The pixmap is stored as a matrix.
How the image is represented.
If the file is not a pixmap,
File format
then the image is converted (by imread)
How the image is represented. possible rounding error
pnm (netpbm), png, and Microsoft-BMP contain metadata are lost (always, also for pixmap formats)
pixmap images.
TIFF can carry different image formats Some stego-systems based on JPEG are vulnerable
A file format may contain metadata + image A JPEG file is converted (decompressed) and recompressed before
embedding
JPEG is an image (and compression) format The double compression leads to artifacts which can be exploited
The JPEG standard does not define a file format You should know which image format you work with
JFIF, EXIF, and TIFF all support JPEG data

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 6 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 7 / 36

Image and Data representations The image in the spatial domain Image and Data representations Numbers in Bits

Data types The coefficients in bits

A Matlab image are usually either Take a number 217.


uint8: Integers [0, 255] Written Base 10
double: Floating point [0, 1] i.e. 217 = 7 · 100 + 1 · 101 + 2 · 102
Any range is possible, but imshow makes assumptions Least significant digit: the last/the ones
Suppose img is a uint8 image.
Convert: dblimg = double ( img ) (does not change range. Same numer 21710 = 110110012
What does imshow(dblimg) display?
Assumed range [0, 1]; anything > 1 is treated as 1. Written in Base 2
imshow(dblimg,[0,255]) lets you define the range 110110012 = 1·20 +0·21 +0·22 +1·23 +1·24 +0·25 +1·26 +1·27
Data hiding often requires type conversion Least significant bit: the last/the ones
always remember the range
Any base could be used. (8 and 16 are common.)

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 8 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 10 / 36
Image and Data representations Numbers in Bits Image and Data representations Numbers in Bits

The mod operator Matlab Demo: image

We write m = x mod N
when x = y · N + m for some y and 0 ≤ m < N.
i.e. m is the remainder of x when divided by N. pic = imread ( ’picfile.pnm’ )
Matlab: m = mod ( x, N ) whos pic
What does this have to with Least Significant bits? imshow ( pic ) ;
What is the remainder when you divide by 2 (your base)? lsbpic = mod ( pic, 2 ) ;
picture ; imshow ( lsbpic, [ 0 1 ] ) ;
lsb = x mod 2 pic7bit = pic - lsbpic
picture ; imshow ( pic7bit ) ;
You can extract k bits if you want, y = x mod 2k .
And x and y may be matrices (images).

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 11 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 12 / 36

Image and Data representations Characters in Numbers Image and Data representations Characters in Numbers

The message Message as a binary image

H e l l o W o r l d !
Message: Hello World! 48 65 6C 6C 6F 20 57 6F 72 6C 64 21
100 1000 110 0101 110 1100 110 1100 110 1111 010 0000 101 0111 110 1111 111 0010 110 1100 110 0100 010 0001
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI P1
1 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
2 SP ! " # $ % & ’ ( ) * + , - . / 12 7
3 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
4 @ A B C D E F G H I J K L M N O 1 1 1 1 1 0 1 1 1 1 1 0
5 P Q R S T U V W X Y Z [ \ ] ^ _ hello.pnm
6 ‘ a b c d e f g h i j k l m n o 0 1 1 1 1 1 0 1 1 1 1 1
7 p q r s t u v w x y z { | } ~ DEL
0 0 0 0 0 0 1 0 1 0 0 0
H e l l o W o r l d ! 1 0 1 1 1 0 0 1 0 1 0 0
48 65 6C 6C 6F 20 57 6F 72 6C 64 21 0 1 1 1 1 0 1 1 0 1 1 0
100 1000 110 0101 110 1100 110 1100 110 1111 010 0000 101 0111 110 1111 111 0010 110 1100 110 0100 010 0001
0 0 0 0 1 0 1 1 1 0 0 0
0 1 0 0 1 0 1 1 0 0 0 1

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 14 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 15 / 36
Image and Data representations Characters in Numbers LSB revisited Basic LSB

Matlab Demo: message LSB Steganography

LSB Steganography replaces the LSB plane with message data.


Using the first n pixels
msg = ’Hello World!’
Binary message: binmsg
nummsg = double ( msg )
Pixmap: pic
binmsg = de2bi ( nummsg, 7 )
lsbpic = mod ( pic, 2 )
imshow ( binmsg, [0 1] )
n = length ( binmsg(:) )
pic(1:n) = pic(1:n) - lsbpic(1:n) + binmsg(1:n)

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 16 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 18 / 36

LSB revisited Basic LSB LSB revisited Basic LSB

Matrix indexing Loops in Matlab

Dual representation in matlab


External representation: two-dimensional n × m
Avoid loops (for) in Matlab,
pic(i,j) refers to this representation
There is almost always a faster way to do it.
Internal representation: one-dimensional
pic(i) refers to this representation Operations are defined on matrices
pic(:) is all elements, in one dimension C = A + B ;
Column vector
All columns concatenated as one column
Matrices can be reshaped
foobar = reshape ( pic, [n/2,m*2] )
One-dimensional representation does not change.
pic(1:n) = pic(1:n) - lsbpic(1:n) + binmsg(1:n)

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 19 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 20 / 36
LSB revisited Keys and Randomisation LSB revisited Keys and Randomisation

Random locations Pseudo-random number generators

Random Number Generators don’t exist


... OK possible exageration ...
Basic LSB uses first n pixels
Random Number Generators are not practical
A lot of structure to fixed locations
Pseudo-Random Number Generators (PRNG)
Improvement: use n pseudo-random pixels
Deterministic sequence
determined by secret key shared by sender and receiver Computationally infeasible to calculate next number, given a
no structure in the choice of bits
subsequence
⇒ harder to steganalyse
still some structure in the changes Numbers from PRNG ‘look’ random
⇒ steganalysis is still possible The generator has a state (finite-state machine).
Invocation gives a number and moves to next state
Starting state is defined by giving a seed

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 22 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 23 / 36

LSB revisited Keys and Randomisation LSB revisited Keys and Randomisation

Permuting a vector in Matlab Optional Arguments

help rand for more information about PRNG function foobar ( arg1, arg2, arg3 )
Matlab implements three PRNG-s.
can be called with fewer arguments
B = randintrlv ( A, seed ) (Interleave) nargin contains the number of actual arguments
seeds the PRNG, and applies a pseudo-random permutation
For example, default value
Permutations can also be applied more manually
randperm gives a random permutation if nargin < 3,
intrlv can be used to permute a vector arg3 = 0 ;
Permutations are defined on (1D) vectors end
use 1D representation and reshape

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 24 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 25 / 36
LSB revisited Merits and flaws Readable and Reusable Code Functions

Is LSB a good idea? What are functions?

Simple; easy to implement


Little distortion in the spatial domain
Security: assumes that LSB-s are
Input 1ZZZZZZZ
ddddddd2
Output 1
Random ,
Input 2 / Function / Output 2
d 2 ZZZZZZZ
Independent of higher-order bits dddddd ,
Independent of other pixels Input 3 Output 3
Several authors made this assumption independently
Is it true?
Too simple; easy to detect

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 27 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 29 / 36

Readable and Reusable Code Functions Readable and Reusable Code Functions

Why do we use functions? How do we use functions?

Break down programs into smaller pieces


Each piece X = imread ( ’picfile.pnm’ )
has a clear purpose imread is a function
is easy to understand ’picfile.pnm’ is an input parameter
Black boxes the function outputs an image, which you store in X
Once designed, we don’t care about the contents The function is defined in a file imread.m
Atomic pieces You can open it with edit imread.m
reusable You needn’t know where the file is stored
can be combined in many ways You can create your own functions.
Coursework should be made clean and tidy
and that requires functions.

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 30 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 31 / 36
Readable and Reusable Code Functions Readable and Reusable Code Functions

How do we make functions? How do we make functions?


What it looks like

function [ binmsg ] = lsbget ( X, L )

use any text editor (e.g. vi(1)). % Recover a message embedded by lsbhide.
Matlab has a built-in editor (which I have hardly used)
name: functionname.m (e.g. lsbget.m) S = size ( X ) ; % Image format
put in current directory
if ( nargin < 2 ),% If only one parameter
Recognised by first line:
L = prod ( S ) ; % then set L to size of X.
function [out1,out2] = functionname ( in1, in2 )
end

binmsg = mod ( X(1:L), 2 ) ; % Extract LSB layer

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 32 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 33 / 36

Readable and Reusable Code Comments and Documentation Readable and Reusable Code Comments and Documentation

Comments and Documentation Recommended reading

Percentage marks identify comments


text from % to the end of the line is ignored
First block of comments is a help text Core Reading
displayed by help Digital Image Processing using Matlab by Gonzalez, Woods, Eddins.
should explain the usage Chapter 2.10.
First comment line is a brief description
used by lookfor
Other comments explains your code.

Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 35 / 36 Dr Hans Georg Schaathun Basics of LSB and Matlab Spring 2008 36 / 36

You might also like