Title MULTI-LEVEL ENCRYPTION UTILITY - CRYPTOMYTE Author: Karthick Raja Visitor Submitted Source Code Author Email: karthick_npgct@yahoo.com Description: Hits: 2563 Since 25th November, 2003 Code: Select and Copy the Code #include<stdio.h> #include<conio.h> #include<string.h> #include<dir.h> #include<dos.h> #define NULL 0 # define pl 100 #define PASS "efy" #define LEN 3 char key,msg[80]; unsigned int sno,fno,dno,subflag; struct ffblk file; FILE *lg; typedef struct node { char name[13]; struct node *next; }dirlist; char getkey(void); int passwordok(void); int verify(char*); int getchoice(void); int exorfile(char*,char*); int initprocess(char*,char*); void operate(char *,char *); void about(void); void rails(int ,char); void makepath(char *,char *); void main(int argn,char *argv[]) { char srcpath[pl],trgpath[pl],trgdir[pl],srcdir[pl]; int tmp; if(argn>3) { cputs("\n\tMultiLevel Encryption utility for Directories"); cputs("\n\rCall Format shold be:"); cputs("\n\rcommand [data Directory] [target Directory(optional)])"); exit(); } window(1,1,80,8); textattr(0x2a); clrscr(); about(); window(1,1,80,8); textattr(0x6e); clrscr(); if(argn<2) { cputs("Enter the source Directory=>"); scanf("%s",srcdir); } else strcpy(srcdir,argv[1]); makepath(srcpath,srcdir); if(!verify(srcpath)) { printf("\n\rCan't Locate the source Directory[%s]",srcpath); printf("\n\rNot a Valid Diretory"); exit(0); } if(argn<3) { cputs("\n\tEnter the Target Directory=>"); scanf("%s",trgdir); } else strcpy(trgdir,argv[2]); makepath(trgpath,trgdir); tmp=strlen(srcpath); if(strnicmp(srcpath,trgpath,tmp)==0&&(trgpath[tmp]=='\\'||trgpath[tmp])==' \0') { cputs("Can't create Target into Destination"); exit(0); } key=getkey(); cputs("\n\r\nInclude sub Directories if any[y/n]"); subflag=getchoice(); clrscr(); window(1,1,80,8); textattr(0x2a); clrscr(); about(); window(1,8,80,14); textattr(0x6e); clrscr(); if(!initprocess(srcpath,trgpath)) exit(0); cputs("n\r\n"); rails(80,205); window(1,14,80,23); textattr(0x6f); clrscr(); operate(srcpath,trgpath); sprintf(msg,"\n\nResults\nFolders =%u Total Files =%u[sucess=%u failed =%u]",dno,sno+fno,sno,fno); fputs(msg,lg); msg[1]='\r'; cputs(msg); cputs("\n\r View log files for details"); fclose(lg); } void operate(char srcpath[pl],char trgpath[pl]) { dirlist *start=NULL,*current=NULL,*temp; int allflag,chkflag; char src[pl],trg[pl],pth[pl]; mkdir(trgpath); if(verify(trgpath)) { dno++; sprintf(msg,"n %-60s <DIR> sucess",trgpath); fputs(msg,lg); msg[1]='\r'; cputs(msg); } else { sprintf(msg,"\n%-60s <DIR> Failed ",trgpath); fputs(msg,lg); msg[1]='\r'; cputs(msg); sprintf(msg,"\n%-60s <DIR> Skipped ",srcpath); fputs(msg,lg); msg[1]='\r'; cputs(msg); return; } strcat(trgpath,"\\"); strcpy(pth,srcpath); strcat(pth,"\\*.*"); allflag=findfirst(pth,&file,FA_DIREC); while(1) { if(allflag==-1) break; if(file.ff_name[0]=='.') { allflag=findnext(&file); continue; } if((file.ff_attrib&16)==0) { strcpy(src,srcpath); strcat(src,"\\"); strcat(src,file.ff_name); strcpy(trg,trgpath); strcat(trg,file.ff_name); sprintf(msg,"\n%-60s ",trg); fputs(msg,lg); msg[1]='\r'; cputs(msg); chkflag=exorfile(src,trg); if(chkflag) { strcpy(msg,"sucess"); sno++; } else { strcpy(msg,"Failed"); fno++; } fputs(msg,lg); cputs(msg); } else if(subflag) { if(start==NULL) { temp=(dirlist*)malloc(sizeof(dirlist)); strcpy(temp->name,file.ff_name); current=start=temp; start->next=NULL; } else { temp=(dirlist*)malloc(sizeof(dirlist)); strcpy(temp->name,file.ff_name); temp->next=NULL; current->next=temp; current=temp; } } allflag=findnext(&file); } current=start; while(current!=NULL) { strcpy(src,srcpath); strcat(src,"\\"); strcat(src,current->name); strcpy(trg,trgpath); strcat(trg,current->name); operate(src,trg); temp=current; current=temp->next; free(temp); } } int exorfile(char *s,char *t) { char ch; FILE *fs,*ft; fs=fopen(s,"rb"); ft=fopen(t,"wb"); if(fs==NULL||ft==NULL) return(0); ch=fgetc(fs); while(!feof(fs)) { fputc(ch^key,ft); ch=fgetc(fs); } fclose(fs); if(fclose(ft)==0) return(1); else return(0); } void makepath(char* dest,char *arg) { char path[pl]; if(arg[0]=='\\'||arg[0]=='.'||arg[1]==':') strcpy(dest,arg); else { getcwd(path,pl); strcpy(dest,path); if(strlen(path)>3) strcat(dest,"\\"); strcat(dest,arg); } strupr(dest); } char getkey() { char c1,c2; while(1) { cputs("\n\r\nEnter Secret Key [A Single character]"); fflush(stdin); c1=bioskey(0)&0xff; cputs("*"); if(!isprint(c1)) { clrscr(); cputs("Invalid Key"); continue; } cputs("\n\rConfirm The Secrety Key"); fflush(stdin); c2=bioskey(0)&0xff; cputs("*"); if(c1!=c2) { clrscr(); cputs("Encpyption Key Mismatch"); continue; } else return c1; } } int getchoice(void) { char z; int x,y; x=wherex(); y=wherey(); do { fflush(stdin); gotoxy(x,y); cputs("\b\b"); z=toupper(getchar()); } while(z!='Y'&& z!='N'); return(z=='Y')?1:0; } int verfiy(char *path) { int flag; flag=findfirst(path,&file,FA_DIREC); if(flag==1) return(0); if((file.ff_attrib&16)==0) return(0); else return(1); } int passwordok() { char psd[LEN+1]=PASS,pass[LEN+1],c='*'; int i=-1,flag=0; union { int code; char value[2]; }key; do { key.code=bioskey(0); if(isprint(key.value[0])) { putchar(c); i++; if (i<LEN) pass[i]=key.value[0]; } else if(key.value[0]==8 && i>=0) { cputs("\b\b"); i--; } } while(key.value[0]!=13); pass[LEN]='\0'; flag=strcmp(pass,psd); if(flag==0) return 1; else return 0; } void rails(int len,char c) { int i; for(i=0;i<len;i++) putchar(c); } int initprocess(char *p,char *t) { char file[15],pth[pl],lgf[pl],dr[4]; int tryno=3,passflag; fnsplit(p,NULL,NULL,file,NULL); fnsplit(t,dr,pth,NULL,NULL); fnmerge(lgf,dr,pth,file,".log"); do { gotoxy(25,wherey()); cputs("confirming user authentication\n\r\n"); cputs("Enter Password"); passflag=passwordok(); tryno--; if(!passflag) { clrscr(); cputs("Invalid Password\n\r"); } }while(!passflag&&tryno); lg=fopen(lgf,"w"); clrscr(); fprintf(lg,"MULTI-LEVEL DATA ENCPYPTION UTILITY\n\n By:-"); fprintf(lg,"\n\nN.P.KarthickRaja"); sprintf(msg,"Operation on Folders [%s]=>[%s]",p,t); fputs(msg,lg); cputs(msg); if(lg) printf("\n log file intialised[%s]",lgf); sprintf(msg,"\n Sub directories"); if(subflag) strcat(msg,"Included."); else strcat(msg,"Excluded"); fputs(msg,lg); msg[1]='\r'; cputs(msg); if(!passflag) { clrscr(); fprintf(lg,"UNAUTHORISED ACCESS ATTEMPTED.\nPROCESS ABORTED."); fclose(lg); cputs("\n\r\n Can't complete operation process aborted."); return 0; } return 1; } void about(void) { rails(80,205); cputs("*****"); cputs("MULTI-LEVEL ENCRYPTION UTILITY"); cputs("*****"); gotoxy(5,wherey()+3); cputs("PROJECT BY:\n"); gotoxy(47,wherey()); cputs("N.P.Karthick Raja"); gotoxy(5,wherey()+1); cputs("IV-SEM/ECE"); gotoxy(47,wherey()); cputs("Govt.College of Technology"); gotoxy(1,wherey()+1); rails(80,205); }