/* crypt.c written by Ben Fasenfest on: 11/22/96 Encrypts / decrypts files with a password */ #include #include #include "ansi.c" char menu(); /* displays main menu and returns choic */ int take_action(char choice); /* I changed take_action to return a value so that the program can exit from the main() function or loop back to the main menu I assumed that this had to be or else there would not be a quit function on the menu */ unsigned int code(void); /*prompts for password and returns code value */ unsigned char crypt(unsigned char in, char mode, unsigned char codefactor); /*the actual encryption function */ void aprint(char *what, int attrib); /* takes a character string and prints it to the screen with an attribute */ void header(void); /* prints the header to the top of each screen */ FILE *file=NULL, *ofile=NULL; int main(void) { char choice; int ret; do { choice=menu(); fcloseall(); } while((ret=take_action(choice)) !=0); if(isansi()==1) ansi_clrscrn(); printf("Thankyou for using\nNSCRYPT File encrypter / decrypter\n\tCopyright 1996 by NebSoft Inc.\n\tWritten by Ben Fasenfest.\n"); return ret; } char menu() { char rc; header(); printf("\t\t------------MAIN MENU------------"); printf("\n\n\t\t\ta) encrypt a file\n\t\t\tb) decrypt a file\n\t\t\tc) quit"); printf("\n\n\t\tPlease enter your choice --> "); do { rc=getche(); if(rc!='a' && rc!='b' && rc!='c') printf("\n\n\t\tThat is not a choice.\n\t\tPlease enter your choice --> "); } while(rc!='a' && rc!='b' && rc!='c'); return rc; } int take_action(char choice) { char iname[50]; char oname[50]; unsigned char ch; int c, ret=1; unsigned int cd; if(choice=='a') { header(); printf("What is the name of the file to encrypt ? "); fflush(stdin); scanf("%s", iname); if((file=fopen(iname,"r")) !=NULL) { cd=code(); printf("\n\nDo you want the encrypted file\n\n\ta) saved to another file\n\tb) printed on the screen"); do { printf("\n\nEnter your choice --> "); ch=getche(); if(ch!='a' && ch!='b') printf("\nThat is not a choice."); } while(ch!='a' && ch!='b'); if(ch=='a') { printf("\n\nPlease enter the name of the file to save to --> "); fflush(stdin); scanf("%s",oname); if((ofile=fopen(oname,"w"))!=NULL) { while((c=getc(file)) !=EOF) { fprintf(ofile, "%c", crypt(c, 'e', cd)); } printf("\n\nFile %s encrypted and saved as %s\n",iname,oname); } else { printf("\n\nThe file %s cannot be written to.\n", oname); } } else { printf("\n"); while((c=getc(file)) !=EOF) { ch=crypt(c,'e',cd); if(ch!=7) /* 7 is the bell - really annoying */ printf("%c", ch); } printf("\n\n"); } } else printf("\n\nThe file %s cannot be found.\n", iname); } else if(choice=='b') { header(); printf("What is the name of the file to decrypt ? "); fflush(stdin); scanf("%s", iname); if((file=fopen(iname,"r")) !=NULL) { cd=code(); printf("\n\nDo you want the decrypted file\n\n\ta) saved to another file\n\tb) printed on the screen"); do { printf("\n\nEnter your choice --> "); ch=getche(); if(ch!='a' && ch!='b') printf("\nThat is not a choice."); } while(ch!='a' && ch!='b'); if(ch=='a') { printf("\n\nPlease enter the name of the file to save to --> "); fflush(stdin); scanf("%s",oname); if((ofile=fopen(oname,"w"))!=NULL) { while((c=getc(file)) !=EOF); fprintf(ofile, "%c", crypt(c, 'd', cd)); printf("File %s decrypted and saved as %s\n",iname,oname); } else printf("\n\nThe file %s cannot be written to.\n", oname); } else { printf("\n"); while((c=getc(file)) !=EOF) { ch=crypt(c,'d',cd); if(ch!=7) /* 7 is the bell - really annoying */ printf("%c", ch); } printf("\n\n"); } } else printf("\n\nThe file %s cannot be found.\n", iname); } else if(choice=='c') ret= 0; if(ret==1) { printf("(Press any key to continue)"); getch(); } return ret; } unsigned int code(void) { char password[4]; char * pntr; unsigned int pnumber; int c; do { printf("\nPlease enter the password (three letters, lower case) -->"); for(c=0;c<3;c++) { password[c]=getch(); printf("*"); } if( password[0] < 97 || password[0] > 122 || password[1] < 97 || password[1] > 122 || password[2] < 97 || password[2] > 122) printf("\n\nThat password is not all lower case letters."); } while( password[0] < 97 || password[0] > 122 || password[1] < 97 || password[1] > 122 || password[2] < 97 || password[2] > 122); pntr=password; /* my formula is not bombproof - there some different passwords that will return the same value */ pnumber= (*pntr-96); pntr++; pnumber+= 2* (*pntr-96); pntr++; pnumber+= 3* (*pntr-96); pnumber+=2; return pnumber; } unsigned char crypt(unsigned char in, char mode, unsigned char codefactor) { int ch; if(mode=='e') { ch=in-codefactor; if(ch < 1) ch=ch+255; if( ch==26) /* eof character-don't want to put it to file */ ch=0; } else { if(ch==0) /* converts 0 back to eof */ ch=26; ch=in+codefactor; if(ch>255) ch=ch-255; } return ch; } void aprint(char *what, int attr) { char *pntr; int row, col; pntr=what; row=cursrow(); col=curscol(); while(*pntr!=0) { writechs(*pntr,attr,1); pntr++; poscurs(row, ++col); } } void header(void) { int attr1; int attr2; int fg,bg; if(MONOCHROME) /*detect card type */ { /* monochrome */ attr1=0x07; attr2=0x09; } else { /* color */ attr1= 0x17; attr2= 0x1F; fg=ANSI_FG_WHITE; bg=ANSI_BG_BLUE; } if(isansi()==0) clrscrn2(attr1); else ansi_clrscrn2(fg, bg); printf("\n\t\t"); aprint("NSCRYPT File encrypter / decrypter",attr2); printf("\n\t\t"); aprint("Copyright 1996 by NebSoft Inc.", attr2); printf("\n\t\t"); aprint("Written by Ben Fasenfest.", attr2); printf("\n\n"); }