Click here to Skip to main content
13,045,657 members (51,046 online)
Click here to Skip to main content
Add your own
alternative version


49 bookmarked
Posted 13 Jun 2003

File Encryption and Encrypted text embedding in an image

, 20 Sep 2003
Rate this:
Please Sign up or sign in to vote.
This is an encryption tool which encrypts all files and can embed the encrypted data in an image. For encryption three ciphers are used ASG, SG, RC4.


Sample screenshot


This is an encryption S/W which encrypts all files, text, video etc. To make the data more secure, I have embedded the cipher text (encrypted text ) in the bitmap. This means that the cipher text will not be visible to any one except the person who has embedded that. This embedding is done in the .bmp format, why? Just because this file has a large size, data can be easily hided in this format. The main advantage of this technique will be that if some one (hacker) wants to get the information, first he or she has to get the cipher text and then decrypt that! In our case, the first step is quite tough!!! - to get the cipher text in proper format without this S/W. Then there is another security check of encryption, so its more secure.

The case of ordinary files is quite common, any file can be encrypted and saved in the encrypted format. There are three encryption techniques I have used. These are called,

  1. RC4
  2. Alternating Step Generator
  3. Shrinking Generator

First of all, here is description of all these encryption techniques.


The RC4 algorithm works in two phases, key setup and ciphering. Key setup is the first and most difficult phase of this algorithm. During a N-bit key setup (N being key length), the encryption key is used to generate an encrypting variable using two arrays, state and key, and N-number of mixing operations.

For randomizing:

For i=0 to 255 
Swap Si and Sj 

To create pseudo random byte for encryption key

Swap Si and Sj 

Once the encrypting variable is produced from the key setup, it enters the ciphering phase, where it is XORed with the plain text message to create and encrypted message. It can be viewed as:

Sample screenshot

In the Visual C++ code it is done as:

DWORD inplen , keylen;

// Get the Size of string of key and plain text 


// Remove Duplicates if exists in Key



// Declaration and initialization of Variables 

char Sbox[2000], Sbox2[2000];

unsigned long i, j, t, x;

static const CString OurUnSecuredKey = "tahir_naeem" ;

static const int OurKeyLen = _tcslen(OurUnSecuredKey); 

i = j = t = x = 0;

char temp ,k;

temp ='0';

k ='0';

ZeroMemory(Sbox, sizeof(Sbox));

ZeroMemory(Sbox2, sizeof(Sbox2));

// Initialize Sbox1 with numbers from 0 to 255 

for(i = 0; i < 256U; i++)




j = 0;

// Initialize Sbox2 with Key if its is not provided use //temporary key 



for(i = 0; i < 256U ; i++)


if(j == keylen)


j = 0;


if( key.GetLength()>(j+1))

Sbox2[i]= key.GetAt(j++);





for(i = 0; i < 256U ; i++)


if(j == OurKeyLen)


j = 0;


Sbox2[i]= OurUnSecuredKey.GetAt(j++);



j = 0 ; 

// To generate a random byte 

for(i = 0; i < 256; i++)


j = (j + (unsigned long) Sbox[i] + (unsigned long) Sbox2[i]) % 256U ;

temp = Sbox[i];

Sbox[i] =Sbox[j];

Sbox[j]= temp;


i = j = 0;

// Final loop

for(x = 0; x < inplen; x++)


i = (i + 1U) % 256U;

j = (j + (unsigned long) Sbox[i]) % 256U;

// Swap the Sboxi and Sboxj 

temp = Sbox[i];

Sbox[i] =Sbox[j] ;

Sbox[j]= temp;

t = ((unsigned long) Sbox[i] + (unsigned long) Sbox[j]) % 256U ;

k = Sbox[t];

// Xor the key with Plain text & store it in a string

inp.SetAt(x , (inp.GetAt(x) ^ k));


// Return the Cipher text string

return inp;

Shrinking generator and Alternating step Generator uses random byte stream. These ciphers are best for hardware implementation.

A control LFSR R1 is used to control the output of a second LFSR R2.

The following steps are repeated until the desired key length is generated:

  1. R1 & R2 are clocked.
  2. If the output of R1 is 1, the output bit of R2 forms the part of the key stream
  3. If the output of R1 is 0, the output of R1 is discarded and clocked again
  4. Finally the key stream is XORed with the plain text stream and that is the final cipher text.

It works in the following steps:

  1. Register 1 is clocked.
  2. If the output of R1 is 1 then R2 is clocked, R3 is not clocked but its privies repeated.
  3. If the output of R1 is 0, R3 is clocked, R2 is not clocked but its previous bit is repeated
  4. When the desired length (equal to length of plain text stream) is generated, Key is XORed with the plain text.

