You are on page 1of 53

“Image and Audio Steganography”

B.Tech Project Report


Submitted in partial fulfillment of the requirements for the degree of

Bachelor of Technology, Computer Science and Engineering

Submitted By:
Rohit Jaiswal
CSE – 06000025

SUPERVISOR:

Mr. Rajeev Srivastava


(Reader)
Department of Computer Science and Engineering
Institute of Technology
Banaras Hindu University, Varanasi - 221005
DEPARTMENT OF COMPUTER ENGINEERING
INSTITUTE OF TECHNOLOGY
BANARAS HINDU UNIVERSITY
Varanasi – 221005, INDIA

Rajeev Srivastava
Reader
Computer Science and Engineering

Ref. No. IT/CSE/2007-08/ Dated: ______________

CERTIFICATE

This is to certify that Rohit Jaiswal ( Roll no. 06000025), student of the
Department of Computer Science & Engineering , Institute of
Technology, Banaras Hindu University, Varanasi worked for his
B.Tech. Minor Project entitled “Image and Audio Steganography”
under my supervision from beginning of fourth semester of B.Tech.
program 2006-2010.

The report submitted by him embodies the literature from various


reputed resources and is an authentic record of the work carried out by
them under my supervision and guidance.

(Rajeev Srivastava)
Reader
Department of Computer Science & Engineering
Institute Of Technology
Banaras Hindu University

Page | 2
ACKNOWLEDGEMENT

It has indeed been a great privilege for me to have Mr. Rajeev Srivastava,
Department of Computer Science and Engineering, Institute of Technology,
Banaras Hindu University, as my mentor for this project. His awe-inspiring
personality, superb guidance and constant encouragement are the motive force
behind this project work. I take this opportunity to express my utmost gratitude to
him. I am also indebted to him for his timely and valuable advice.

I am highly grateful to Prof. A.K.Tripathi, Head, Department of Computer


Science and Engineering, Institute of Technology, Banaras Hindu University for
providing necessary facilities and encouraging me during the course of work.
I am thankful to all technical and non-teaching staff of the Department of
Computer Science and Engineering for their constant assistance and co-operation.

( Rohit Jaiswal )
Roll No. : 06000025
B.Tech (Part II) Semester IV
Department of Computer Science & Engineering
Institute Of Technology ,Banaras Hindu University

Page | 3
Contents

1. Abstract ...........................................................................................................5

2. Chapter 1: Introduction.....................................................................................6
Steganography and Cryptography……………………………………...6
Scope of Steganography………………………………………………...7

3. Chapter 2: Technologies Used…….................................................................8


Visual C#.NET 8.0 or higher……………………………………………8
Macromedia Flash 8……………………………………………………..8

4. Chapter 3: Theoretical Background……..........................................................9


Analysis of Digital Image………………………………………………...9
Analysis of Digital Audio………………………………………………..12

5. Chapter 4: Analysis and Design….……….......................................................14


Context Diagram………………………………………………………...14
Data Flow Diagram.........................................................................15
Encoding………………………………………………………….15
Decoding………………………………………………………….16

6. Chapter 5. Algorithms………………………………………………………………17

7. Chapter 6: Software Implementation……........................................................23


Functions…………………………………………………………………23
Functions Map…………………………………………………………..24

8. Chapter 7: Inputs and Outputs………….........................................................25

9. Chapter 8:Conclusion and Future Work...........................................................34

8. References......................................................................................................35

9. Appendix…………………………......................................................................36
Keywords……………...……………………………………………………….36
Source Code............................................................................................37

Page | 4
ABSTRACT
Steganography is the art and science of writing hidden messages in such
a way that no one apart from the sender and intended recipient even
realizes there is a hidden message.
There are often cases when it is not possible to send messages openly or in
encrypted form. This is where steganography can come into play. While
cryptography provides privacy, steganography is intended to provide secrecy.
This project deals with hiding of text behind multimedia, i.e. digital images,
wave audio, real media audio, etc. Cryptographic cipher is used before hiding text
to make this procedure more secure. The GUI has been given a funky look by
using flash.

IMPORTANT KEYWORDS

Steganography , Cryptography, Cover Medium , Plain Text, Cipher


Text, Stego Key, Stego Medium

Page | 5
CHAPTER 1

Introduction
The word steganography is of Greek origin and means "covered, or hidden
writing". Steganography is the art and science of communicating in a way which
hides the existence of the communication. By contrast, cryptography obscures the
meaning of a message, but it does not conceal the fact that there is a message.

Steganography and Cryptography

Cryptography — the science of writing in secret codes — addresses all of the


