#include "stdio.h" #include "stdlib.h" #include "time.h" #include "string.h" #include "define.h" #include "unistd.h"
/* This function is used to show the instruction. */ void vInstruction(void) { printf("This program can produce a 512 byte key-file"); printf(" and/or protect the selected file.\n"); printf("Copyright 2006-2007 (C) Northeastern University at Qinhuangdao\n"); printf(" All Rights Reserved\n\n"); printf("CODETEST [-P] [-U] [-C] [-K:keyfile] [-S:source]/[-D:destination]\n\n"); printf(" -s:source Specifies the file to be coded or decoded.\n"); printf(" [-p] Use this parameter to protect your file.\n"); printf(" [-u] Use this parameter to unprotect your file.\n"); printf(" [-c] Use this parameter to creat your key file.\n"); printf(" -k:keyfile Specifies the directory and/or filename of the key file.\n"); printf(" -d:destination Specifies the directory and/or filename for the new file.\n\n"); printf("The switch -p may be preset in the program ,so I advise you that before "); printf("you use this program , make sure that you did know what you are doing."); printf("The producter \nrefuse to take any clear or potential responsibility for"); printf(" the correct or uncorre-ct use of this program.Have Fun and Good Luck.\n\n"); system("pause"); exit(0); }
/* This function is used to creat the key string */ void vCreatKey(char sTemp[]) { int iNum,iTemp; srand( (unsigned)time(NULL)); for (iNum = 0; iNum <= 511; iNum++) while((iTemp = (char)(rand()%10))!=0x00) sTemp[iNum] = iTemp; }
/* This function is used to creat the key file in ascii mode */ int iSaveFile(char sFilePath[],char sKey[]) { FILE *fpTemp; int iNum=0; if( ( fpTemp=fopen(sFilePath,"wb+") )==NULL) { printf("File Creat Error!"); return 0; } else while(iNum<512) { putc(sKey[iNum],fpTemp); iNum++; } fclose(fpTemp); printf("Key File Has Been Created\n"); return 1; }
/* This function is used to find the path of the orginal file. */ void vFindPath(char sTemp[]) { int iNum; for (iNum=strlen(sTemp);iNum>=0;iNum--) { if (sTemp[iNum]=='\\') break; sTemp[iNum]='\0'; } }
/* This function is used to find the real file name whitout path */ void vFindFileName(char sPath[],char sFilePreName[],char sFileLstName[]) { char sOri[50]; int iNum,iNumB; strcpy(sOri,sPath); vFindPath(sPath); for (iNum = strlen(sPath);iNum <= 50;iNum++) { if(sOri[iNum]=='.') { sFilePreName[iNum-strlen(sPath)] = '\0'; break; } sFilePreName[iNum-strlen(sPath)] = sOri[iNum]; }; iNumB=iNum+1; for (iNum=iNum+1;iNum <= 50;iNum++) { sFileLstName[iNum-iNumB] = sOri[iNum]; if(sOri[iNum]=='\0')break; }; }
/* This function is used to read file and save it to a string */ void vReadKeyFile(char sTemp[],char sKeyFilePath[]) { FILE *fpTemp; int iNum=0; if( ( fpTemp=fopen(sKeyFilePath,"rb") )==NULL) { printf("File Read Error!"); } else { for(iNum=0;iNum<=511;iNum++) sTemp[iNum]=fgetc(fpTemp); } }
/* This function is used to protect or unprotect the file */ void vProtectFile(char sSrcFile[],char sDstFile[],char sKeyFile[]) { FILE *fpTempA,*fpTempB; int iNum; char sTemp[512],cTemp; vReadKeyFile(sTemp,sKeyFile); if( (fpTempA = fopen(sSrcFile,"rb"))==NULL ) { printf("Open Source File Error!"); exit(0); } else if( (fpTempB = fopen(sDstFile,"wb"))==NULL ) { printf("Open Destination File Error!"); exit(0); } else { for(iNum=0;iNum<=511;iNum++) { cTemp=fgetc(fpTempA); if(feof(fpTempA)) { break; } fputc( (cTemp^sTemp[iNum]),fpTempB ); if(iNum==511)iNum=0; } fclose(fpTempA); fclose(fpTempB); } }
/* This function is used to copy one file to a temp file */ void vCopyFile(char sSrcPath[],char sDstPath[]) { FILE *fpTempA,*fpTempB; char cTemp; if ( (fpTempA=fopen(sSrcPath,"rb"))==NULL ) { printf("Source File Open Error!"); exit(0); } else if ( (fpTempB=fopen(sDstPath,"wb"))==NULL ) { printf("Temp File Creat Error!"); exit(0); } else { while(1) { cTemp=fgetc(fpTempA); if(feof(fpTempA))break; fputc(cTemp,fpTempB); } fclose(fpTempA); fclose(fpTempB); } }
/* This is the Entry Point of the program */ int main(int argc, char **argv) { char cParameter,sSrcPath[50],sDstPath[50],sKeyPath[50]; char sTemp[512],sFilePreName[20],sFileLstName[6],sFileName[20]; char sSrcPathA[50],sSrcPathB[50],sFilePreNameA[50],sFileLstNameA[50]; int iCheck=0,iNum; if (argc==1)vInstruction(); while( ( cParameter = getopt(argc,argv,"s:d:k:ucp") ) != -1 ) switch(cParameter) { case 's': strcpy(sSrcPath,optarg);iCheck |= P_SRC; break; case 'd': strcpy(sDstPath,optarg);iCheck |= P_DST; break; case 'k': strcpy(sKeyPath,optarg);iCheck |= P_KEY; break; case 'u': iCheck |= P_UP; break; case 'c': iCheck |= P_CKEY; break; case 'p': iCheck |= P_PROTECT; break; default: iCheck = 0; } switch(iCheck) { case (P_CKEY): vCreatKey(sTemp); iSaveFile("key.sys",sTemp); break; case (P_CKEY|P_DST): vCreatKey(sTemp); iSaveFile(sDstPath,sTemp); break; case (P_SRC): case (P_SRC|P_PROTECT): vCreatKey(sTemp); strcpy(sSrcPathA,sSrcPath); vFindFileName(sSrcPath,sFilePreName,sFileLstName); strcpy(sSrcPathB,sSrcPath); strcpy(sFilePreNameA,sFilePreName); strcpy(sKeyPath,strcat(sSrcPath,strcat(sFilePreName,".key"))); iSaveFile(sKeyPath,sTemp); strcpy(sDstPath,sSrcPathA); strcpy(sFilePreName,sFilePreNameA); strcpy(sSrcPath,sSrcPathB); strcpy(sFileName,strcat(sSrcPath,strcat(sFilePreName,".bak"))); vCopyFile(sSrcPathA,sFileName); vProtectFile(sFileName,sDstPath,sKeyPath); printf("Operation Down"); break; case (P_SRC|P_UP|P_DST): case (P_SRC|P_DST): case (P_SRC|P_PROTECT|P_DST): vCreatKey(sTemp); strcpy(sSrcPathA,sSrcPath); vFindFileName(sSrcPath,sFilePreName,sFileLstName); strcpy(sSrcPathB,sSrcPath); strcpy(sFilePreNameA,sFilePreName); strcpy(sKeyPath,strcat(sSrcPath,strcat(sFilePreName,".key"))); iSaveFile(sKeyPath,sTemp); strcpy(sFilePreName,sFilePreNameA); strcpy(sSrcPath,sSrcPathB); strcpy(sFileName,strcat(sSrcPath,strcat(sFilePreName,".bak"))); vCopyFile(sSrcPathA,sFileName); vProtectFile(sFileName,sDstPath,sKeyPath); printf("Operation Down"); break; case (P_SRC|P_DST|P_KEY): case (P_SRC|P_DST|P_KEY|P_UP): case (P_SRC|P_DST|P_KEY|P_PROTECT): strcpy(sSrcPathA,sSrcPath); vFindFileName(sSrcPath,sFilePreName,sFileLstName); strcpy(sFileName,strcat(sSrcPath,strcat(sFilePreName,".bak"))); vCopyFile(sSrcPathA,sFileName); vProtectFile(sFileName,sDstPath,sKeyPath); printf("Operation Down"); break; case (P_SRC|P_KEY): case (P_SRC|P_UP|P_KEY): case (P_SRC|P_PROTECT|P_KEY): strcpy(sSrcPathA,sSrcPath); vFindFileName(sSrcPath,sFilePreName,sFileLstName); strcpy(sDstPath,sSrcPathA); strcpy(sFileName,strcat(sSrcPath,strcat(sFilePreName,".bak"))); vCopyFile(sSrcPathA,sFileName); vProtectFile(sFileName,sDstPath,sKeyPath); printf("Operation Down"); break; default: vInstruction(); } return 0; }
|