#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;
}
}
(c语言)文件加密程序
最新推荐文章于 2024-04-28 21:13:08 发布