利用异或实现随机加密程序的方法

通常程序师们在写代码的时候都会将一个字段进行加密,加密出来之后不仅要不相同还需要可以解密。那么要实现加密文件随机存储应该要怎么办呢?下面中国E盟小编教你利用异或实现随机加密程序的方法来解决这个问题吧。

利用异或的性质来对文件进行加密:


c=a^b

c^b=a

#include "stdio.h"
#include "stdlib.h"

void main(int argc,char *argv[])
{
FILE *fp1,*fp2;
char c,ch;
long j;
if(3!=argc)
{
printf("Command error/n");
exit(1);
}

if((fp1=fopen(argv[1],"rb"))==NULL)
{
printf("Can not open the source file/n");
exit(1);
}

if(NULL==(fp2=fopen(argv[2],"wb")))
{
printf("Can not open the aim file/n");
exit(1);
}

printf("Please input the password:/n");
scanf("%i",&j);
srand(j);
ch=fgetc(fp1);
while(!feof(fp1))
{
c=rand();
ch=ch^c;
fputc(ch,fp2);
ch=fgetc(fp1);
}

fclose(fp1);
fclose(fp2);
}

以上就是利用异或实现随机加密程序的方法,希望对大家的学习或工作能带来一定的帮助~如果有疑问大家可以留言交流,谢谢大家对中国E盟技术频道的支持!