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

📄 encrypt.c

📁 一个简单的加解密档案的程序提供命令列的方式下命令可适合初学者学习
💻 C
字号:
/*	This program is free software; you can redistribute it and/or modify	it under the terms of the GNU General Public License as published by	the Free Software Foundation; either version 2 of the License, or	(at your option) any later version.	This program is distributed in the hope that it will be useful,	but WITHOUT ANY WARRANTY; without even the implied warranty of	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	GNU General Public License for more details.	You should have received a copy of the GNU General Public License	along with this program; if not, write to the Free Software	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA	*//*	Included files	*/#include <stdio.h>int main(int argc, char *argv[]) {/*	Declared Variables	*/	FILE *fileone;	FILE *filetwo;	FILE *filetemp;	char filesource[1024] = "";	char filedestin[1025] = "";	char instring[1024] = "";	char outstring[1024] = "";	char password[1024] = "";    char hexstring[64][5];	int inchar = 0;	int outchar = 0;	int taskdo = 0;	int allpassed = 0;	int loop = 0;	int passchar = 0;	int passcount = 0;    int hashvalue = 0;	int hashchar[8];/*	Encrypt or Decrypt file	*/	if (argc < 3) {                             /*	If the source filename, and a password are passed then don't ask, assume encrypt, if a destination file exists then assume decrypt	*/		printf("\n\nEncrypt/Decrypt   : ");		inchar = getch();		if (inchar == 'd' || inchar == 'D') {			taskdo = 1;		} else {			taskdo = 0;			inchar = 'E';		}		printf("%c", inchar);	} else {		strcpy(filesource, argv[1]);            /*	Get source file name	*/		strcpy(password, argv[2]);              /*	Get password	*/		passcount = strlen(password);		if (argc > 3) {			allpassed = 2;			taskdo = 1;			strcpy(filedestin, argv[3]);        /*	Get destination file	*/		} else {			allpassed = 1;			taskdo = 0;		}	}/*	Get source file name	*/	if (allpassed == 0) {		if (taskdo == 1) {			sprintf(outstring, "De");		} else {			sprintf(outstring, "En");		}		printf("\nFile to %scrypt   : ", outstring);		if (argc > 1) {			strcpy(filesource, argv[1]);			printf("%s\n", filesource);		} else {	        scanf("%s", filesource);		}	}/*  Get passphrase  */	if (allpassed == 0) {		printf("Enter Passphrase  : ");		fflush(stdout);		while ((passchar = getch()) != EOF && passchar != '\n' && passchar != '\r' && passcount < sizeof(password) - 1) {			if (passchar == '\b' && passcount > 0) {				printf("\b \b");				fflush(stdout);				passcount--;				password[passcount] = '\0';			}			else if (isalnum(passchar)) {				putchar('*');				password[passcount++] = (char)passchar;			}	 	}	}/*  Hash passphrase */	loop = 0;	while (loop < 5) {		password[passcount + loop] = password[loop];		loop++;	}	loop = 0;	while (loop < passcount) {		hashchar[0] = password[loop];		hashchar[1] = password[loop + 1];		hashchar[2] = password[loop + 2];		hashchar[3] = password[loop + 3];		hashchar[4] = password[loop + 4];		hashchar[5] = password[loop + 5];		hashvalue = ((hashchar[0] ^ hashchar[3]) + hashchar[1]) * ((hashchar[5] ^ hashchar[2]) + hashchar[4]);		sprintf(hexstring[loop], "%X", hashvalue);		loop++;	}	passcount = loop;/*	Either encode the file name, or ask for it	*/	if (allpassed != 2) {		if (taskdo == 0) {			loop = 0;			while (loop < sizeof(filesource)) {				inchar = filesource[loop];				sscanf(hexstring[loop], "%X", &hashvalue);				outchar = (outchar + (inchar * hashvalue));				loop++;			}			sprintf(filedestin, "%#x", outchar);			printf("\n");		} else {			printf("\nOriginal Filename : ");			scanf("%s", filedestin);		}	}/*	Open files for the first time	*/	fileone = fopen(filesource, "rb");	filetemp = fopen("tempfile", "w");	filetwo = fopen(filedestin, "wb");	if (fileone == NULL) {		printf("\n\n\tERROR!\n\n\tSource File cannot be accessed ...");		fclose (filetwo);		fclose (filetemp);		remove ("tempfile");		inchar = getch();		exit(0);	}/*	Either encrypt source file to temp file, or decompress source file to temp file	*/	if (taskdo == 0) {       	loop = 0;		while ((inchar = getc(fileone)) != (unsigned)EOF) {			if (loop >= passcount) {				loop = 0;			}			sscanf(hexstring[loop], "%X", &hashvalue);			outchar = inchar + hashvalue;			fprintf(filetemp, "%4X", outchar);			loop++;		}	} else {		while ((inchar = getc(fileone)) != (unsigned)EOF) {			if (inchar < 16) {				sprintf(outstring, "0%X", inchar);			}			else {				sprintf(outstring, "%2X", inchar);			}			fprintf(filetemp, "%2s", outstring);		}	}/*	Close temp file and reopen to read from it files	*/	fclose (filetemp);	filetemp = fopen("tempfile", "r");/*	Now either compress the temp file or decrypt it	*/	if (taskdo == 0) {		while (!feof(filetemp)) {			fgets(instring, 3, filetemp);			sscanf(instring, "%X", &outchar);			fputc(outchar, filetwo);		}	} else {		while (!feof(filetemp)) {			if (loop >= passcount) {				loop = 0;			}			fgets(instring, 5, filetemp);			sscanf(hexstring[loop], "%X", &hashvalue);			sscanf(instring, "%X", &inchar);			outchar = inchar - hashvalue;			fputc(outchar, filetwo);			loop++;		}	}/*	End of program, remove tempfile	*/	fclose (fileone);	fclose (filetwo);	fclose (filetemp);	remove ("tempfile");	printf("\nDone\n\n");	inchar = getch();	exit(0);}

⌨️ 快捷键说明

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