code.h
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//加密
void codeFile(char * sourceFile, char * destFile);
//解密
void deCodeFile(char * sourceFile, char * destFile);
code.c
#include"code.h"
void codeFile(char * sourceFile, char * destFile)
{
FILE *fp1 = fopen(sourceFile, "r");
FILE *fp2 = fopen(destFile, "w");
if (!fp1 || !fp2)
{
return;
}
char ch;
while ((ch=fgetc(fp1))!=EOF)
{
// # 35
// 0000 0000 0010 0011 <<4
short temp = (short)ch;
temp = temp << 4;
//1000 0000 0000 0000 |
//1000 0010 0011 0000 + 0000~1111 0~15
temp = temp | 0x8000;
temp = temp + rand() % 16;
//printf("%d", temp);
//加密后数据 写入到文件中
fprintf(fp2, "%hd", temp);
}
fclose(fp1);
fclose(fp2);
}
void deCodeFile(char * sourceFile, char * destFile)
{
FILE *fp1 = fopen(sourceFile, "r");
FILE *fp2 = fopen(destFile, "w");
if (!fp1 || !fp2)
{
return;
}
short temp;
while (!feof(fp1))
{
fscanf(fp1, "%hd", &temp);
//1000 0010 0011 1010 << 1
// 000 0010 0011 10100 >> 5
//0000 0000 0010 0011
temp <<= 1;
temp >>= 5;
char ch = (char)temp;
fputc(ch, fp2);
}
fclose(fp1);
fclose(fp2);
}
main.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include"code.h"
void test02()
{
codeFile("./config.txt", "./加密文件.txt");
deCodeFile("./加密文件.txt", "./解密文件.txt");
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
test02();
system("pause");
return EXIT_SUCCESS;
}
ps: %hd:打印以十进制显示short类型的整数