c语言文件加密,加密出现问题,解密不了

图片说明
图片说明

 #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;
}

2个回答

程序没有问题,只是参数写错了
runJiaJiePasswad(path,newPathJia,"520");
runJiaJiePasswad(newPathJia,newPathJie,"520");
改成上面那样就行了

qq_37362700
qq_37362700 感谢,非常感谢
2 年多之前 回复
qq_37362700
qq_37362700 谢谢了
2 年多之前 回复

加密没有问题,但是没有解密成功,却,跟加密文件一样,自己没有找出错误,也没有找到原因,麻烦大家帮忙看下,谢谢。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问