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

📄 encoderopensslimpl.cpp

📁 这是一个基于VS2003开发的数据加密和解密的程序
💻 CPP
字号:
/*!
	\file EncoderOpenSslImpl.cpp
	\author Jackson
	\date 13/1/2005
*/

#include "EncoderOpenSslImpl.h"

namespace Lucid {

namespace Encryption {

TEncoderOpenSslImpl::TEncoderOpenSslImpl(
	const unsigned char* key,
	unsigned int key_length,
	const unsigned char* iv,
	unsigned int iv_length,
	TEncodeAlgorithm algorithm)
	:
	TEncoderImplBase(key, key_length, iv, iv_length, algorithm)
{
}

TEncoderOpenSslImpl::~TEncoderOpenSslImpl()
{
}

void TEncoderOpenSslImpl::Encode(
	const unsigned char *input,
	unsigned char *output,
	const int input_len,
	int& output_len)
{
	int tlen;

//	Lucid::System::Thread::TThreadGuard m(mMutex);

	EVP_CIPHER_CTX_init(&mEncodeCtx);
	EVP_EncryptInit(&mEncodeCtx, GetAlgorithmType(),
		const_cast<unsigned char*>(GetKey()), const_cast<unsigned char*>(GetIV()));

	//int blockSize = EVP_CIPHER_CTX_block_size(&mEncodeCtx);
	//int len = input_len + blockSize - 1;
	EVP_EncryptUpdate(&mEncodeCtx, output, &output_len,
		const_cast<unsigned char*>(input), input_len);
	//output_len = len;

	//tlen = blockSize;
	int ret = EVP_EncryptFinal(&mEncodeCtx, output + output_len, &tlen);
	output_len += tlen;

	EVP_CIPHER_CTX_cleanup(&mEncodeCtx);
}

void TEncoderOpenSslImpl::Decode(
	const unsigned char *input,
	unsigned char *output,
	const int input_len,
	int& output_len)
{
	int tlen;

//	Lucid::System::Thread::TThreadGuard m(mMutex);

	EVP_CIPHER_CTX_init(&mDecodeCtx);
	EVP_DecryptInit(&mDecodeCtx, GetAlgorithmType(),
		const_cast<unsigned char*>(GetKey()), const_cast<unsigned char*>(GetIV()));

	//int blockSize = EVP_CIPHER_CTX_block_size(&mDecodeCtx);
	//int len = input_len + blockSize;
	EVP_DecryptUpdate(&mDecodeCtx, output, &output_len,
		const_cast<unsigned char*>(input), input_len);
	//output_len = len;

	//tlen = blockSize;
	int ret = EVP_DecryptFinal(&mDecodeCtx, output + output_len, &tlen);
	output_len += tlen;
		
	EVP_CIPHER_CTX_cleanup(&mDecodeCtx);
}

const EVP_CIPHER* TEncoderOpenSslImpl::GetAlgorithmType() const
{
	const EVP_CIPHER *type;
	switch (GetAlgorithm()) {
	case NO_ENCODE:
		type = EVP_enc_null();
		break;
	case DES_CBC:
		type = EVP_des_cbc();
		break;
	case DES_ECB:
		type = EVP_des_ecb();
		break;
	case DES_CFB:
		type = EVP_des_cfb();
		break;
	case DES_OFB:
		type = EVP_des_ofb();
		break;
	case DES_EDE_CBC:
		type = EVP_des_ede_cbc();
		break;
	case DES_EDE:
		type = EVP_des_ede();
		break;
	case DES_EDE_OFB:
		type = EVP_des_ede_ofb();
		break;
	case DES_EDE_CFB:
		type = EVP_des_ede_cfb();
		break;
	case DES_EDE3_CBC:
		type = EVP_des_ede3_cbc();
		break;
	case DES_EDE3:
		type = EVP_des_ede3();
		break;
	case DES_EDE3_OFB:
		type = EVP_des_ede3_ofb();
		break;
	case DES_EDE3_CFB:
		type = EVP_des_ede3_cfb();
		break;
	case DESX_CBC:
		type = EVP_desx_cbc();
		break;
	case RC4:
		type = EVP_rc4();
		break;
	case RC4_40:
		type = EVP_rc4_40();
		break;
	case IDEA_CBC:
		type = EVP_idea_cbc();
		break;
	case IDEA_ECB:
		type = EVP_idea_ecb();
		break;
	case IDEA_CFB:
		type = EVP_idea_cfb();
		break;
	case IDEA_OFB:
		type = EVP_idea_ofb();
		break;
	case RC2_CBC:
		type = EVP_rc2_cbc();
		break;
	case RC2_ECB:
		type = EVP_rc2_ecb();
		break;
	case RC2_CFB:
		type = EVP_rc2_cfb();
		break;
	case RC2_OFB:
		type = EVP_rc2_ofb();
		break;
	case BF_CBC:
		type = EVP_bf_cbc();
		break;
	case BF_ECB:
		type = EVP_bf_ecb();
		break;
	case BF_CFB:
		type = EVP_bf_cfb();
		break;
	case BF_OFB:
		type = EVP_bf_ofb();
		break;
	case CAST5_CBC:
		type = EVP_cast5_cbc();
		break;
	case CAST5_ECB:
		type = EVP_cast5_ecb();
		break;
	case CAST5_CFB:
		type = EVP_cast5_cfb();
		break;
	case CAST5_OFB:
		type = EVP_cast5_ofb();
		break;
	case RC5_CBC:
		type = EVP_rc5_32_12_16_cbc();
		break;
	case RC5_ECB:
		type = EVP_rc5_32_12_16_ecb();
		break;
	case RC5_CFB:
		type = EVP_rc5_32_12_16_cfb();
		break;
	case RC5_OFB:
		type = EVP_rc5_32_12_16_ofb();
		break;
	case AES_128_ECB:
		type = EVP_aes_128_ecb();
		break;
	case AES_128_CBC:
		type = EVP_aes_128_cbc();
		break;
	case AES_128_CFB:
		type = EVP_aes_128_cfb();
		break;
	case AES_128_OFB:
		type = EVP_aes_128_ofb();
		break;
	case AES_192_ECB:
		type = EVP_aes_192_ecb();
		break;
	case AES_192_CBC:
		type = EVP_aes_192_cbc();
		break;
	case AES_192_CFB:
		type = EVP_aes_192_cfb();
		break;
	case AES_192_OFB:
		type = EVP_aes_192_ofb();
		break;
	/*case AES_256_ECB:
		type = EVP_aes_256_ecb();
		break;
	case AES_256_CBC:
		type = EVP_aes_256_cbc();
	case AES_256_CFB:
		type = EVP_aes_256_cfb();
		break;
	case AES_256_OFB:
		type = EVP_aes_256_ofb();
		break;*/
	}
	return type;
}

}

}

⌨️ 快捷键说明

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