#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *path="E:\\c\\q.txt";
char *newPathJia="E:\\c\\qjia.txt";
char *newPathJie="E:\\c\\qjie.txt";
//读取文件大小理论都是rb,如果用r,如果字符等于\n,需要额外++
//文本文件注意事项,所有字符单一加密方式,可以读写文本实现加密
//文本文件按照密码加密,不能用文本,读取 \r\n ->\r\n 写入\n ->\r\n
void runJiaJie(char *_path,char *_newPath)
{
FILE *fpR=fopen(_path,"r+");
FILE *fpW=fopen(_newPath,"w+");
if (fpR==NULL||fpW==NULL)
{
return -1;
}
else
{
int ch;
while((ch=fgetc(fpR))!=EOF)
{
fputc(ch^3,fpW);
}
}
printf ("ok!\n");
fclose(fpR);
fclose(fpW);
//return 0;
}
//获取文件大小
int fileSize(char *_path)
{
FILE*fp=fopen(_path,"rb");
if (fopen==NULL)
{
printf ("error: %s cat't open file !\a\n",_path);
return -1;
}
char *_fileName=path;
if (_fileName==NULL)
{
printf("error: _fileName copy nothing \a\n");
}
int length=0;
int ch=0;
while(ch=fgetc(fp)!=EOF)
{
length++;
}
printf ("file sizeof %d byte\n",length);
printf ("fileName = %s",_fileName);
fclose(fp);
return length;
}
//文件加密
void runJiaJiePasswad(char *_path,char *_newPath,char *_passwad)
{
FILE *fpR=fopen(_path,"rb");
FILE *fpW=fopen(_newPath,"wb");
if (fpR==NULL||fpW==NULL)
{
return -1;
}
else
{
int allLength=fileSize(path);//整体长度
int passwadLength=strlen(_passwad);//密码长度
printf ("allLength = %d length\n ",allLength);
printf ("passwadLength = %d length\n ",passwadLength);
for (int i=0;i<allLength/passwadLength;i++)
{
for (int k=0;k<passwadLength;k++)
{
int ch=fgetc(fpR);//读文件
fputc(ch^_passwad[k],fpW);//加密写入到fpW
}
}
for (int j=0;j<allLength%passwadLength;j++)
{
int ch=fgetc(fpR);
fputc(ch ^ _passwad[j],fpW);
}
fclose(fpR);
fclose(fpW);
}
}
int main (int argc,char *argv[])
{
//runJiaJie(path,newPathJia);
//runJiaJie(newPathJia,newPathJie);
//fileSize(path);
runJiaJiePasswad(path,newPathJia,"520");
runJiaJiePasswad(path,newPathJie,"520");
return 0;
}

c语言文件加密,加密出现问题,解密不了
- 写回答
- 好问题 1 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 软件攻城小狮 2017-06-22 00:28关注
程序没有问题,只是参数写错了
runJiaJiePasswad(path,newPathJia,"520");
runJiaJiePasswad(newPathJia,newPathJie,"520");
改成上面那样就行了本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报