The result is final cipher text.

The edit boxes of initial state and key takes values in the format "1,1,0,0,1" etc. One thing to be noted is that there must be equal length parameters of LFSR 1's Key and function and similarly for LFSR 2 and LFSR 3s.

First of all the input is taken from the user in the form of CString object, for each polynomial and key. If check box of Shrinking generator is checked, the boxes for third LFSR input are disabled using the function tahir1.SetReadOnly(TRUE);. It is performed as:















//When the button Process is pressed then its checked 
//that whether it is for asg or sg .It is performed as :






And the function for sg is OnSg() called for SG and EncryptAsg() for ASG.

The input is checked whether it is in a valid from or not and it is performed in the functions Check( );, testNoOfElements( ); and testFunctionLength( );. The input, after checking whether valid, is send to the class CMyAsg via its constructer tahirasg. In the implementation of tahirasg constructer ,the object of class LFSR is created and initialized with the input. The LFSR is used for LFSRs .In the constructer tahirasg the function of CMyAsg flowControl2(); is called. In this function, some other functions such as convertTextToBits() to convert in binary form the data and bitstreamMaker1(), bitstreamMaker2() are called to generate the key stream.

The function findCtBits() is used to XOR the key with plain text stream.

Then the function createCTextString() is used to convert the cipher text stream to text string form. Then the function writeFile(); is called to prompt the user to store the result in some file.

The procedure is similar in the case of Shrinking generator. The difference is when check box of Shrinking generator is checked the boxes for third LFSR input are disabled using the function tahir1.SetReadOnly(TRUE);

And the function for sg is OnSg() called for SG and EncryptAsg() for ASG. flowcontrol1() is called. In this function, the function bitstreamMaker1(); is called to generate the key stream. Then the key stream is XORed with the plain text using the function findbits();.

Finally in both cases the output is presented to user via a dialog box.

Audio Encryption & Decryption

Audio recording module, I have got from an article in the Code Project. While the playing of wav and other audio and video files is done through the function:

