C/C++实现的最基本的加解密函数代码

Crypt.h

#pragma once

class CCrypt
{
public:
	static BOOL Encrypt(BYTE* source,BYTE* destination,DWORD length);
	static BOOL Decrypt(BYTE* source,BYTE* destination,DWORD length);
};

Crypt.cpp
#include "Crypt.h"

const INT C1	= 52845;
const INT C2	= 22719;
const INT KEY	= 72957;

//进行加密的函数
BOOL CCrypt::Encrypt( BYTE* source,BYTE* destination,DWORD length )
{
	INT Key = KEY;

	if(!source || !destination || length <= 0)
		return FALSE;

	for(DWORD i = 0; i < length; i++)
	{
		//按照Key值进行1BYTE的平方,推8bit
		destination[i] = source[i]^Key>>8;
		//在下一BYTE中,通过C1、C2修改所要使用的KEY
		Key = (destination[i]+Key)*C1+C2;
	}

	return TRUE;
}

//进行解密的函数
BOOL CCrypt::Decrypt( BYTE* source,BYTE* destination,DWORD length )
{
	BYTE previousBlock;
	INT Key = KEY;

	if(!source || !destination || length <= 0)
		return FALSE;

	for(DWORD i = 0; i < length; i++)
	{
		//加密为BYTE单位
		previousBlock = source[i];
		//保存之前的块(Block),然后查看解密所使用的KEY
		destination[i] = source[i]^Key>>8;
		Key = (previousBlock+Key)*C1+C2;
	}

	return TRUE;
}


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页