伪随机数加密问题 写好了加密 可是解密我不会了
用的是异或原理 认为把加密后的文件再次以同样的方式异或
能得到没有加密的文件 可是自己想的错了
#include "stdio.h"#include "stdlib.h"#include "stdio.h"int file_length(char path[]){ FILE *fp; int length; if((fp=fopen(path,"rb"))!=NULL) { fseek(fp,0L,SEEK_END); length=ftell(fp); fclose(fp); } return length;}int main(){ FILE *fp; int i,len,key; char ch,buf[60000],path[50]; printf("input the name of file:\n"); scanf("%s",path); if((fp=fopen(path,"rb"))==NULL) { printf("file cannot opened"); exit(1); } else { printf("file opened for encrypt\n"); } len=file_length(path); printf("please input key:\n"); scanf("%d",&key); srand(key); for(i=0;i<len;i++) { buf[i]=rand(); ch=fgetc(fp); if(ch<=33) { ch+=33; } buf[i]=ch^buf[i]; } fp=fopen("encrypt.txt","wb"); fputs(buf,fp); fclose(fp); return 0; }//最后几行这样改//对于解密,把加密文件输入再加密一次就解密了 for(i=0;i<len;i++) { buf[i]=rand(); ch=fgetc(fp); buf[i]=ch^buf[i]; } fclose(fp); fp=fopen("encrypt.txt","wb"); fwrite(buf, len, 1, fp); fclose(fp);
[解决办法]
我认为你这样会溢出的buf[60000]
建议
while(!feof(fp2))
{
fputc(fgetc(fp2),fp3);
}
采用这样的行吗?
[解决办法]
把if(ch<=33) { ch+=33; }去掉就ok了
[解决办法]
解密就按加密算法再来一遍吧,原理类似于求一个数的原码等于这个数的补码再求次补码
[解决办法]