⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encrypt.h

📁 This project demonstrates the use of secure hash functions technique to implement a file encryption
💻 H
字号:
#include<time.h>
#include <fcntl.h>
#include<io.h>

 
struct ffblk ffblk;
char dirpath[MAXPATH]="",temppath[MAXPATH]="",tpath[MAXPATH]="",
tfile[MAXPATH]="",tempfname[25]="";

int encrypt();

int encrypt()
{

     FILE  *fpr,  *fpw;
     char opt,ch;
     int lastfile=0,row=0,col=0,i=0,pcol;
     char pwd1[40]="",pwd2[40]="",signstr[20]="";
     char* curdir;
     int totalfiles=0;
     float timetaken=0.1;
     clock_t first, second;

     strcpy(pwd1,"");
     strcpy(pwd2,"");
     strcpy(signstr,"");
     strcpy(curdir,"");

     curdir=malloc(MAXPATH);

     clrscr();

   textcolor(LIGHTMAGENTA);
   textbackground(BLUE);
   singlebox(17,1,61,3);
   gotoxy(20,2);
   textcolor(LIGHTGREEN);
   textbackground(BLUE);
   cputs("Secure Hash Functions -  Implementation");


   textcolor(CYAN);
   textbackground(BLUE);
   singlebox(2,5,78,7);
   gotoxy(6,6);
   textcolor(LIGHTRED);
   textbackground(BLUE);
   cputs("Please Backup your original files before attempting to Encrypt them.");

     drawwindow(5,9,75,14," Get Files to Encrypt ",WHITE,RED);
     gotoxy(8,11);
     textcolor(WHITE);
     textbackground(RED);
     cprintf("Enter File(s) to be Encrypted : ");

     textbackground(BLUE);
     textcolor(LIGHTMAGENTA);
     singlebox(2,17,78,24);
     gotoxy(8,17); cprintf(" Important ");


     gotoxy(5,19);
     textbackground(BLUE);
     textcolor(YELLOW);
     cprintf("Long File Names like ""C:\\Program Files\\My Projects\\..."" are not recognised!\n");
     gotoxy(5,20);
     cprintf("Please use files names like ""C:\\Progra~1\\Myproj~1\\...""\n");
     gotoxy(5,22);
     cprintf("You can use DOS  Wild cards  *  and  ?");
     gotoxy(5,23);
     cprintf("Example :   c:\\abc\\*.*      or     c:\\abc\\???.c     etc..");
     gotoxy(41,11);
     textcolor(WHITE);
     textbackground(RED);
     cputs("                                  ");
     fflush(stdin);

     if(strcmp(dirpath,"")!=0) //if1
      {
	gotoxy(41,11);
	cputs(temppath);

	if(getch()==13) //if2
	 {
	   strcpy(dirpath,temppath);
	 } //end if2
	else //else1
	{
	   gotoxy(41,11);
	   cputs("                                ");
	   gotoxy(41,11);
	   fflush(stdin);
	   gets(dirpath);
	   strcpy(temppath,dirpath);
	} //end else1

     }  //endif1
     else //else2
     {
	gotoxy(41,11);
	fflush(stdin);
	gets(dirpath);
	strcpy(temppath,dirpath);
     } //end else2


     lastfile=findfirst(dirpath,&ffblk,0);

    if(lastfile!=0) // if3
    {
       alert2();
       gotoxy(17,13);
       textbackground(RED);
       textcolor(LIGHTGREEN+BLINK);
       cputs("Incorrect Path  or  Filename ! Strike a key.");
       getch();
       return(-1);
    } //end if3

     clrscr();

   textcolor(BLACK);
   textbackground(BLUE);
   fillarea(0,0,79,23,1);
   textcolor(LIGHTRED);
   textbackground(BLUE);
   singlebox(17,1,61,3);
   gotoxy(20,2);
   textcolor(LIGHTGREEN);
   textbackground(BLUE);
   cputs("Secure Hash Functions -  Implementation ");

     drawwindow(5,4,74,19," Encrypting Files ",BLACK,LIGHTGRAY);

     if(dispfilelist(dirpath)==1)  // if 4 if the user presses Esc...
      {
	 return(1);
      } //end if4

     textcolor(YELLOW);
     textbackground(BLUE);
     gotoxy(14,22);
     cprintf("                                                        ");
     gotoxy(15,22);
     cprintf("ALL these files will be encrypted ! Proceed ? (Y/N)");
     gotoxy(69,22);
     fflush(stdin);
     opt=getche();

     if(toupper(opt)!='Y') //if5
      {
	 return(0);
      } //endif5

      strcpy(pwd1,"");
	 strcpy(pwd2,"");
	 fflush(stdin);

do
{
      drawwindow(10,8,69,16," Get Pass Word ",WHITE,RED);
      gotoxy(12,10);cprintf("Enter Password   :                             ");
      strcpy(pwd1,getpassword(32,10));
      if(strcmp(pwd1,"")==0)
	 return(0);
      gotoxy(12,12);cprintf("Confirm Password :                         ");
      strcpy(pwd2,getpassword(32,12));
      if(strcmp(pwd2,"")==0)
	 return(0);

//if 5
       if(strcmp(pwd1,pwd2)!=0)
	{
	   alert1();
	   gotoxy(22,14);
	   textcolor(WHITE+BLINK);
	   cputs("Passwords don't Match ! Please Re Enter.");
	   getch();
	   textcolor(RED);
	   gotoxy(22,14);
	   cputs("                                    ");
	   gotoxy(32,12);
	   cputs("                                   ");
	   gotoxy(32,14);
	   cputs("                                   ");
	   textcolor(WHITE);
	}   //end if5

 }while(strcmp(pwd1,pwd2)!=0); //enddo1

   strcpy(pwd1,encpwd(pwd1));

      textbackground(LIGHTGRAY);
      textcolor(LIGHTGRAY);
      gotoxy(15,7);
      cprintf(" ");
      fillarea(9,6,72,18,LIGHTGRAY);

     ///  get the directory part from path and change current directory

     lastfile = findfirst(dirpath,&ffblk,0);

     for(i=strlen(dirpath);dirpath[i]!='\\';i--)
      {
	 dirpath[i]='\0';
      }
      dirpath[i]='\0';

//      getcwd(curdir,MAXPATH);

////////////////////////////////////

   textcolor(LIGHTRED);
   textbackground(BLUE);
   singlebox(17,1,61,3);
   gotoxy(20,2);
   textcolor(LIGHTGREEN);
   textbackground(BLUE);
   cputs("Secure Hash Functions -  Implementation ");

   col = 10;
   row = 5;
   textcolor(BLUE);
   textbackground(LIGHTGRAY);

   first = clock();
//while1
   while (!lastfile)
   {
       strcpy(tfile,dirpath);
       strcat(tfile,"\\");
       strcat(tfile,ffblk.ff_name);
       gotoxy(col,row);
       textcolor(BLUE);
       chmod(tfile,-1);
       if ((fpr=fopen( tfile,"rb"))==NULL)
	{
	   cprintf("%s",ffblk.ff_name);
	   gotoxy((col+13),row);
	   cprintf("....Error in opening the file!");
	}
      else     //else2
	{
	   strcpy(signstr,"");
	   fscanf(fpr,"%s\n",signstr);
	   if(strcmp(signstr,"ENCRYPTED!")==0 && fpr!=NULL)
	    {
		 cprintf("%s",ffblk.ff_name);
		 gotoxy((col+13),row);
		 cprintf("....is  Already Encrypted...",ffblk.ff_name);
	     }
	   else   //else3
	    {

	      strcpy(tpath,dirpath); strcat(tpath,"\\enctemp.tmp");

	      if ((fpw=fopen(tpath,"wb"))==NULL)
	       {
		 gotoxy(1,24);
		 clreol();
		 gotoxy(12,24);
		 cprintf(" Can't open a Scratch file on Disk! Terminating...");
		 getch();
		 return(-1);
	       }
	      else      //else4
	       {
		 cprintf("%s",ffblk.ff_name);
		 gotoxy((col+13),row);
		 cprintf("....is being Encrypted...");
		 fprintf(fpw,"ENCRYPTED!\n");
		 fprintf(fpw,"Please do not change the contents of this file.\n");
		 fprintf(fpw,"*********************************************************************\n");
		 fprintf(fpw,"This File is Encrypted using Secure Hash Funcs\n");
		 fprintf(fpw,"*********************************************************************\n\n");

		 fprintf(fpw,"%s",pwd1);
		 rewind(fpr);
//do1
		 do
		 {
		    if(!feof(fpr))
		     {
		      ch=fgetc(fpr);
		      ch+=pwd1[0];
		      fputc(ch,fpw);
		     }
		    if(!feof(fpr))
		    {
		      ch=fgetc(fpr);
		      ch+=pwd1[1];
			fputc(ch,fpw);
		    }
		    if(!feof(fpr))
		     {
		      ch=fgetc(fpr);
		      ch+=pwd1[2];
			fputc(ch,fpw);
		     }
		   if(!feof(fpr))
		   {
		     ch=fgetc(fpr);
		     ch+=pwd1[3];
		       fputc(ch,fpw);
		   }
		   if(!feof(fpr))
		   {
		    ch=fgetc(fpr);
		    ch+=pwd1[4];
		     fputc(ch,fpw);
		   }
		  if(!feof(fpr))
		  {
		    ch=fgetc(fpr);
		    ch+=pwd1[5];
		     fputc(ch,fpw);
		  }
		  if(!feof(fpr))
		   {
		    ch=fgetc(fpr);
		    ch+=pwd1[6];
		     fputc(ch,fpw);
		   }
		  if(!feof(fpr))
		   {
		    ch=fgetc(fpr);
		    ch+=pwd1[7];
		    fputc(ch,fpw);
		   }

		}while(!feof(fpr)); // end do1

		fclose(fpr);
		fclose(fpw);

		remove(tfile);
		rename(tpath,tfile);
		chmod(tfile,0);

		cprintf("......Done! ");
		totalfiles++;
	      }// end else4

	   } // end else3
       } //end else2

  row++;
  //if1
   if(row>18)
    {
       second = clock();
       timetaken+=(second-first)/CLK_TCK;
       gotoxy(1,22);
       textbackground(BLUE);
       cprintf("                                                                                ");
       gotoxy(18,22);
       textcolor(YELLOW);
       cprintf("More Files !  Hit a Key to Continue...");
       getch();
       first=clock();
       textbackground(LIGHTGRAY);
       textcolor(LIGHTGRAY);
       gotoxy(15,7);
       cprintf(" ");
       fillarea(9,6,72,18,LIGHTGRAY);
       gotoxy(11,8);
       row=5;
    }  //end if1

     lastfile = findnext(&ffblk);
     fclose(fpr);
     fclose(fpw);

 } // end while1

  second = clock();
  timetaken+=(second-first)/CLK_TCK;
  gotoxy(1,22);
  textcolor(BLUE);
  textbackground(BLUE);
  cprintf("                                                                       ");
  textcolor(WHITE);
  gotoxy(13,22);
  cprintf("Total %d files Encrypted.  Time taken : %.2f Seconds",totalfiles,timetaken);
  gotoxy(29,24);
  textcolor(YELLOW+BLINK);
  cprintf("All Done! Hit a key..");
  getch();
  remove("enctemp.tmp");
  chdir(curdir);
  return(0);

} // end Encrypt

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -