//编译环境vs2013
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//字符串加密
char *stringEncrypt(char *password, char *string)
{
int passLength = strlen(password);<span style="white-space:pre"> </span>//获取加密长度
int stringLength = strlen(string);<span style="white-space:pre"> </span>//获取字符串长度
if (stringLength%passLength == 0)<span style="white-space:pre"> </span>//字符串长度是密码长度的整数倍
{
int times = stringLength / passLength;
for (int i = 0; i < times; i++)
{
for (int j = 0; j < passLength; j++)
{
string[i*passLength + j] ^= password[j];
//这里的下标计算画图很容易理解
}
}
}
else
{
int times = stringLength / passLength;
for (int i = 0; i < times; i++)
{
for (int j = 0; j < passLength; j++)
{
string[i*passLength + j] ^= password[j];
}
}
int lastLength = stringLength%passLength;
//不能整除的,也就是余数长度
for (int i = 0; i < lastLength; i++)
{
string[passLength*(stringLength / passLength) + i] ^= password[i];
//这里的解密要从前边已经整除完成的后一个位置开始,
//假如说stringLength = 10,passLength = 4;那么除不尽的加密就从下标8开始,
//(10 / 4)*4 = 8.
}
}
return string;
}
//字符串解密算法
char *stringDecode(char *password, char *string)
{
int passLength = strlen(password);<span style="white-space:pre"> </span>//获取加密长度
int stringLength = strlen(string);<span style="white-space:pre"> </span>//获取字符串长度
if (stringLength % passLength == 0)<span style="white-space:pre"> </span>//字符串长度是密码长度的整数倍
{
int times = stringLength / passLength;
for (int i = 0; i < times; i++)
{
for (int j = 0; j < passLength; j++)
{
string[i*passLength + j] ^= password[j];
}
}
}
else
{
int times = stringLength / passLength;
for (int i = 0; i < times; i++)
{
for (int j = 0; j < passLength; j++)
{
string[i*passLength + j] ^= password[j];
}
}
int lastLength = stringLength%passLength;
//不能整除的,也就是余数长度
for (int i = 0; i < lastLength; i++)
{
string[passLength*(stringLength / passLength) + i] ^= password[i];
//这里的解密要从前边已经整除完成的后一个位置开始,
//假如说stringLength = 10,passLength = 4;那么除不尽的加密就从下标8开始,
//(10 / 4)*4 = 8.
}
}
return string;
}
//文件内容按字符串加密解密
void fileEncryptString(char *path, char *pathEn, char *password)
{
FILE *pfr, *pfw; //读写
pfr = fopen(path, "r");
pfw = fopen(pathEn, "w");
if (pfr == NULL || pfw == NULL)
{
fclose(pfr);
fclose(pfw);
return;
}
else
{
int fileLength = getFileSize(path);//获取文件长度
char *newStr = (char *)malloc(sizeof(char)*(fileLength + 1));
//newstr保存从文件中读出来的字符
</pre><pre code_snippet_id="1666783" snippet_file_name="blog_20160429_3_6104291" name="code" class="cpp"> for (int i = 0; i < fileLength; i++)
{
char ch = fgetc(pfr); //获取一个字符
newStr[i] = ch;
}
newStr[fileLength] = '\0'; //字符串结束
stringEncrypt(password, newStr); //加密
for (int i = 0; i < fileLength; i++)
{
fputc(newStr[i], pfw); //加密之后的字符写入文件
}
fclose(pfr);
fclose(pfw);
}
}
void fileDecodetString(char *path, char *pathDe, char *password)
{
FILE *pfr, *pfw;
pfr = fopen(path, "r");
pfw = fopen(pathDe, "w");
if (pfr == NULL || pfw == NULL)
{
fclose(pfr);
fclose(pfw);
return;
}
else
{
while (!feof(pfr))
{
char string[256] = { 0 };
fgets(string, 256, pfr);
stringDecode(password,string);
//调用加密算法,pfr指向文件中读出来的字符串,使用passwoed加密
fputs(string, pfw);
//将加密之后的字符处写入pfw指向的文件中
//这里与之前的fileEncrypt()函数的加密的不同之处在于,这里用到自己设定的密码
//而fileEncrypt()只是对字符进行了+1运算
}
fclose(pfr);
fclose(pfw);
}
}
void main()
{
char *path = "F:\\My Program\\1.txt";
char *pathEn = "F:\\My Program\\1.1_en.txt";
char *pathde = "F:\\My Program\\1.1_de.txt";
char *password = "ABCDE";
printf("%d字节\n", getFileSize(path));
fileEncryptString(path, pathEn, password);
fileDecodetString(pathEn, pathde, password);
system("pause");
}
原文件
加密之后
解密之后