以前写得一个简单文件异或加密解密,你试着看下,对你是否有帮助。
#include <stdio.h>
#include <stdlib.h>
void encode(char* path_source, char* path_aim, unsigned char pwd);
void decode(char* path_source, char* path_aim, unsigned char pwd);
int main(void)
{
unsigned char pwd = 230;
char path_source[100] = "D:\\DEV C++\\ok.txt";
char path_aim[100] = "D:\\DEV C++\\ok_encode.txt";
encode(path_source, path_aim, pwd);
/* char path_source[100] = "D:\\DEV C++\\ok_encode.txt";
char path_aim[100] = "D:\\DEV C++\\ok_decode.txt";
decode(path_source, path_aim, pwd); */
return 0;
}
void encode(char* path_source, char* path_aim, unsigned char pwd)
{
FILE* pf_source;//源文件指针
FILE* pf_aim;//目标文件
pf_source = fopen(path_source, "rb");//二进制,若是r,可能会有误差
//源文件文件是可打开,并读取文件
pf_aim = fopen(path_aim, "wb");//目标文件可打开,并写入文件
if(NULL == pf_source || NULL == pf_aim)
{
printf("加密失败\n");
return;
}
char ch = fgetc(pf_source);
while(ch != EOF)
{
//加密处理
ch = ch ^ pwd;
fputc(ch, pf_aim);
ch = fgetc(pf_source);
}
fclose(pf_source);
fclose(pf_aim);
printf("加密成功\n");
}
void decode(char* path_source, char* path_aim, unsigned char pwd)
{
FILE* pf_source;//源文件指针
FILE* pf_aim;//目标文件
pf_source = fopen(path_source, "rb");//二进制,若是r,可能会有误差
pf_aim = fopen(path_aim, "wb");
if(NULL == pf_source || NULL == pf_aim)
{
printf("解密失败\n");
return;
}
char ch = fgetc(pf_source);
while(ch != EOF)
{
//解密处理
ch = ch ^ pwd;
fputc(ch, pf_aim);
ch = fgetc(pf_source);
}
fclose(pf_source);
fclose(pf_aim);
printf("解密成功\n");
}
[此贴子已经被作者于2019-6-23 21:41编辑过]