mysound =MCIWndCreate(GetSafeHwnd(),AfxGetInstanceHandle(),WS_VISIBLE|

mysound is HWND type's object.

Wav format:

The wav file consists of a string which has a 70 bytes header at the start. The string consists of audio information. While encryption, the header must be saved with out encryption or decryption.

Encryption & Decryption

While encrypting, the header is first extracted and stored in the file. Then the remaining portion of string is passed to the encryption or decryption function. The encryption function returns the string in the encrypted form, which is stored in the file after the 70 bytes header which was stored previously.

Image Embedding

To secure the data, the encrypted data is embedded in the bitmap format image. If the image is large there will not be any serious distortion of cipher text in the image. While encrypting, the header of image is saved first because if it is interrupted then image will not be visible.

CString ct="",tahir="";
char *temp2;
char *temp1;
char *temp3;
int control=800;
int fsize=0;
CFile myfile;
if(m_pt=="" && FileNamex!="")
CFile mfile;

// read the text file in pointer to char in temp3
int fs=mfile.GetLength();
temp3=new char[fs];
mfile.Close(); // close file ,,no further need to open
for(int d=0;d<fs;d++) // fs is file length
m_pt+=temp3[d]; // store the file in CString object m_pt
if(m_pt!="" && m_k !="")
int ctl,f=0,s=0;
// Open the bitmap file to read and store it in temp1
temp1=new char[fsize];

AfxMessageBox("NO IMAGE ");
// Encrypt the Text and store in ct using Encrypt function 

temp2=new char[fsize+ctl];

// save header
for(int i =0 ;i<401;i++)
temp2[401]=tahir.GetLength(); //store Text length 's String length at 
            //401 location of the array in the integer  format
f=(fsize-1000)/ctl; // find the interval after which the cipher text's byte 
            //is to be sent

for(int a=0;a<tahir.GetLength();a++)
for(int k=0;k<ctl&&control<fsize;k++)
control++; // Save the cipher text bytes after f byte interval 

for(int p=control;p<fsize;p++)
temp2[p]=temp1[p]; // Convert all the image's bytes and cipher text's byte in 
         //one array temp2 
SaveName="Embeded.bmp"+SaveName; // Save name 
myfile.Write(temp2,fsize); // Write the File 
AfxMessageBox("Embeding Completed"); // Prompt that Embeding is complete
else {

AfxMessageBox("Enter Plain text or File Name and Key");

Retrieving the Data back 

CString ct="",tahir="",strz="",str;
int ctl;
CFile myfile;
int fsize=myfile.GetLength(); 
int control=800,f=0;
char *temp1=new char[fsize+1];
char *temp2=new char[fsize+1];
myfile.Read(temp1,fsize); // Read the Image in char *
int t,r, q,s=0;
t=temp1[401]; // Get the saved cipher text String's size's string size 
//at 401 lacation 
for(int z=0;z<r;z++)
// Covert the Length into int format ,as it was stored in char format

for(int k=0;k<ctl&& control<fsize;k++)
control++; // Now read the cipher text in the same format as it was 
        //mixed with image


for(int a=0;a<ctl;a++)
ct+=temp2[a]; // save cipher text in CString format 
m_pt=Encrypt(ct,m_k); // Decrypt the same cipher text back into plain 
            //text using the key 
AfxMessageBox(" Retrieval Completed ");


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Tahir Naeem
No Biography provided

You may also be interested in...

Comments and Discussions

Questionimage processing area Pin
Member 126660202-Aug-16 16:21
memberMember 126660202-Aug-16 16:21 
AnswerRe: image processing area Pin
Member 126660203-Aug-16 16:04
memberMember 126660203-Aug-16 16:04 
Questionneed your help Pin
prasadkumbhar30-Apr-13 20:29
memberprasadkumbhar30-Apr-13 20:29 
sir, i am making a project on ONLINE FILE TRANSFERRING in which i am planing to use this mechanism for which i would like to have your help!

so can you please guide me!

i will be waiting for your response, otherwise you can reply on my mail id..

my email id is!
GeneralMy vote of 5 Pin
prasadkumbhar13-Apr-13 2:01
memberprasadkumbhar13-Apr-13 2:01 
QuestionSecurely encrypt files with image key Pin
gnrsu24-Jan-13 1:39
membergnrsu24-Jan-13 1:39 
Questionhow to data embedded into encrypted image using AES algorithem in java Pin
toshi sathawane1-Sep-12 3:44
membertoshi sathawane1-Sep-12 3:44 
Questiongreat Pin
k7krishna31-Aug-11 18:13
memberk7krishna31-Aug-11 18:13 
QuestionModules Pin
Member 808896218-Jul-11 4:59
memberMember 808896218-Jul-11 4:59 
GeneralRC4 Using Java Pin
saini_bravo20-Mar-11 13:45
membersaini_bravo20-Mar-11 13:45 
Generalplease help its urgent!!!!!!!!!!!!!!!!!!!!!very urgent Pin
amitnaik2003-Sep-10 12:18
memberamitnaik2003-Sep-10 12:18 
Questionhow to run Pin
avanthini12-Aug-09 18:33
memberavanthini12-Aug-09 18:33 
Questionabout Shrinking generator Pin
subait24-Jul-09 4:23
membersubait24-Jul-09 4:23 
Generalhelp Pin
kratheesh3-Mar-09 18:50
memberkratheesh3-Mar-09 18:50 
Generali've got the solutuion Pin
kaunreeves5-May-07 5:14
memberkaunreeves5-May-07 5:14 
GeneralRe: i've got the solutuion Pin
ns22118328-Feb-10 19:43
memberns22118328-Feb-10 19:43 
Generallinking errors Pin
kaunreeves28-Apr-07 23:25
memberkaunreeves28-Apr-07 23:25 
GeneralRe: linking errors Pin
Tahir Naeem29-Apr-07 1:55
memberTahir Naeem29-Apr-07 1:55 
GeneralRe: linking errors Pin
kaunreeves30-Apr-07 2:54
memberkaunreeves30-Apr-07 2:54 
Generalthanx Pin
kaunreeves24-Apr-07 22:08
memberkaunreeves24-Apr-07 22:08 
Generalplzzz help Pin
kaunreeves24-Apr-07 5:25
memberkaunreeves24-Apr-07 5:25 
GeneralRe: plzzz help Pin
Tahir Naeem24-Apr-07 5:35
memberTahir Naeem24-Apr-07 5:35 
Generalhelp in...this Pin
keshar8-Oct-05 5:02
memberkeshar8-Oct-05 5:02 
GeneralRe: help in...this Pin
Tahir Naeem12-Oct-05 20:54
memberTahir Naeem12-Oct-05 20:54 
GeneralStore LFSR state... Pin
StarBulenceGen6-Jul-05 21:28
sussStarBulenceGen6-Jul-05 21:28 
GeneralRe: Store LFSR state... Pin
Tahir Naeem8-Jul-05 23:13
memberTahir Naeem8-Jul-05 23:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 21 Sep 2003
Article Copyright 2003 by Tahir Naeem
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid