(c语言)文件加密程序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{ 
	FILE*fpread,*fpwrite;
	char file1[50],file2[50],filename[20],filepath[30],sign[11];
	printf("请输入你的文件路径:\n"); 
	scanf("%s",filepath);
	strcpy(file1,filepath);
	printf("\n请输入你所用的文件名:\n");
	scanf("%s",filename);
	strcat(file1,filename);
	if((fpread=fopen(file1,"rb")) == NULL){
		printf("can't open this file");
		exit(0);
	} 
	fread(sign,10,1,fpread);
	if(strncmp(sign,"qwertyuiop",10) != 0){    //加密过程 
		char ch;
		strcpy(file2,filepath);
		strcat(file2,"incode");
		strcat(file2,filename);
		if((fpwrite=fopen(file2,"wb")) == NULL){
			printf("can't open this file");
			exit(0);
		}
		fwrite("qwertyuiopqwertyuiop",20,1,fpwrite);
		rewind(fpread);
		while(!feof(fpread)){
			ch = fgetc(fpread);
			ch += 1;
			fputc(ch,fpwrite);
		}
		fclose(fpread);
		fclose(fpwrite);
		printf("加密成功!");
	}
	else{       //解密过程 
		char ch,code[11],incode[11];
		strcpy(file2,filepath);
		strcat(file2,"outcode");
		strcat(file2,filename);
		if((fpwrite=fopen(file2,"wb")) == NULL){
			printf("can't open this file");
			exit(0);
		}
		fread(code,10,1,fpread);
		while(1){
			printf("请输入解密密码:\n");
			scanf("%s",incode);
			if(strncmp(code,incode,10) == 0) break;
			printf("密码错误,请重新输入密码!"); 
			system("cls");
		}
		while(!feof(fpread)){
			ch = fgetc(fpread);
			ch -= 1;
			fputc(ch,fpwrite);
		}
		fclose(fpread);
		fclose(fpwrite);
		printf("解密成功!");
		return 0; 
	}
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值