

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,
- RC4
- Alternating Step Generator
- 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:

In the Visual C++ code it is done as:
DWORD inplen , keylen;
inplen=inp.GetLength();
key=removeDuplicates(key);
keylen=key.GetLength();
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));
for(i = 0; i < 256U; i++)
{
Sbox[i]=(char)i;
}
j = 0;
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 ;
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;
for(x = 0; x < inplen; x++)
{
i = (i + 1U) % 256U;
j = (j + (unsigned long) Sbox[i]) % 256U;
temp = Sbox[i];
Sbox[i] =Sbox[j] ;
Sbox[j]= temp;
t = ((unsigned long) Sbox[i] + (unsigned long) Sbox[j]) % 256U ;
k = Sbox[t];
inp.SetAt(x , (inp.GetAt(x) ^ k));
}
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:
- R1 & R2 are clocked.
- If the output of R1 is 1, the output bit of R2 forms the part of the key stream
- If the output of R1 is 0, the output of R1 is discarded and clocked again
- Finally the key stream is XORed with the plain text stream and that is the final cipher text.

It works in the following steps:
- Register 1 is clocked.
- If the output of R1 is 1 then R2 is clocked, R3 is not clocked but its privies repeated.
- If the output of R1 is 0, R3 is clocked, R2 is not clocked but its previous bit is repeated
- 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);
}
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;
mfile.Open(FileNamex,CFile::modeRead);
int fs=mfile.GetLength();
temp3=new char[fs];
mfile.Read(temp3,fs);
mfile.Close();
for(int d=0;d<fs;d++)
m_pt+=temp3[d];
}
if(m_pt!="" && m_k !="")
{
int ctl,f=0,s=0;
if(FileName!="")
{
myfile.Open(FileName,CFile::modeRead);
fsize=myfile.GetLength();
temp1=new char[fsize];
myfile.Read(temp1,fsize);
myfile.Close();
}
else
AfxMessageBox("NO IMAGE ");
ct=Encrypt(m_pt,m_k);
ctl=ct.GetLength();
temp2=new char[fsize+ctl];
for(int i =0 ;i<401;i++)
{
temp2[i]=temp1[i];
}
tahir.Format("%d",ctl);
temp2[401]=tahir.GetLength();
f=(fsize-1000)/ctl;
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++;
temp2[control]=temp1[control];
}
control++;
temp2[control]=ct[k];
}
for(int p=control;p<fsize;p++)
temp2[p]=temp1[p];
SaveName=myfile.GetFileName();
SaveName="Embeded.bmp"+SaveName;
myfile.Open(SaveName,CFile::modeCreate|CFile::modeWrite);
myfile.Write(temp2,fsize);
myfile.Close();
AfxMessageBox("Embeding Completed");
}
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);
myfile.Close();
int t,r, q,s=0;
t=temp1[401];
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()));
f=(fsize-1000)/ctl;
for(int k=0;k<ctl&& control<fsize;k++)
{
for(s=0;s<f;s++)
{
control++;
}
control++;
temp2[k]=temp1[control];
}
for(int a=0;a<ctl;a++)
ct+=temp2[a];
m_pt=Encrypt(ct,m_k);
UpdateData(FALSE);
AfxMessageBox(" Retrieval Completed ");