Click here to Skip to main content
Email Password   helpLost your password?

screenshot

Sample screenshot

Introduction

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.

RC4

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 
J=(j+Si+Sj) 
Swap Si and Sj 

To create pseudo random byte for encryption key

i=(i+1)mod256 
J=(j+Si)mod256 
Swap Si and Sj 
T=(Si+Sj)mod256 
K=St

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 


inplen=inp.GetLength();

// Remove Duplicates if exists in Key


key=removeDuplicates(key);

keylen=key.GetLength();

// 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++)

{

Sbox[i]=(char)i;

}

j = 0;

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


if(keylen)

{

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

{

if(j == keylen)

{

j = 0;

}

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

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

} 

}

else

{

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:

if(SG.GetCheck()==TRUE)

{

ASG=FALSE;

ASG1.SetCheck(BST_UNCHECKED);

tahir1.SetReadOnly(TRUE);

tahir2.SetReadOnly(TRUE);

}

else

{

ASG=TRUE;

ASG1.SetCheck(BST_CHECKED);

tahir1.SetReadOnly(FALSE);

tahir2.SetReadOnly(FALSE);

}

//When the button Process is pressed then its checked 

//that whether it is for asg or sg .It is performed as :


if(ASG==TRUE)

EncryptAsg();

else

EncryptSg();

UpdateData(FALSE);

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|
MCIWNDF_SHOWNAME,FileName);
MCIWndPlay(mysound);

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.

UpdateData(true);
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

mfile.Open(FileNamex,CFile::modeRead);
int fs=mfile.GetLength();
temp3=new char[fs];
mfile.Read(temp3,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;
if(FileName!="")
{
// Open the bitmap file to read and store it in temp1

myfile.Open(FileName,CFile::modeRead);
fsize=myfile.GetLength();
temp1=new char[fsize];

myfile.Read(temp1,fsize);
myfile.Close();
}
else
AfxMessageBox("NO IMAGE ");
// Encrypt the Text and store in ct using Encrypt function 

ct=Encrypt(m_pt,m_k); 
ctl=ct.GetLength();

temp2=new char[fsize+ctl];

// save header

for(int i =0 ;i<401;i++)
{
temp2[i]=temp1[i];
}
tahir.Format("%d",ctl);
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++)
temp2[a+402]=tahir[a];
for(int k=0;k<ctl&&control<fsize;k++)
{ 
for(s=0;s<f;s++)
{
control++; // Save the cipher text bytes after f byte interval 

temp2[control]=temp1[control];
}
control++;

temp2[control]=ct[k];
}
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=myfile.GetFileName();
SaveName="Embeded.bmp"+SaveName; // Save name 

myfile.Open(SaveName,CFile::modeCreate|CFile::modeWrite);
myfile.Write(temp2,fsize); // Write the File 

