异或后 还原不回去 大家帮忙看看啊

gou1366324 2014-03-09 07:58:10

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>

void descrypt_file()
{
int ch;
int key = 112;
int i = 0;

FILE * fp_dstTxt,*fp_srcTxt;

fp_dstTxt = fopen("dst.txt", "rb");

if(fp_dstTxt == NULL)
{
return;
}

fp_srcTxt = fopen("resource.txt","wb");

if(fp_srcTxt == NULL)
{
fclose(fp_dstTxt);
return;
}

int x = 0;

while( (ch=fgetc(fp_dstTxt)) != EOF )
{
x = (ch - i) ^ key;
fputc(x, fp_srcTxt);
key ^= ch;
i++;
}

fclose(fp_srcTxt);
fclose(fp_dstTxt);
}

void encrypt_file()
{
int ch,i = 0;
int key = 112;

FILE * fp_srcTxt,*fp_dstTxt;
fp_srcTxt = fopen("src.txt", "rb");

if(fp_srcTxt == NULL)
{
return;
}

fp_dstTxt = fopen("dst.txt","wb");

if(fp_dstTxt == NULL)
{
fclose(fp_srcTxt);
return;
}

int x = 0;
while( (ch=fgetc(fp_srcTxt)) != EOF )
{
x = (ch ^ key) + i;
fputc(x, fp_dstTxt);
key ^= ch;
i++;
}

fclose(fp_srcTxt);
fclose(fp_dstTxt);
}

int main(int argc, const char * argv[])
{

encrypt_file();
descrypt_file();
return 0;
}
...全文
369 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mangoalx 2014-03-11
  • 打赏
  • 举报
回复
引用 6 楼 Automation_dmu 的回复:
将descrypt_file()中的key ^= ch 改成 key ^= x
这个好像正解
AndyStevens 2014-03-10
  • 打赏
  • 举报
回复
将descrypt_file()中的key ^= ch 改成 key ^= x
gou1366324 2014-03-10
  • 打赏
  • 举报
回复
key ^= ch; 每次改变key的值啊
PDD123 2014-03-10
  • 打赏
  • 举报
回复
key ^= ch; 是不是把key的值改变了?
mangoalx 2014-03-09
  • 打赏
  • 举报
回复
完全不对?还是有些地方不对? 跟踪一下应该很容易查的。
bobo_包子 2014-03-09
  • 打赏
  • 举报
回复
'a' - 256 = -1不等于-1,反正就是这意思
bobo_包子 2014-03-09
  • 打赏
  • 举报
回复
   while( (ch=fgetc(fp_dstTxt)) != EOF )
    {
        x = (ch - i) ^ key;
        fputc(x, fp_srcTxt);
        key ^= ch;
        i++;
    }

  while( (ch=fgetc(fp_srcTxt)) != EOF )
    {
        x = (ch ^ key) + i;
        fputc(x, fp_dstTxt);
        key ^= ch;
        i++;
    }
i如果大于255怎么办? 例如: 'a' - 256 = -1,你写的文件里是什么? 可以改成: i = i %255

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