【加密解密】 文件的加密解密 <含源代码>

//编译环境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");
}
 

原文件

加密之后


解密之后


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值