Displaying Source Code(s)
|
|
Data Encryption Program which can encrypt any file (Mini
Project)
--------------------------------------------------------------------------------
Description : The program below uses properties of traditional
DES algorithm which is widely used in data security.The program
can encrypt and decrypt any file of any format be it be a text
file,a document,a bitmap image,a Jpg image,a Video or
anything...
Code :
*
A simulated C program to encrypt or decrypt a file with L=26,
K=3 and
Buffer Stream Divisions=2 as per described approach
*/
/*
Few Notations used
R-Read
W-write
*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include"fcntl.h"
#include"types.h"
#include"stat.h"
#include<io.h>
#include<ctype.h>
#include<time.h>
#include<stdlib.h>
/*Procedure to copy a buffer stream content to another
unsigned char *strcp(unsigned char buf[],unsigned char
l[],unsigned int
byt)
{
for(int i=0;i<byt;i++)
buf[i]=l[i];
buf[i]=0;
return(buf);
}
/*Procedure To reverse buffer stream content*/
unsigned char *strrever(unsigned char buf[],unsigned int byt)
{
unsigned char l[1024];
for(int i=byt-1;i>=0;i--)
l[i]=buf[i];
l[i]=0;
for(i=0;i<byt;i++)
buf[i]=l[i];
buf[i]=0;
return(buf);
}
/* Procedure Jumble Buffer stream content*/
unsigned char *strjab(unsigned char buf[],unsigned int
byt,unsigned
char
parts,unsigned char comb)
{
unsigned char k[1024],l[1024],x[1024],y[1024];
int hlen,i,j,len,n=0;
if(parts==2&&comb==2)
{
len=byt;
hlen=len/2;
for(i=0;i<hlen;i++)
k[i]=buf[i];
k[i]=0;
j=i;
for(i=hlen;i<len;i++)
{l[n]=buf[i];n++;}
l[n]=0;
for(i=0;i<j;i++)
l[n+i]=k[i];
l[n+i]=0;
for(i=0;i<len;i++)
buf[i]=l[i];
buf[i]=0;
}
else if(parts==4)
{
}
strrever(buf,byt);
return(buf);
}
/*
A procedure to Encrypt a given file of any format
taking parameter name of the as parameter.
*/
int encrypt(unsigned char *p)
{
unsigned char buf[1024]; // buf is a buffer where
data
of file is R/W
int in,out,i; /* 'in' is a
handle
to a file which is for R a file
'out'
is a
handle to a file which is for W a file,
'i' is
a
counter */
unsigned int byt;
z:in=open(p,O_RDONLY|O_BINARY); // Opening given
file
in R mode
out=open("Ò",O_CREAT|O_BINARY|O_WRONLY,S_IWRITE);
// Opening a temp file in W mode
if(in==-1)
{
return(0);
// if file doesn't exist it quits
}
while(1)
{
byt=read(in,buf,1024); /*
The
os starts fetching data
from the file and fills it in the buffer*/
for(i=0;i<byt;i++) //
looping the buffer
{
_BL=buf[i];
asm neg bl;
_CL=0x04;
asm ror bl,cl;
_BL=_BL+0x1B0;
buf[i]=_BL; /*
Encryption logic is applied and
a single byte is encrypted */
asm xor bl,bl;
}
if(byt>0) // If
file is not empty it goes to next statement
{
write(out,buf,byt); // The
buffer
values are written in temp file
}
else //
If
file is now empty then it is quits the loop
break;
}
close(in); //
Closing the given file
close(out); //
Closing the temp file
in=open("Ò",O_RDONLY|O_BINARY);
//
Opening Temp file in R mode
out=open(p,O_CREAT|O_BINARY|O_WRONLY,S_IWRITE);
// Opening given file in W mode
if(in==-1)
//If
resource file gets deleted by an attack by an other process
{
close(in);
close(out); // The
process
restarts
goto z;
}
while(1)
{
byt=read(in,buf,1024); // R content of Temp
file
strjab(buf,byt,2,2);
if(byt>0)
write(out,buf,byt); // W content to
Given
file
else
break;
}
close(in); // Closing
the
Temp file
close(out); // Closing
the
Given file
remove("Ò"); // Deleting the
temp
file
strcp(buf," "); //Corrupting the
present Buffer value
return(1);
}
/*
A procedure to Decrypt a given file of any format
taking parameter name of the as parameter.
*/
int decrypt(unsigned char *p)
{
unsigned char buf[1024]; // buf
is
a buffer where data of file is R/W
int in,out,i;
/* 'in' is a handle to a file which is for R a file
'out' is a handle to a file which is for W a file,
'i' is a counter */
unsigned int byt;
l:in=open(p,O_RDONLY|O_BINARY);
// Opening given file in R mode
out=open("Ò",O_CREAT|O_BINARY|O_WRONLY,S_IWRITE);
// Opening a temp file in W mode
if(in==-1)
// if file doesn't exist it quits
{
return(0);
}
while(1)
{
byt=read(in,buf,1024);
// R content of Given file
for(i=0;i < byt;i++)
{
_BL=buf[i];
_BL=_BL-0x1B0;
_CL=0x04;
asm rol bl,cl;
// Decrypting logic for Decrypting a Single byte
asm neg bl;
buf[i]=_BL;
asm xor bl,bl;
}
if(byt>0)
{
write(out,buf,byt);
//W into Temp file
}
else
break;
}
close(in);
close(out);
in=open("Ò",O_RDONLY|O_BINARY); //
Opening
Temp file in R mode
out=open(p,O_CREAT|O_BINARY|O_WRONLY,S_IWRITE);
// Opening Given file in W mode
if(in==-1)
{
close(in);
close(out);
goto l;
}
while(1)
{
byt=read(in,buf,1024);
strjab(buf,byt,2,2);
//Jumbling the buffer stream
if(byt>0)
write(out,buf,byt);
// W into Given file
else
break;
}
close(in);
close(out);
remove("Ò");
// Deleting Temp file
strcp(buf," ");
// Corrupting the buffer
return(1);
}
/* The Main Function to execute the program */
int main(int argc,char *argv[])
{
clock_t start, end;
argc=argc&argc;
argc=14;
if(strcmp(argv[1],"")==0) //If No File or No Input is specified
{
clrscr();
textcolor(14);
// User Help Screen when no input is dragged
cprintf("
DRAG AN INPUT FILE TOWARDS THIS ICON");
textcolor(RED);
cputs("
DO LOCKING OR UNLOCKING BY TYPING L OR U");
textcolor(GREEN);
cputs("
IF U FRIST LOCK A FILE THEN UNLOCK IT FOR RETRIEVAL AND
VICE-VERSA");
_setcursortype(_NOCURSOR);
getch();
return(0);
}
else
{
char c;
clrscr();
// User input Screen if they drag a file
x:printf("
U WANT LOCK OR UNLOCK THE FILE (L/U):");
/*
If u press L or l for the first time,
then for retrieval press U or u and vice versa..
The if u r were pressing L or l for 'N' times without pressing U
or u
then for retrieval pressing U or u 'N' times
*/
_setcursortype(_SOLIDCURSOR);
c=getchar();
//Giving
the input whether to lock or unlock by pressing 'L' or 'U' (or)
'l' or
'u'
if(c=='L' || c=='l')
//Procedure
for locking
{
printf("
Locking....");
_setcursortype(_NOCURSOR);
start = clock(); //Starting the
clock
counter
encrypt(argv[1]);
end = clock(); // Stopping the
clock
counter
printf("
The time was: %f<BR>, (end - start) / CLK_TCK);
}
else if(c=='U' || c=='u') //Procedure for
unlocking
{
printf("
Unlocking.....");
_setcursortype(_NOCURSOR);
start=clock(); //Starting the
clock
counter
decrypt(argv[1]);
end= clock(); // Stopping the
clock counter
printf("
The time was: %f<BR>,(end - start) / CLK_TCK);
}
else
{
printf("
wrong i/p,please press a key as per given i/p's ");
_setcursortype(_NOCURSOR);
getch();
clrscr();
goto x;
}
}
return(1);
}
|
|
|