elements necessary for secure communication over an insecure channel, namely
privacy, confidentiality, key exchange, authentication, and non-repudiation. But
cryptography does not always provide safe communication. Consider an
environment where the very use of encrypted messages causes suspicion. Consider
the following text file; what else is it likely to be if not encrypted?
qANQR1DBwU4D/TlT68XXuiUQCADfj2o4b4aFYBcWumA7hR1Wvz9rbv2BR6WbEUsy
ZBIEFtjyqCd96qF38sp9IQiJIKlNaZfx2GLRWikPZwchUXxB+AA5+lqsG/ELBvRa
c9XefaYpbbAZ6z6LkOQ+eE0XASe7aEEPfdxvZZT37dVyiyxuBBRYNLN8Bphdr2zv
z/9Ak4/OLnLiJRk05/2UNE5Z0a+3lcvITMmfGajvRhkXqocavPOKiin3hv7+Vx88
uLLem2/fQHZhGcQvkqZVqXx8SmNw5gzuvwjV1WHj9muDGBY0MkjiZIRI7azWnoU9
3KCnmpR60VO4rDRAS5uGl9fioSvze+q8XqxubaNsgdKkoD+tB/4u4c4tznLfw1L2
YBS+dzFDw5desMFSo7JkecAS4NB9jAu9K+f7PTAsesCBNETDd49BTOFFTWWavAfE
gLYcPrcn4s3EriUgvL3OzPR4P1chNu6sa3ZJkTBbriDoA3VpnqG3hxqfNyOlqAka
mJJuQ53Ob9ThaFH8YcE/VqUFdw+bQtrAJ6NpjIxi/x0FfOInhC/bBw7pDLXBFNaX
HdlLQRPQdrmnWskKznOSarxq4GjpRTQo4hpCRJJ5aU7tZO9HPTZXFG6iRIT0wa47
AR5nvkEKoIAjW5HaDKiJriuWLdtN4OXecWvxFsjR32ebz76U8aLpAK87GZEyTzBx
dV+lH0hwyT/y1cZQ/E5USePP4oKWF4uqquPee1OPeFMBo4CvuGyhZXD/18Ft/53Y
WIebvdiCqsOoabK3jEfdGExce63zDI0=
=MpRf

The message above is a sentence in English that is encrypted using Pretty Good Privacy (PGP),
probably the most commonly used e-mail encryption software today.

The advantage of steganography over cryptography alone is that messages do not


attract attention to themselves, to messengers, or to recipients. Whereas the goal of
cryptography is to make data unreadable by a third party, the goal of
steganography is to hide the data from a third party. Often, steganography and
cryptography are used together to ensure security of the covert message.

Page | 6
SCOPE OF STEGANOGRAPHY

Steganography is a very interesting and advantageous science these days and has
following uses:

 Digital Watermarking
To protect a copyright on information. Photo collections, sold on CD,
often have hidden messages in the photos which allow detection of
unauthorized use. The same technique applied to DVDs is even more
effective, since the industry builds DVD recorders to detect and disallow
copying of protected DVDs.

 The simplest and oldest are used in map making, where cartographers
sometimes add a tiny fictional street to their maps, allowing them to
prosecute copycats.

 A similar trick is to add fictional names to mailing lists as a check


against unauthorized resellers.

 Steganography doesn't just apply to written forms of communication. Radio


and TV messages, from World War II to today, can be used to hide coded
or hidden messages. Some government sources suspect that Osama bin
Laden's pre-recorded videos that are re-played on TV stations around the
world contain hidden messages.

 Even biological data, stored on DNA, may be a candidate for hidden


messages, as biotech companies seek to prevent unauthorized use of their
genetically engineered material. The technology is already in place for
this: three New York researchers successfully hid a secret message in a
DNA sequence and sent it across the country.

 Steganography can also be used to allow communication within an


underground community.

 Steganography is used by some modern printers, including HP and Xerox


brand color laser printers. Tiny yellow dots are added to each page. The
dots are barely visible and contain encoded printer serial numbers, as well
as date and time stamps.

Page | 7
CHAPTER 2

TECHNOLOGIES USED

 Visual Studio.NET version 2.0 or higher or Visual C#.NET 8.0 or higher


C# is an object-oriented programming language developed by Microsoft as
part of the .NET initiative and later approved as a standard by ECMA (ECMA-
334) and ISO (ISO/IEC 23270). Anders Hejlsberg leads development of the
C# language, which has a procedural, object-oriented syntax based on C++
and includes influences from aspects of several other programming
languages (most notably Delphi and Java) with a particular emphasis on
simplification.
In this project all the framework has been designed in C#. It deals with the
main Algorithms of this project.
 Macromedia Flash Professional 8.0
Flash is commonly used to create animation, advertisements, various web
page components, to integrate video into web pages, and more recently, to
develop rich Internet applications. Files in the SWF (pronounced: Swif)
format, traditionally called "Flash movies" or "Flash games", usually have a
.swf file extension and may be an object of a web page, strictly "played" in a
standalone Flash Player, or incorporated into a Projector, a self-executing
Flash movie (with the .exe extension in Microsoft Windows).
In this project Flash has been used to give the application a stylish look and
some animaions. The SWF movies has been associated with the basic
platform of C# and has become the background of GUI.

