#define _CRT_SECURE_NO_WARNINGS
#define SRC_PATH "C:\\Users\\michael\\Desktop\\yzh.txt"
#define CODE_PATH "C:\\Users\\michael\\Desktop\\yzh_code.txt"
#define DECODE_PATH "C:\\Users\\michael\\Desktop\\yzh_decode.txt"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getFileSize_C(char * file)
{
int size = -1;
FILE * path;
path = fopen(file, "r");
if (NULL == path)
{
printf("文件打开失败!\n");
return size; //文件大小不可能为负数
}
else
{
//设置流文件指针的位置,以SEEK_END为起点,偏移量是0,亦即SEEK_END
fseek(path, 0, SEEK_END);
//函数结果:当前文件流指针位置相对于文件起始位置的字节偏移量
size = ftell(path);
fclose(path);
path = NULL;
}
return size;
}
void code_decode_file_with_psw(char* path, char* newpath,char* psw)
{
FILE* pRead = fopen(path, "r");
FILE* pWrite = fopen(newpath, "w");
if (pRead == NULL || pWrite == NULL)
{
return;
}
else
{
int ch = 0;
int fileSize = getFileSize_C(path);
int pswLength = strlen(psw);
for (int i = 0; i < fileSize / pswLength; i++)
{
for (int j = 0; j < pswLength; j++)
{
ch = fgetc(pRead);
fputc(ch^psw[j], pWrite);
}
}
for (int k = 0; k <fileSize % pswLength; k++)
{
ch = fgetc(pRead);
fputc(ch^psw[k], pWrite);
}
}
fclose(pRead);
fclose(pWrite);
pRead = NULL;
pWrite = NULL;
}
void main()
{
code_decode_file_with_psw(SRC_PATH, CODE_PATH,"yzh");
code_decode_file_with_psw(CODE_PATH, DECODE_PATH,"yzh");
system("pause");
}
文件加解密(二)——使用密码
最新推荐文章于 2025-12-17 17:59:57 发布
本文介绍了一种基于文本文件的简单加密与解密方法,使用C语言实现。该方法通过将文件内容与预设密码进行异或运算来完成加密过程,并能够逆向操作实现解密。文章提供了完整的源代码及注意事项。
57万+

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