myfile.Close();
AfxMessageBox("Embeding Completed"); // Prompt that Embeding is complete

}
else {


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

Retrieving the Data back 

UpdateData(true);
CString ct="",tahir="",strz="",str;
int ctl;
CFile myfile;
myfile.Open(FileName,CFile::modeRead);
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 *

myfile.Close();
int t,r, q,s=0;
t=temp1[401]; // Get the saved cipher text String's size's string size 

//at 401 lacation 

for(q=0;q<t;q++)
tahir+=temp1[402+q];
r=8-t;
for(int z=0;z<r;z++)
strz+='0';
str=strz+tahir; 
ctl=atoi(str.GetBuffer(str.GetLength()));
tahir.Format("%d",ctl);
ctl=atoi(tahir.GetBuffer(tahir.GetLength())); 
// Covert the Length into int format ,as it was stored in char format


f=(fsize-1000)/ctl;
for(int k=0;k<ctl&& control<fsize;k++)
{
for(s=0;s<f;s++)
{
control++; // Now read the cipher text in the same format as it was 

        //mixed with image


}
control++;
temp2[k]=temp1[control];
}

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 

UpdateData(FALSE);
AfxMessageBox(" Retrieval Completed ");
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalhow to run
avanthini
19:33 12 Aug '09  
hi.. still i din run ur proj cos i dono how to run it.. can u tel me in procedure how to run and wat al shd install to run ur proj.. it ll be useful if u tel me sooon..
Thank you.. plz reply me.. Smile Confused
Questionabout Shrinking generator
subait
5:23 24 Jul '09  
Thanks very mush for your hard work i want to ask about Shring Generator code if you have the code in java i try to write the shrinking generator code in java but i don't know where to start
Regards
Generalhelp
kratheesh
19:50 3 Mar '09  
i want a readme file 4 this project..bcse i cant run it...
Generali've got the solutuion
kaunreeves
6:14 5 May '07  
Hi!

the problm wid ur code was it didnt have vfw32.lib in the project setting under the tab of Link(object/library module).... i include it in object/library module edit box n ur project was build wid out any error n u did a fantastic job by making this project!!!


well Done n ur project is really helping me alot...

Faisal...
GeneralRe: i've got the solutuion
ns221183
20:43 28 Feb '10  
Thanks it work fine..i am using Visual Studio 2005
Generallinking errors
kaunreeves
0:25 29 Apr '07  

i encounter these 2 errors wen i build ur prjct...... how it can b resolved coz its some sort of linking eror... guide me how i can remove it!!!

error-1 fisterDlg.obj : error LNK2001: unresolved external symbol _MCIWndCreateA


error-2 Debug/fister.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
GeneralRe: linking errors
Tahir Naeem
2:55 29 Apr '07  
Possibly it seems some thing wrong with settings of your visual studio. You must use VC6.

And reopen project and try to run it.

Tahir

GeneralRe: linking errors
kaunreeves
3:54 30 Apr '07  
hmmmmm......D'Oh! okay tell me can MSDN be a reason of these errors coz i didnt have MSDN in my VC6 Cd, so i havent installed MSDN... May it be the reason that my VC6 is unable to build ur project

Faisal
Generalthanx
kaunreeves
23:08 24 Apr '07  
Hi!

so nice of u ...... u gave me a very good idea. actually im doin my own code n i want to include some part of ur code so thts y i was asking u... Anywaz u r right i'll copy RC4 algo as u've said....any how one problm will still remain there that i want online encrytion too hahahaha no problem i'll search for information on this issue ... i think it will b completed. Ok i try n again thanx...SO nice of YOU!!! BYE
Generalplzzz help
kaunreeves
6:25 24 Apr '07  
hi!!

MY name is faisal. My need is only the sound encryption n decryption using RC4 technique of ur project.....can u eliminate the remaining other two techniques from ur project n snd me it ........coz i've tried it alot but it results in many errors..... plzzz send me the modified version of ur project which only encrypt n decrypt the sound.......its my final year prjct n i m this much close so plz help me out........ n plz try out the code must b in the running form..... i'll b really gr8ful 2u Mr.tahir
my mailing address is
stringsfello@hotmail.com
GeneralRe: plzzz help
Tahir Naeem
6:35 24 Apr '07  
Hello Faisal,

Thanks for your comment, But if it is your final project, should not you develop your own instead of using mine's ?

OK, if you want to use this one, Create a new project in VC, Copy RC4 Encryption function, and make a file browse dialog to encrypt the sound file. Isn't it so simple ?

If you want Online recording as well, then you'll have to bear some more trouble to copy the sound recording classes !

I am sorry i can't do this for you because it's been around 4 years that i wrote this article, dont remember every thing well. Neither i have Visual studio 6 with me to edit this.


Good luck with your project !

Cheers,

Tahir

Generalhelp in...this
keshar
6:02 8 Oct '05  
hello sir,


I am very thankful to u. u r proj is exallent.
but i want to ask u that can i do it in java.
will it ok or it will very hard.will u send me any link regarding u r help.

thank u.plz reply.Smile
GeneralRe: help in...this
Tahir Naeem
21:54 12 Oct '05  
Yes, you can do it in JAVA, if you know JAVA well, it would not be much hard. I dont know any link.
GeneralStore LFSR state...
StarBulenceGen
22:28 6 Jul '05  
Dear Tahir:

First of all, thanks a lot for your code. It is well written and interesting to read! Now coming to my problem...suppose you want to store the LFSR state of the RC4 encryption engine and re-use its state for the next encryption how will it be possible to do so with your code? I mean suppose I am encrypting 5 different plaintexts individually, now I would want to use the final LRSR state when PlainText 1 was encrypted and continue with that state to encrypt CipherText 2 and so on. The only constraint is that I cannot concatenate all the strings together and they HAVE to be encrypted individually. The key, however, will remain the same. Can you please assist me on this? I would be highly greatful! Thanks and have a great day/evening!

Seemant
GeneralRe: Store LFSR state...
Tahir Naeem
0:13 9 Jul '05  
Dear Seemant,

Thank for admiring my code and article. I am sorry i could got you exactly what you want to do with it. What you want from the state of the LFSR when Plain text 1 is encrypted ? If you want to use its contents then you can get from its variable When the plain text is encrypted.

If you need further information, let me know.




Tahir
GeneralSource Code....
Bishopds
8:08 30 Jun '05  
I have download both your program and the demo. Can a different application be built using the demo code or is it limited in some way. If the demo is not usable for build a totally new application would the actual source code be available.

I ask because I would like to build a different application but have this as a functional part of the application.

Thanks
GeneralRe: Source Code....
Anonymous
7:19 4 Jul '05  
You can use this application to build another application.

GeneralLexar JumpDrive
Anonymous
9:20 28 Mar '05  
Hey guys i hav a lexar jumpdrive tht i found and i can't get into the secure area of it, but i want to use it. does anyone know how to crack or how to alternately get access to the secure area.
GeneralRe: Lexar JumpDrive
Anonymous
3:23 29 Apr '05  
Dont know abou it
GeneralRe: Lexar JumpDrive
Tahir Naeem
1:31 18 May '05  
Dont know about it


Tahir
GeneralRC4
RalfKammerer
5:11 16 Mar '05  
Hello Tahir Naeem,

you made a great job in doing the picture of RC4 above which explains the function of RC4. I am a student and writing about WLANs and cryptography. Please help me, I would like to have the Word file from which you have created the rc4.jpg. Please email me at Ralf_Kammerer@arcor.de

Thank you very much,

Ralf
GeneralAlways on top
Tomaz Rotovnik
21:47 11 Jan '05  
I would like to debug your source code, but the application is always on top. Is it possible to turn this option off?



Tomaz Rotovnik
GeneralRe: Always on top
Tahir Naeem
4:28 13 Jan '05  
Well, it is possible to turn this optino off.But how ? , i dont exactly because i have not touched this code from 2 years !!!!

I think there is a function for it . Search it , definately you will find that . And when find also tell me Smile

Tahir
GeneralCode badly
pestking
16:05 22 Sep '03  
new without delete, inefficient......:(
GeneralRe: Code badly
Anonymous
0:50 2 Dec '03  
Its only updated to correct formetting .


Last Updated 21 Sep 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010