.SWF file
.EXE file ( built in Flash)
( built in C#)

Functions
( built in C#)

Page | 8
CHAPTER 3

THEORETICAL BACKGROUND
ANALYSIS OF DIGITAL IMAGE

An image file is merely a binary file containing a binary representation of


the color or light intensity of each picture element (pixel) comprising the image.
Images typically use either 8-bit or 24-bit color.

When using 8-bit color, there is a definition of up to 256 colors forming a


palette for this image, each color denoted by an 8-bit value.

A 24-bit color scheme, as the term suggests, uses 24 bits per pixel and
provides a much better set of colors. In this case, each pixel is represented by three
bytes, each byte representing the intensity of the three primary colors red, green,
and blue (RGB), respectively. The color orange, for example, would be displayed
with red set to 100% , green set to 50% and no blue .

The size of an image file, then, is directly related to the number of pixels
and the granularity of the color definition. A typical 640x480 pix image using a
palette of 256 colors would require a file about 307 KB in size (640 • 480 bytes),
whereas a 1024x768 pix high-resolution 24-bit color image would result in a 2.36
MB file (1024 • 768 • 3 bytes).

To avoid sending files of this enormous size, a number of compression schemes


have been developed over time, notably Bitmap (BMP), Graphic Interchange
Format (GIF), and Joint Photographic Experts Group (JPEG) file types. Not
all are equally suited to steganography, however.

GIF and 8-bit BMP files employ what is known as lossless compression, a
scheme that allows the software to exactly reconstruct the original image. JPEG,
on the other hand, uses lossy compression, which means that the expanded image

Page | 9
is very nearly the same as the original but not an exact duplicate. Lossless
compression is much better suited to applications where the integrity of the
original information must be maintained, such as steganography. While JPEG can
be used for stego applications, it is more common to embed data in GIF or BMP
files.
The simplest approach to hiding data within an image file is called Least
Significant Bit (LSB) insertion. In this method, we can take the binary
representation of the hidden_data and overwrite the LSB of each byte within the
cover_image. If we are using 24-bit color, the amount of change will be minimal
and indiscernible to the human eye. As an example, suppose that we have three
adjacent pixels (nine bytes) with the following RGB encoding:
10010101 00001101 11001001
10010110 00001111 11001010
10011111 00010000 11001011

Now suppose we want to "hide" the following 9 bits of data (the hidden data
is usually compressed prior to being hidden): 101101101. If we overlay these 9 bits
over the LSB of the 9 bytes above, we get the following (where bits in bold have
been changed):
10010101 00001100 11001001
10010111 00001110 11001011
10011111 00010000 11001011

Note that we have successfully hidden 9 bits but at a cost of only changing
4, or roughly 50%, of the LSBs.

A 640x480 pixel image, the size of a small computer monitor, can hold over
400,000 characters. That's a whole novel hidden in one modest photo! This poject
involves following formats of images:

Bitmap Images (.bmp)


Joint Photographic Experts Group (.jpg)
Portable Network Graphics (.png)
Tagged Image File Format (.tif)
Windows Meta Files (.wmf)

Page | 10
BMP Image File Format
File Header
Data Size(Bytes)
File Type(BM in case of BMP files) 2
File Size 4
Reserved Byte(Always 0) 1
Reserved Byte(Always 0) 1
Bf of Bits
Size of Info Header 4
Width of Bitmap 2
Height of Bitmap 2
No of planes(1 for BMP) 1
Bit Count(Bits/Pixel, Must be 1, 4, 8, 24) 1
Type of Compression used(none) 2
Size of Image Data in Bytes 2
Horizontal Resolution in Pixels/Meter 2
Vertical Resolution in Pixels/Meter 2
No of Color’s Indexes Used 2
Important Color Indexes 2

Reading Pixel Values

Data Size(Bytes)
R(Red) 1
B(Blue) 1
G(Green) 1
Reserved Word for RGB 1

Page | 11
ANALYSIS OF DIGITAL AUDIO

Digital audio differs from traditional analog sound in that it is a discrete


rather than continuous signal. A discrete signal is created by sampling a continuous
analog signal at a specified rate. For example, the standard sampling rate for CD
digital audio is about 44kHz. The following figure illustrates a continuous analog
sound wave being sampled to produce digital audio. Note the sinusoidal nature of a
sound wave.

We emphasize the discrete nature of a digital signal in the diagram.


However, standard sampling rates are usually set at a level where the resultant
digital signal is visually indistinguishable from the original analog signal.

Digital audio is stored on a computer as a sequence of 0's and 1's. With


the right tools, it is possible to change the individual bits that make up a digital
audio file. Such precise control allows changes to be made to the binary sequence
that are not discernible to the human ear. The secret message is embedded by
slightly altering the binary sequence of a sound file.

The key innovation in recent years was to choose an innocent looking cover
that contains plenty of random information, called white noise. You can hear white
noise as a the nearly silent hiss of a blank tape playing. The secret message
replaces the white noise, and if done properly it will appear to be as random as the
noise was. Thus the basic design principle of steganographic systems is
“replacing high entropy noise with a high entropy secret transmission” .

Page | 12
Wave File Format

Wave Header

Data Size(Bytes)
Header Name(RIFF) 4
File Size 4
Format Name(wave) 4
Key Word(fmt) 4
Channels(Mono = 1, Stereo = 2) 1
Frequency 4
Blank(Less Important Data) 6
Bit Resolution 1
Blank(Less Important Data) 12

Wave Data

Data Size(Bytes)
Sound Length 1
Frequency 1
Sample Data Rest Of Wave File

Page | 13
CHAPTER 4

ANALYSIS AND DESIGN

CONTEXT DIAGRAM

PLAIN TEXT OR
COVER MEDIUM TEXT FILE STEGNO KEY
(Image or Audio File) (Text to be Hidden)

STEGNO-ENCODING
TOOL

STEGNO
MEDIUM

STEGNO-DECODING
TOOL

.DAT FILE
CONTAINING
HIDDEN TEXT

Page | 14
DATA FLOW DIAGRAM

ENCODING

STEGNO
PLAIN KEY
TEXT
Vigenere
Cipher
Arithmetic
Encripted coding
Text

Conversion
into ASCII
form Key
Conversion Information
into 8-bit
form

Bit
Stream

Read the Audio in


Frequency Values Using Key
in Byte form Information Encode
Bit Stream in the
COVER Bit/Byte Stream of
MEDIUM Cover Medium

Read the Image in


RGB values of
pixel in Bit form
Resultant
Bit/ Byte Stream

STEGNO
MEDIUM
Page | 15
DECODING

Arithmetic
STEGNO coding Key
KEY Information

Read the Audio in


Frequency Values Using Key
in Byte form Information
Decode Bit
STEGNO Stream in the
MEDIUM Bit/Byte Stream
Read the Image in
RGB values of
pixel in Bit form

Bit Stream of
Hidden Text

Conversion
into 8-bit
form Conversion
into ASCII
form

Vigenere Encripted
Cipher Text

ORIGINAL
TEXT

Page | 16
CHAPTER 5

ALGORITHMS
ENCODING

Least significant bit (LSB) coding is the simplest way to embed information in a
digital Image or Audio file. By substituting the least significant bit of each
sampling point in Audio and each pixel in Image with a binary message, LSB
coding allows for a large amount of data to be encoded.

The following diagram illustrates how the message 'HEY' is encoded in a


16-bit CD quality sample using the LSB method:

In LSB coding, the ideal data transmission rate is 1 kbps per 1 kHZ.

Page | 17
ALGORITHM FOR ENCODING

STEP 1. //Information from Stegno Key

Input the key in string datatype;


//Apply Arithmetic coding to the string key
float num = Arithmetic_coding( key);
num=num*100;
x=10*(1st digit of num);
y=10*(2st digit of num);

STEP 2. //converting Plain Text into Bit Stream

//Input Text to be hidden in string datatype;


string plaintext;
//apply encription algorithm on this string
string ciphertext = Vigenere_cipher(plaintext);
convert ciphertext ASCII form;
convert ASCII form Bit_stream;

STEP 3. //Hiding bitstream of input text in Image or Audio file


int n = length(plaintext);

//HIDING IN IMAGE FILE

get_resolution( image )  pxq;


if (p==odd)
p=p-1;
if(q==odd)
q=q-1;
R value of 1x1 pixel = n;
int g =1;
int h=1;
int m= bit lodation in Bit_stream;
char C= R or G or B;
for(int i=0;i<8n;i++)
{
If (m%3==1) C=R;
If (m%3==1) C=G;
If (m%3==1) C=B;
Page | 18
Least significant digit of C value of (g+x)x(h+y) th pixel
= Bit_stream[i];
g = g+x;
h = h+y;
}

//HIDING IN AUDIO FILE

string audio_stream= sampled audio Byte_stream;


audio_stream[0]= n;
for(int i=0;i<8n;i++)
{
If (Bit_stream[i]==0)
audio_stream[i++]=audio_stream[i++] AND “11111110”;

If (Bit_stream[i]==1)
audio_stream[i]=audio_stream[i] OR “00000001”;

Page | 19
ALGORITHM FOR DECODING

STEP 1. //Information from Stegno Key

Input the key in string datatype;


//Apply Arithmetic coding to the string key
float num = Arithmetic_coding( key);
num=num*100;
x=10*(1st digit of num);
y=10*(2st digit of num);

//from IMAGE file

STEP 2. //read stegno image


int n= R value of 1x1 pixel
int g=0;
int h=0;

STEP 3. for(int i=1; i<=n;i++)


{
g=g+x;
h=h+y;
string Bit_stream;

char C;

If (i%3==1) C=R;
If (i%3==1) C=G;
If (i%3==1) C=B;

Bit_stream[i--]=least significant digit of C value of (gxh) pixel

// from Audio file

STEP 2. //read stegno audio


string audio_stream= sampled stegno audio Byte_stream;
int n = audio_stream[0];

Page | 20
STEP 3. for(int i=1;i<8n;i++)
{
Bit_stream[i]=least significant digit of audio_stream[i];
}

STEP 4. //converting Bit Stream into Plain Text

string ciphertext ;
convert Bit_stream  ASCII form;
convert ASCII form  ciphertext;
//apply dencription algorithm on this string
string plaintext = inverse_Vigenere_cipher(ciphertext);
PRINT plaintext;

Page | 21
Vigenere Cipher

Plain text: ATTACKATDAWN


Key: LEMONLEMONLE
Cipher text: LXFOPVEFRNHR

The person sending the message chooses a keyword and repeats it until it matches
the length of the plaintext, for example, the keyword "LEMON" makes :
Key: LEMONLEMONLE

Page | 22
CHAPTER 6

IMPLEMENTATION
FUNCTIONS

 private void axShockwaveFlash1_FSCommand()


This functions is used to make connection between Flash GUI (i.e. SWF
movie) and C# codes. It also performs encoding and decoding operation for
audio medium.

 private void generate_bincodes()


It is used to convert the string into binary codes or bit stream.This function
uses the class stack to perform this task.

 private void text_encrypt()


It is used to encrypt the plain text using vigenere cipher.

 private string text_decrypt()


It is used to decrypt the text which is generated from the stegno medium.

 private float get_key()


This function is used to manipulate the stegno key.

 public Bitmap encrypt()


This is the function responsible for hiding encryted text in the image file

 public string decrypt()


This is the function responsible for decoding the stegno medium.

Page | 23
FUNCTION MAP

axShockwav
eFlash1_FS
EXE file Command()
(built in

CLASS
Stegnos text_
SWF file decrypt()
(built in Flash)

gen_binary_ get_key()
code()

text_
encrypt()
CLASS
CLASS Stack
ENCODE

CLASS
DECODE
encrypt()

decrypt()

Page | 24
CHAPTER 7

INPUTS AND OUTPUTS

ENCODING

Page | 25
Page | 26
Page | 27
Page | 28
Page | 29
DECODING

Page | 30
Page | 31
Page | 32
CHAPTER 8

CONCLUSION AND FUTURE WORK


Steganography is a really interesting subject and outside of the mainstream
cryptography and system administration that most of us deal with day after day.
“You never know if a message is hidden”, this is the dilemma that empowers
steganography. As more emphasis is placed on the areas of copyright protection,
privacy protection, and surveillance, we believe that steganography will continue
to grow in importance as a protection mechanism.

This project deals with Steganography in Image and Audio files using
Least Significant Bit (LSB) coding. This project can uplifted by
considering following measures:

 A more sophisticated approach can be implemented by using a


pseudo-random number generator to spread the message over
the sound file in a random manner.
 This project can be extended by using other media files like video and
other complex formats of audio and image.

Page | 33
REFERENCES

 “Steganography: How to Send a Secret Message By Bryan Clair”


http://www.strangehorizons.com/2001/20011008/steganography.s
html

 “A detailed look at SteganographicTechniques and their use in an


Open-SystemsEnvironment”
http://www.scribd.com/word/full/20529?access_key=34h2xr3z7wokx

 Steganography from Wikipedia


http://en.wikipedia.org/wiki/Steganography

 Image Steganography and Steganalysis


http://www.ims.nus.edu.sg/Programs/imgsci/files/memon/sing_stego.pdf

 Microsoft Tutorials for C# Express edition

Page | 34
APPENDIX

KEYWORDS AND DEFINITIONS

Steganography: The art and science of hidden writing.


Cryptography : The science of writing in secret codes.
Cover Medium: file in which we will hide the hidden_data
Plain Text : Data to be hidden.
Cipher Text : The encrypted data to be hidden.
Stego Key : Data is hidden by using this string
Stego Medium : The final resultant file after hiding data.
Bit Stream : The binary code generated from the string.

Page | 35
SOURCE CODE

PROGRAM.CS
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Steganography
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Stegnos());
}
}
}

FORM1.CS
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Steganography
{
public partial class Stegnos : Form
{
public Stegnos()
{
InitializeComponent();
this.axShockwaveFlash1.FSCommand += new
AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEventHandler(axShockw
aveFlash1_FSCommand);
}
string code_text;
string myTextFile = "", myPassword = "", myText = "";
public string myImageFile = "";
int index=0;
string mybinarycode_final = "",myAudioFile="";
string final_text = "";

Page | 36
string sampleStr = "";
Stream outputstream;
int chkval = 0;

private void axShockwaveFlash1_FSCommand(object sender,


AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)
{

if (e.command == "exit")
Application.Exit();
if (e.command == "password")
{
myPassword = e.args.ToString();
}
if (e.command == "savetextfile")
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Binary Files (*.dat)|*.dat";
if (dlg.ShowDialog() == DialogResult.OK)
{
StreamWriter fwriter = File.CreateText(dlg.FileName);
fwriter.WriteLine(final_text);
fwriter.Close();
}

}
if(e.command == "savedfile")
{
SaveFileDialog dlg = new SaveFileDialog();

dlg.Filter = "Bitmap (*.bmp)|*.bmp|Portable Network Graphics


(*.png)|*.png|Tagged Image File Format (*.tif)|*.tif|Windows MetaFile
(*.wmf)|*.wmf|Windows Audio (*.wav)|*.wav";
if (dlg.ShowDialog() == DialogResult.OK)
{
System.Drawing.Imaging.ImageFormat format =
System.Drawing.Imaging.ImageFormat.Bmp;
if (dlg.FilterIndex == 2)
format = System.Drawing.Imaging.ImageFormat.Png;
if (dlg.FilterIndex == 3)
format = System.Drawing.Imaging.ImageFormat.Tiff;
if (dlg.FilterIndex == 4)
format = System.Drawing.Imaging.ImageFormat.Wmf;
if (dlg.FilterIndex == 5)
{
chkval = 1;

Page | 37
BinaryWriter messageWriter = new BinaryWriter(new
MemoryStream());
FileStream fs = new FileStream(myAudioFile,
FileMode.Open);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
messageWriter.Write(buffer);
messageWriter.Seek(0, SeekOrigin.Begin);
Stream WaveStream = messageWriter.BaseStream;

/*SaveFileDialog dlg1 = new SaveFileDialog();


dlg1.Filter = "wav|*.wav";
if (dlg1.ShowDialog() == DialogResult.OK)
{
StreamWriter fwriter =
File.CreateText(dlg1.FileName);
fwriter.Close();
}*/

outputstream = new FileStream(dlg.FileName,


FileMode.Create);
BinaryWriter writer = new BinaryWriter(outputstream);

String str = "";


byte[] ch = new byte[100];

WaveStream.Read(ch, 0, 100);
writer.Write(ch);

generate_bincodes();
char[] message = mybinarycode_final.ToCharArray();

int messageLength = mybinarycode_final.Length;


int msglnth = messageLength;

byte[] newByte = new byte[1];


int sampleint = 4;

int count = 0;
int messagecount = 0;
//int streamcount = 44;
for(int i=0;i<6;i++)
{
WaveStream.Read(ch, 0, 1);
writer.Write((byte)(msglnth % 10));
msglnth = msglnth / 10;
}

float keygen = get_key(myPassword);


int advancesteps = (int)(keygen * 100);
if (advancesteps <50)
advancesteps = 50;
if ((fs.Length - 100) < messageLength * advancesteps)
{
MessageBox.Show("Audio File is too small to hide
this message");

Page | 38
Application.Exit();
}
while (messagecount < messageLength)
{
sampleint = WaveStream.ReadByte();
if (count % advancesteps == 0)
{
if (message[messagecount] == '1')
{
if (sampleint % 2 == 0 && sampleint < 255)
{
sampleint++;
}
else
sampleint = 255;
}
else if (messagecount < messageLength)
{
if (sampleint % 2 == 1 && sampleint < 255)
{
sampleint++;
}
else
sampleint = 254;
}
messagecount++;
}

count++;
writer.Write((byte)sampleint);
}

while ((sampleint = WaveStream.ReadByte()) >= 0)


{
writer.Write((byte)sampleint);

}
writer.Close();
}

if (chkval != 1)
{
Image img = new Bitmap(pictureBox1.Image);
this.SuspendLayout();
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
this.ResumeLayout();
img.Save(dlg.FileName, format);
}
}

Page | 39
}
if (e.command == "decodeaudiofile")
{
BinaryWriter messageWriter = new BinaryWriter(new
MemoryStream());
FileStream fs = new FileStream(e.args, FileMode.Open);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
messageWriter.Write(buffer);
messageWriter.Seek(0, SeekOrigin.Begin);
Stream WaveStream = messageWriter.BaseStream;

byte[] header = new byte[100];


WaveStream.Read(header, 0, 100);

String outputstr = "";

int count = 0;

int messagecount = 0;
float keygen = get_key(myPassword);
int advancesteps = (int)(keygen * 100);
if (advancesteps < 50)
advancesteps = 50;

int msglnth=0;
int pow=1;
for (int i = 0; i < 6; i++)
{

msglnth += WaveStream.ReadByte() * pow;


pow = pow * 10;
}

while (messagecount < msglnth)


{
int newdata = WaveStream.ReadByte();
if (count % advancesteps == 0)
{
if (newdata % 2 == 0)
outputstr += '0'.ToString();
else
outputstr += '1'.ToString();
messagecount++;
}
count++;
}
WaveStream.Close();
//MessageBox.Show(outputstr);
int length_text = outputstr.Length;
char[] original_val = outputstr.ToCharArray();
string coded_text = "";
int sum = 0;
for (int i = 0; i <= length_text - 7; i = i + 8)
{
sum = 0;

Page | 40
char[] binvalue = { '0', '0', '0', '0', '0', '0', '0', '0'
};
for (int j = 0; j <= 7; j++)
binvalue[j] = original_val[i + j];

for (int j = 0; j <= 7; j++)


{
sum += (binvalue[j] - 48) * (int)(Math.Pow((double)2,
(double)(7 - j)));
}
coded_text += ((char)sum).ToString();
}

final_text = text_decrypt(coded_text.Length,
myPassword.Length, coded_text.ToCharArray(), myPassword.ToCharArray());
}

if (e.command == "textfile")
{
myTextFile = e.args.ToString();
BinaryWriter messageWriter = new BinaryWriter(new
MemoryStream());
FileStream fs = new FileStream(e.args, FileMode.Open);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
messageWriter.Write(buffer);
messageWriter.Seek(0, SeekOrigin.Begin);

Stream MessageStream = messageWriter.BaseStream;


int MessageBuffer;
byte msg;
while ((MessageBuffer = MessageStream.ReadByte()) >= 0)
{
msg = (byte)MessageBuffer;
sampleStr += ((char)msg).ToString();
}
text_encrypt(sampleStr.Length, myPassword.Length,
sampleStr.ToCharArray(), myPassword.ToCharArray());
/*string text_line;
while ((text_line = freader.Read().ToString()) != null)
{

char[] textline = text_line.ToCharArray();


int length_text = text_line.Length;
char[] passwd = myPassword.ToCharArray();
int length_key = myPassword.Length;
text_encrypt(length_text, length_key, textline, passwd);
}
freader.Close();*/

}
if (e.command == "decodeimagefile")
{

Bitmap bitmap = (Bitmap)(new Bitmap(e.args));


string originalText = "";
float key = get_key(myPassword);

Page | 41
int codex = (int)((key * 1000000)%1000);
int codey = (int)(key * 1000);

int width = bitmap.Width;


int height = bitmap.Height;

if (codex > width)


{
while (codex > width)
codex -= width;
}

if (codey > height)


{
while (codey > height)
codey -= height;
}

if(codex == 0)
codex = 2;
if(codey == 0)
codey = 4;

decode myDecode = new decode();


originalText = myDecode.decrypt(codex, codey, bitmap);

int length_text = originalText.Length;


char[] original_val = originalText.ToCharArray();
string coded_text = "";
int sum = 0;
for (int i = 0; i <= length_text - 7; i = i + 8)
{
sum = 0;
char[] binvalue = { '0', '0', '0', '0', '0', '0', '0', '0'
};
for (int j = 0; j <= 7; j++)
binvalue[j] = original_val[i+j];

for (int j = 0; j <= 7; j++)


{
sum += (binvalue[j] - 48) * (int)(Math.Pow((double)2,
(double)(7 - j)));
}
coded_text += ((char)sum).ToString();
}

final_text = text_decrypt(coded_text.Length,
myPassword.Length, coded_text.ToCharArray(), myPassword.ToCharArray());

Page | 42
}
if (e.command == "text")
{
myText = e.args.ToString();
int length_text = myText.Length;
int length_key = myPassword.Length;
char[] textline = myText.ToCharArray();
char[] passwd = myPassword.ToCharArray();
text_encrypt(length_text, length_key, textline, passwd);

}
if(e.command == "audiofile")
{
myAudioFile = e.args;

}
if (e.command == "imagefile")
{
myImageFile = e.args.ToString();

try
{
pictureBox1.Image = new Bitmap(myImageFile);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

Bitmap bitmap = (Bitmap)pictureBox1.Image;

int width = bitmap.Width;


int height = bitmap.Height;
float key = get_key(myPassword);
int length = code_text.Length * 8;

int blue = length % 100;


int green = (length / 100) % 100;
int red = (length / 10000) % 100;
int codex = (int)((key * 1000000)%1000);
int codey = (int)(key * 1000);

if (codex > width)


{
while (codex > width)
codex -= width;
}

if (codey > height)


{
while (codey > height)
codey -= height;
}

Page | 43
Color pixelcolor;
pixelcolor = bitmap.GetPixel(codex, codey);
pixelcolor = Color.FromArgb(red, green, blue);

length = code_text.Length * 8;
generate_bincodes();

if(codex == 0)
codex = 2;
if(codey == 0)
codey = 4;
bitmap.SetPixel(codex, codey, pixelcolor);

if (length > (width * height) - 1)


{
MessageBox.Show("Text too large !!!");
Application.Exit();
}

char[] text = mybinarycode_final.ToCharArray();


encode myCode = new encode();
bitmap = myCode.encrypt(codex,codey,length,text,bitmap);

}
}

private void generate_bincodes()


{

stack s = new stack();


char ch;
int i=0, j=0, k, temp;
int myLength = code_text.Length;
char[] codedText = code_text.ToCharArray();
while(i<myLength)
{
int count = 0;
s.initial();
temp = (int)codedText[i];
while(temp!=0)
{
k = temp%2;
s.push(k);
count++;
temp/=2;
}

Page | 44
if(count<8)
for(int y=count;y<8;y++)
s.push(0);
mybinarycode_final += s.display();
i++;
j++;
}

private void text_encrypt(int length_text, int length_key, char[]


text, char[] key)
{
int i = 0, j = 0;
char temp;
while (i < length_text)
{
if (j >= length_key)
j = 0;
if ((text[i] + key[j]) < 255)
temp = (char)(text[i] + key[j]);
else
temp = (char)(text[i] + key[j] - 256);
code_text += temp.ToString();
i++;
j++;
}

private string text_decrypt(int length_text, int length_key, char[]


text, char[] key)
{
string final = "";
int i = 0, j = 0;
char temp;
while (i < length_text)
{
if (j >= length_key)
j = 0;
if ((text[i] - key[j]) < 255)
temp = (char)(text[i] - key[j]);
else
temp = (char)(text[i] - key[j] - 256);
final += temp.ToString();
i++;
j++;
}
return (final);

Page | 45
private float get_key(string pwd)
{
int k = 0, i = 0, j = 0;
float[] prob ={ 0.08f, 0.02f, 0.03f, 0.03f, 0.12f, 0.02f, 0.02f,
0.05f, 0.08f, 0.01f, 0.01f, 0.04f, 0.03f, 0.07f, 0.08f, 0.02f, 0.01f, 0.06f,
0.06f, 0.06f, 0.03f, 0.01f, 0.02f, 0.01f, 0.02f, 0.01f };
float[] range_to ={ 0.08f, 0.1f, 0.13f, 0.16f, 0.28f, 0.30f,
0.32f, 0.37f, 0.45f, 0.46f, 0.47f, 0.51f, 0.54f, 0.61f, 0.69f, 0.71f, 0.72f,
0.78f, 0.84f, 0.9f, 0.93f, 0.94f, 0.96f, 0.97f, 0.99f, 1.00f };
float[] range_from ={ 0.00f, 0.08f, 0.1f, 0.13f, 0.16f, 0.28f,
0.30f, 0.32f, 0.37f, 0.45f, 0.46f, 0.47f, 0.51f, 0.54f, 0.61f, 0.69f, 0.71f,
0.72f, 0.78f, 0.84f, 0.90f, 0.93f, 0.94f, 0.96f, 0.97f, 0.99f };
float low = 0.00f, high = 1.00f, diff = 1.00f;
int x = 0;
char[] mypassword = pwd.ToCharArray();
int pwdlength = pwd.Length;
while (k < pwdlength)
{
if (((int)mypassword[k] >= 97) && ((int)mypassword[k] <= 122))
x = (int)mypassword[k] - 97;
else if (((int)mypassword[k] >= 65) && ((int)mypassword[k] <=
90))
x = (int)mypassword[k] - 65;
diff = high - low;
high = low + diff * range_to[x];
low = low + diff * range_from[x];
k++;
}
return (low);
}

private void axShockwaveFlash1_Enter(object sender, EventArgs e)


{

}
}

ENCODE.CS

Page | 46
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Steganography
{
class encode:Stegnos
{
public Bitmap encrypt(int x, int y, int length, char[] text,Bitmap
bitmap)
{

Color pixelcolor = new Color();

int width = bitmap.Width;


int height = bitmap.Height;

int[,] transformmatrix = new int[1300, 1300];


int[,] rotatematrix = new int[1300, 1300];

for (int i = 0; i < width; i++)


{
for (int j = 0; j < height; j++)
{
transformmatrix[i, j] = 0;
rotatematrix[i, j] = 0;

}
}

transformmatrix[x, y] = 3;
int row = 0, col = 0, xprob = 0, yprob = 0, xadd = 0, yadd = 0;

row = bitmap.Height;
col = bitmap.Width;

xprob = x;
yprob = y;

if (yprob % 2 == 1)
yadd = yprob - 1;
else
yadd = yprob;
if (xprob % 2 == 1)
xadd = xprob - 1;
else
xadd = xprob;
if (row % 2 == 0)
row--;
if (col % 2 == 0)
col--;

Page | 47
if (xadd == 0)
xadd = 2;
if (yadd == 0)
yadd = 4;

int red, green, blue;

int flag = 0;

for (int i = 0; i < length; i++)


{
flag++;
x += xadd;
while (x >= col)
x -= col;
y += yadd;
while (y >= row)
y -= row;
if (transformmatrix[x, y] != 0)
{
y++;
if (y >= row)
y = 0;
}

pixelcolor = bitmap.GetPixel(x, y);


red = pixelcolor.R;
green = pixelcolor.G;
blue = pixelcolor.B;

if (text[i] == '0')
{
transformmatrix[x, y] = 2;
rotatematrix[x, y] = flag % 3;

switch (rotatematrix[x, y])


{
case 1:
if (red == 0)
{
red = 2;
}
else if (red % 2 == 1)
{
if (red == 255)
red--;
else
red++;

pixelcolor = Color.FromArgb(red, green, blue);


bitmap.SetPixel(x, y, pixelcolor);

break;
case 2:

Page | 48
if (green == 0)
{
green = 2;
}

else if (green % 2 == 1)
{
if (green == 255)
green--;
else
green++;
}

pixelcolor = Color.FromArgb(red, green, blue);


bitmap.SetPixel(x, y, pixelcolor);

break;
case 0:
if (blue == 0)
{
blue = 2;
}

else if (blue % 2 == 1)
{
if (blue == 255)
blue--;
else
blue++;
}

pixelcolor = Color.FromArgb(red, green, blue);


bitmap.SetPixel(x, y, pixelcolor);

break;
}

}
else if (text[i] == '1')
{
transformmatrix[x, y] = 1;
rotatematrix[x, y] = flag % 3;

switch (rotatematrix[x, y])


{
case 1: if (red % 2 == 0)
{
if (red == 0)
red++;
else
red--;

pixelcolor = Color.FromArgb(red, green, blue);


bitmap.SetPixel(x, y, pixelcolor);
}

Page | 49
break;
case 2: if (green % 2 == 0)
{
if (green == 0)
green++;
else
green--;
pixelcolor = Color.FromArgb(red, green, blue);
bitmap.SetPixel(x, y, pixelcolor);

break;
case 0: if (blue % 2 == 0)
{
if (blue == 0)
blue++;
else
blue--;
pixelcolor = Color.FromArgb(red, green, blue);
bitmap.SetPixel(x, y, pixelcolor);

break;
}

}
}

return (bitmap);
}
}
}

DECODE.CS
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

Page | 50
using System.Windows.Forms;

namespace Steganography
{
class decode:Stegnos
{
public string decrypt(int x,int y,Bitmap bitmap)
{

string final = "";


Color pixelcolor = new Color();

pixelcolor = bitmap.GetPixel(x, y);


int textlength = (pixelcolor.R * 10000) + (pixelcolor.G * 100) +
pixelcolor.B;
pixelcolor = Color.FromArgb(pixelcolor.R, pixelcolor.G, 0);
bitmap.SetPixel(x, y, pixelcolor);

int width = bitmap.Width;


int height = bitmap.Height;

int row = 0, col = 0, xprob = 0, yprob = 0, xadd = 0, yadd = 0;

row = bitmap.Height;
col = bitmap.Width;

xprob = x;
yprob = y;

if (yprob % 2 == 1)
yadd = yprob - 1;
else
yadd = yprob;
if (xprob % 2 == 1)
xadd = xprob - 1;
else
xadd = xprob;
if (row % 2 == 0)
row--;
if (col % 2 == 0)
col--;

if (xadd == 0)
xadd = 2;
if (yadd == 0)
yadd = 4;

int k = 0;

char[] outputstring = new char[textlength];

for (int i = 0; i < textlength; i++)


{

Page | 51
k++;
x += xadd;
while (x >= col)
x -= col;
y += yadd;
while (y >= row)
y -= row;

pixelcolor = bitmap.GetPixel(x, y);


int red = pixelcolor.R;
int green = pixelcolor.G;
int blue = pixelcolor.B;
switch (k % 3)
{
case 1: if (red == 0)
{
y++;
if (y >= row)
y = 0;

if (red % 2 == 0)
outputstring[i] = '0';
else
outputstring[i] = '1';
red = 0;
pixelcolor = Color.FromArgb(red, green, blue);
bitmap.SetPixel(x, y, pixelcolor);

break;

case 2: if (green == 0)
{
y++;
if (y >= row)
y = 0;
}
if (green % 2 == 0)
outputstring[i] = '0';
else
outputstring[i] = '1';
green = 0;
pixelcolor = Color.FromArgb(red, green, blue);
bitmap.SetPixel(x, y, pixelcolor);

break;
case 0: if (blue == 0)
{
y++;
if (y >= row)
y = 0;

}
if (blue % 2 == 0)
outputstring[i] = '0';

Page | 52
else
outputstring[i] = '1';
blue = 0;
pixelcolor = Color.FromArgb(red, green, blue);
bitmap.SetPixel(x, y, pixelcolor);

break;
}

}
for (int i = 0; i < textlength; i++)
{
final += outputstring[i].ToString();
}
return (final);

}
}
}

Page | 53

You might also like