【加密解密】对exe文件的加密解密 <含源代码》

//编译环境VS2013

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


int getFileSize(char *path)
{
	FILE *pf = fopen(path, "r");
	if (pf == NULL)
	{
		return-1;
	}
	else
	{
		fseek(pf, 0, SEEK_END); //文件指针移动到文件末尾
		int length = ftell(pf);

		return length;
	}
}

//字符串加密  
char *stringEncrypt(char *password, char *string)
{
	int passLength = strlen(password); //获取加密长度  
		int stringLength = strlen(string); //获取字符串长度  

		if (stringLength%passLength == 0)//字符串长度是密码长度的整数倍  
		{
			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); //获取加密长度  
		int stringLength = strlen(string); //获取字符串长度  

		if (stringLength % passLength == 0)//字符串长度是密码长度的整数倍  
		{
			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 Encrypt(char *oldPath, char *newPath, char *password)
{
	FILE *pfr, *pfw;

	pfr = fopen(oldPath, "rb");//以二进制读取
	pfw = fopen(newPath, "wb");
	if (pfr == NULL || pfw == NULL)
	{
		fclose(pfr);
		fclose(pfw);
		return;
	}
	else
	{
		int length = getFileSize(oldPath);//获取原文件大小
		char *p = malloc(length*sizeof(char));//为新文件申请空间

		fread(p, sizeof(char), length, pfr);//读取二进制到文件
		stringEncrypt(password, p);
		fwrite(p, sizeof(char), length, pfw);//写入二进制到文件

		//文件操作完成之后关闭文件
		fclose(pfr);
		fclose(pfw);
	}
}
//解密
void decode(char *oldPath, char *newPath, char *password)
{
	FILE *pfr, *pfw;

	pfr = fopen(oldPath, "rb");//以二进制读取
	pfw = fopen(newPath, "wb");
	if (pfr == NULL || pfw == NULL)
	{
		fclose(pfr);
		fclose(pfw);
		return;
	}
	else
	{
		int length = getFileSize(oldPath);//获取原文件大小
		char *p = malloc(length*sizeof(char));//为新文件申请空间
		fread(p, sizeof(char), length, pfr);//读取二进制到文件
		stringDecode(password, p);
		fwrite(p, sizeof(char), length, pfw);//写入二进制到文件

		//文件操作完成之后关闭文件
		fclose(pfr);
		fclose(pfw);
	}
}

void main()
{

	char *oldPath = "F:\\My Program\\20160313心型动态图.exe";
	char *newPath = "F:\\My Program\\20160313心型动态图a_password.exe";
	char *newDePath = "F:\\My Program\\20160313心型动态图b_password.exe";
	int length = getFileSize(oldPath);
	char *password = "ABCDE";

	printf("%d\n", length);
	
	Encrypt(oldPath, newPath, password);
	decode(newPath, newDePath, password);
	system("pause");
}

原exe文件执行效果


加密后不能打开


解密后,正常打开


  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值