示例代码中利用“异或操作”实现对文件加密。首先,建立两个*.txt文件,分别将其命名为“原文件.txt”,“key.text”,“原文件.txt”为需要加密的文件,“key.text”为密钥文件,运行代码后“原文件.txt”被加密,再一次运行代码“原文件.txt”会被解密。
示例代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fp_src1,*fp_src2;//两个文件指针, FILE大写表示系统已经定义FILE这个变量
//char buffer[],buffer1[120];
int i,j,l,k=0;
long int size;
//打开文件“原文件.txt”,需要加(解)密的文件,r表示读,b表示以二进制方式读,
if((fp_src1=fopen("原文件.txt","rb+"))==NULL)
{
perror("open1");
exit(0);
}
//打开文件“key.txt”,密钥文件,r表示读,b表示已二进制方式读,
if((fp_src2=fopen("key.txt","rb+"))==NULL)
{
perror("open2");
exit(0);
}
fseek(fp_src1,0,SEEK_SET);
fseek(fp_src1,0,SEEK_END);// 为了知道文件的大小
size=ftell(fp_src1);//ftell()的返回值为文件的大小
fseek(fp_src1,0,SEEK_SET);
char buffer1[size],buffer2[size];
d


381

被折叠的 条评论
为什么被折叠?



