⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tea.cpp

📁 300中加密方法
💻 CPP
字号:
// tea.cpp - modified by Wei Dai from code in the original paper

#include "pch.h"
#include "tea.h"

NAMESPACE_BEGIN(CryptoPP)

const word32 TEA::DELTA = 0x9e3779b9;

TEA::TEA(const byte *userKey)
	: k(4)
{
	for (int j=0;j<4;j++)
		k[j] = ((word32)userKey[4*j]<<24) | ((word32)userKey[4*j+1]<<16) 
			 | ((word32)userKey[4*j+2]<<8) | ((word32)userKey[4*j+3]);
}

void TEAEncryption::ProcessBlock(const byte *in, byte *out) const
{
#ifdef IS_LITTLE_ENDIAN
	word32 y = byteReverse(*(word32 *)in);
	word32 z = byteReverse(*(word32 *)(in+4));
#else
	word32 y = *(word32 *)in;
	word32 z = *(word32 *)(in+4);
#endif

	word32 sum = 0;
	for (int i=0; i<ROUNDS; i++)
	{   
		sum += DELTA;
		y += (z << 4) + k[0] ^ z + sum ^ (z >> 5) + k[1];
		z += (y << 4) + k[2] ^ y + sum ^ (y >> 5) + k[3];
	}

#ifdef IS_LITTLE_ENDIAN
	*(word32 *)out = byteReverse(y);
	*(word32 *)(out+4) = byteReverse(z);
#else
	*(word32 *)out = y;
	*(word32 *)(out+4) = z;
#endif
}

void TEADecryption::ProcessBlock(const byte *in, byte *out) const
{
#ifdef IS_LITTLE_ENDIAN
	word32 y = byteReverse(*(word32 *)in);
	word32 z = byteReverse(*(word32 *)(in+4));
#else
	word32 y = *(word32 *)in;
	word32 z = *(word32 *)(in+4);
#endif

	word32 sum = DELTA << LOG_ROUNDS;
	for (int i=0; i<ROUNDS; i++)
	{
		z -= (y << 4) + k[2] ^ y + sum ^ (y >> 5) + k[3]; 
		y -= (z << 4) + k[0] ^ z + sum ^ (z >> 5) + k[1];
		sum -= DELTA;
	}

#ifdef IS_LITTLE_ENDIAN
	*(word32 *)out = byteReverse(y);
	*(word32 *)(out+4) = byteReverse(z);
#else
	*(word32 *)out = y;
	*(word32 *)(out+4) = z;
#endif
}

NAMESPACE_END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -