#include<dos.h>
#include<graphics.h>
#include<alloc.h>
#include<string.h>
#include<sys\stat.h>
#include<fcntl.h>
#include<sys\types.h>
#include<stdio.h>
#include<stdlib.h>
void displaymenu(char **menu, int count, int x1, int y1);
void savemenu(char **menu, char **buffer, int width, int count, int x1, int y1);
int getresponse(char **menu, char **buffer, int width, int count, int x1, int y1);
void highlight(char **buffer, int ch, int h, int x1, int y1);
void dehighlight(char **buffer, int ch, int h, int x1, int y1);
void getmousepos(int *button, int *x, int *y);
void initmouse();
void hidemouseptr();
void showmouseptr();
unsigned long encrypt();
void setmousepos(int x, int y);
char *menu[]={"Encrypt","Decrypt","Help","Exit"};
char source[50],pass[20],dest[50];
void main()
{
int gd=DETECT,gm,choice=1,width=0,i,count,ret_code;
char **buffer;
initgraph(&gd,&gm,"c:\\tc\\bgi");
initmouse();
showmouseptr();
settextstyle(0,0,5);
outtextxy(300,150,"EnCrYpT");
gotoxy(38,13);
printf(" CoNcEpT: mayank2cool@yahoo.com");
count=sizeof(menu)/sizeof(char *);
settextstyle(TRIPLEX_FONT,0,3);
displaymenu(menu,count,100,300);
for(i=0;i<count;i++)
{
if(textwidth(menu[i])>width)
width=textwidth(menu[i]);
}
buffer=malloc(sizeof(menu));
savemenu(menu,buffer,width,count,100,100);
while(choice!=4)
{ // 1-encrypt 2-decrypt
choice=getresponse(menu,buffer,width,count,100,100);
switch(choice)
{
case 1: setmousepos(400,150); // just for fine adjustment
setfillstyle(SOLID_FILL,0);
bar(50,250,600,450);
rectangle(50,300,600,375);
gotoxy(20,20);
printf("Enter the path of file to be encrypted:\n ");
gotoxy(10,22);
printf("Path: ");
scanf("%s",source);
bar(50,300,600,375);
rectangle(50,300,600,375);
gotoxy(20,20);
printf("Enter password for the file to be encrypted:\n ");
gotoxy(10,22);
printf("Password: ");
scanf("%s",pass);
bar(50,300,600,375);
rectangle(50,300,600,375);
gotoxy(20,20);
printf("Enter path of the output encrypted file:\n ");
gotoxy(10,22);
printf("Path: ");
scanf("%s",dest);
ret_code=encrypt();
if(ret_code!=0)
{
bar(50,300,600,375);
rectangle(50,300,600,375);
gotoxy(35,20);
printf("SUCCESS: ");
gotoxy(10,22);
printf("%s encrypted to %s using the key: %s",source,dest,pass);
}
else
{
bar(50,300,600,375);
rectangle(50,300,600,375);
gotoxy(35,20);
printf("FAILURE: ");
gotoxy(10,22);
printf("The selected file(s) couldn't be opened, please verify the path");
}
break;
case 2: setmousepos(400,150);
setfillstyle(SOLID_FILL,0);
bar(50,250,600,450);
rectangle(50,300,600,375);
gotoxy(20,20);
printf("Enter the path of already encrypted file:\n ");
gotoxy(10,22);
printf("Path: ");
scanf("%s",source);
bar(50,300,600,375);
rectangle(50,300,600,375);
gotoxy(20,20);
printf("Enter password of the ecrypted file:\n ");
gotoxy(10,22);
printf("Password: ");
scanf("%s",pass);
bar(50,300,600,375);
rectangle(50,300,600,375);
gotoxy(20,20);
printf("Enter path of the output dencrypted file:\n ");
gotoxy(10,22);
printf("Path: ");
scanf("%s",dest);
ret_code=encrypt();
if(ret_code!=0)
{
bar(50,300,600,375);
rectangle(50,300,600,375);
gotoxy(35,20);
printf("SUCCESS: ");
gotoxy(10,22);
printf("%s decrypted to %s using the key: %s",source,dest,pass);
}
else
{
bar(50,300,600,375);
rectangle(50,300,600,375);
gotoxy(35,20);
printf("FAILURE: ");
gotoxy(10,22);
printf("The selected file(s) couldn't be opened, please verify the path");
}
break;
case 3: setmousepos(400,150);
setfillstyle(SOLID_FILL,0);
bar(50,250,600,450);
rectangle(50,250,600,450);
gotoxy(37,17);
printf("HELP: ");
gotoxy(10,19);
printf("Encrypt: Enter the full path name of both, the source file and");
gotoxy(19,20);
printf("the destination file. Enter a numerical password only");
gotoxy(19,21);
printf("as the encrypting/decrypting algorithm can only use");
gotoxy(19,22);
printf("numerical values.");
gotoxy(10,23);
printf("Decrypt: Only a previously encrypted file can be decrypted using");
gotoxy(19,24);
printf("the same password as was used during encryption. Usage");
gotoxy(19,25);
printf("of wrong password will again encrypt the file.");
break;
}
}
}
void displaymenu(char **menu, int count, int x1, int y1)
{
int i,h;
h=textheight(menu[0]);
for(i=0;i<count;i++)
{
outtextxy(x1,97+i*(h+5),menu[i]);
}
}
void savemenu(char **menu, char **buffer, int width, int count, int x1, int y1)
{
int i,x2,yy1,yy2,area,h;
h=textheight(menu[0]);
for(i=0;i<count;i++)
{
x2=x1+width;
yy1=y1+i*(h+5);
yy2=y1+(i+1)*(h+5);
area=imagesize(x1,yy1,x2,yy2);
buffer[i]=malloc(area);
getimage(x1,yy1,x2,yy2,buffer[i]);
}
}
int getresponse(char **menu, char **buffer, int width, int count, int x1, int y1)
{
int choice=1, prevchoice=0, x,y,x2,y2,button;
int in,i,h;
h=textheight(menu[0]);
y2=y1+count*(h+5);
x2=x1+width;
rectangle(x1-5, y1-5, x2+5, y2+5);
while(1)
{
getmousepos(&button,&x,&y);
if(x>=x1&&x<=x2&&y>=y1&&y<=y2)
{
in=1;
for(i=1;i<=count;i++)
{
if(y<=y1+i*(h+5))
{
choice=i;
break;
}
}
if(prevchoice!=choice)
{
hidemouseptr();
highlight(buffer,choice,h,x1,y1);
if(prevchoice)
dehighlight(buffer,prevchoice,h,x1,y1);
prevchoice=choice;
showmouseptr();
}
if((button&1)==1)
{
while((button&1)==1)
getmousepos(&button,&x,&y);
if(x>=x1&&x<=x2&&y>=y1&&y<=y2)
return(choice);
}
}
else
{
if(in==1)
{
in=0;
prevchoice=0;
hidemouseptr();
dehighlight(buffer,choice,h,x1,y1);
showmouseptr();
}
}
}
}
void highlight(char **buffer, int ch, int h, int x1, int y1)
{
putimage(x1,y1+(ch-1)*(h+5),buffer[ch-1],NOT_PUT);
}
void dehighlight(char **buffer, int ch, int h, int x1, int y1)
{
putimage(x1,y1+(ch-1)*(h+5),buffer[ch-1],COPY_PUT);
}
void initmouse()
{
_AX=0;
geninterrupt(0x33);
}
void showmouseptr()
{
_AX=1;
geninterrupt(0x33);
}
void hidemouseptr()
{
_AX=2;
geninterrupt(0x33);
}
void getmousepos(int *button, int *x, int *y)
{
_AX=3;
geninterrupt(0x33);
*button=_BX;
*x=_CX;
*y=_DX;
}
void setmousepos(int x, int y)
{
_AX=4;
_CX=x;
_DX=y;
geninterrupt(0x33);
}
unsigned long encrypt()
{
int in,out;
char buffer[512];
int bytes=0,i;
unsigned long key=0,num_of_bytes=0;
char *endptr;
key=strtoul(pass,&endptr,10);
in=open(source,O_RDONLY|O_BINARY);
if(in==-1)
{
return(0);
}
else
{
out=open(dest,O_CREAT|O_BINARY|O_WRONLY,S_IWRITE);
if(out==-1)
return 0;
}
while(1)
{
bytes=read(in,buffer,512);
if(bytes>0)
{
num_of_bytes+=bytes;
for(i=0;i<bytes;i++)
{
buffer[i]=buffer[i]^key;
}
write(out,buffer,bytes);
}
else break;
}
close(in);
close(out);
return num_of_bytes;
}