dARK

//
// FileName			: HYDarkText.h
// Author			: Cay22
// CreateTime		: 2015-02-28 08:08:19
// Purpose			: 1. 对内存数据进行十六进制字符串可视处理.
//					: 2. 对可视十六进制字符转换成内存数据.
// Note				: 
// Log				: 1. 2015-02-10 08:08:19
//					:	创建
//
#pragma once
#include <string>

class CHYHEXText
{
public:
	CHYHEXText();
	~CHYHEXText();
	// 把内存数据转换成十六进制字符串返回.
	std::string MakeHEXText(unsigned char* pInput, unsigned int nInputLen);
	// 把十六进制字符串转换成内存数据, 并返回, 返回的指针用户不需要删除.
	unsigned char* ReMakeHEXText(const std::string& strHEXText , unsigned int& nOutputLen);
private:
	unsigned char HEXTextToMemery(char chData);
	unsigned char* m_pOutput;
	unsigned int m_nOutputLen;
	void Release();
};


//
// FileName			: HYDarkText.h
// Author			: Cay22
// CreateTime		: 2015-02-28 08:08:19
// Purpose			: 1. 对内存数据进行十六进制字符串可视处理.
//					: 2. 对可视十六进制字符转换成内存数据.
// Note				: 
// Log				: 1. 2015-02-10 08:08:19
//					:	创建
//

#include "stdafx.h"
#include "HYHEXText.h"

CHYHEXText::~CHYHEXText()
{
	Release();
}

CHYHEXText::CHYHEXText()
	: m_pOutput(0)
	, m_nOutputLen(0)
{}

void CHYHEXText::Release()
{
	delete [] m_pOutput;
	m_pOutput = 0;
	m_nOutputLen = 0;
}


std::string CHYHEXText::MakeHEXText(unsigned char* pInput, unsigned int nInputLen)
{
	std::string strHEXText;

	char chTemp[3];
	chTemp[2] = 0x00;

	unsigned char chData;

	unsigned int nIndex = 0;
	while(nIndex < nInputLen)
	{
		chData = ((pInput[nIndex] >> 4) & 0x0F);
		chTemp[0] = (0x09 < chData) ? (chData + 'A' - 0x0A) : (chData + '0');

		chData = ((pInput[nIndex]) & 0x0F);
		chTemp[1] = (0x09 < chData) ? (chData + 'A' - 0x0A) : (chData + '0');

		strHEXText += chTemp;
		++nIndex;
	}
	return strHEXText;
}

unsigned char* CHYHEXText::ReMakeHEXText(const std::string& strHEXText , unsigned int& nOutputLen)
{
	Release();
	
	int nHEXTextLen = strHEXText.size();
	if(0 != (nHEXTextLen % 2))
	{
		return 0;
	}

	nOutputLen = nHEXTextLen / 2;
	m_nOutputLen = nOutputLen;
	m_pOutput = new unsigned char[nOutputLen + 1];
	memset(m_pOutput, 0x00, nOutputLen + 1);

	int i = 0;
	int nIndex = 0;
	for(i = 0; i < nHEXTextLen; i += 2, ++nIndex)
	{
		m_pOutput[nIndex] = ((HEXTextToMemery(strHEXText[i]) << 4) & 0xF0);
		m_pOutput[nIndex] += HEXTextToMemery(strHEXText[i + 1]);
	}

	return m_pOutput;
}


unsigned char CHYHEXText::HEXTextToMemery(char chData)
{
	if('A' <= chData && 'F' >= chData)
	{
		return chData - 'A' + 0x0A;
	}
	else if('0' <= chData && '9' >= chData)
	{
		return chData - '0';
	}
	return 0;
}






//
// FileName			: HYDarkText.h
// Author			: Cay22
// CreateTime		: 2015-02-28 08:08:19
// Purpose			: 1. 对数据进行AES加密, 然后对密文数据生成可视的十六进制文本
//					: 2. 对可视的十六进制文本转换成内存数据, 进行AES解密.
// Note				: 
// Log				: 1. 2015-02-10 08:08:19
//					:	创建
//
#pragma once
#include <string>

class CHYDarkText
{
public:
	CHYDarkText();
	~CHYDarkText();
	bool Init(unsigned char* pKeyBytes, int nKeySize);
	std::string MakeDarkText(unsigned char* pInput, unsigned int nInputLen);
	unsigned char* ReMakeDarkText(const std::string& strDarkText , unsigned int& nOutputLen);
private:
	unsigned char* m_pOutput;
	unsigned int m_nOutputLen;
	unsigned char m_uchKey[32];
	void Release();
};





//
// FileName			: HYDarkText.h
// Author			: Cay22
// CreateTime		: 2015-02-28 08:08:19
// Purpose			: 1. 对数据进行AES加密, 然后对密文数据生成可视的十六进制文本
//					: 2. 对可视的十六进制文本转换成内存数据, 进行AES解密.
// Note				: 
// Log				: 1. 2015-02-10 08:08:19
//					:	创建
//

#include "stdafx.h"
#include "HYDarkText.h"
#include "HYAES.h"
#include "HYHEXText.h"

CHYDarkText::~CHYDarkText()
{
	Release();
}

CHYDarkText::CHYDarkText()
	: m_pOutput(0)
	, m_nOutputLen(0)
{}

void CHYDarkText::Release()
{
	delete [] m_pOutput;
	m_pOutput = 0;
	m_nOutputLen = 0;
}


bool CHYDarkText::Init(unsigned char* pKeyBytes, int nKeySize)
{
	if(32 < nKeySize || 0 > nKeySize)
	{
		return false;
	}
	memset(m_uchKey, 0x66, 32);
	memcpy(m_uchKey, pKeyBytes, nKeySize);
	return true;
}

std::string CHYDarkText::MakeDarkText(unsigned char* pInput, unsigned int nInputLen)
{
	// AES加密
	CHYAES hyAES;
	unsigned int nCipherLen = 0;
	hyAES.Init(m_uchKey, 32);
	unsigned char* pCipher = hyAES.Cipher(pInput, nInputLen, nCipherLen);

	// 转换成可视十六进制
	CHYHEXText hyHEXText;
	return hyHEXText.MakeHEXText(pCipher, nCipherLen);
}

unsigned char* CHYDarkText::ReMakeDarkText(const std::string& strDarkText , unsigned int& nOutputLen)
{
	Release();

	// 转换成可视十六进制
	CHYHEXText hyHEXText;
	unsigned int nDataLen = 0;
	unsigned char* pData = hyHEXText.ReMakeHEXText(strDarkText, nDataLen);

	// AES解密
	CHYAES hyAES;
	unsigned int nCipherLen = 0;
	hyAES.Init(m_uchKey, 32);
	unsigned char* pInvCipher = hyAES.InvCipher(pData, nDataLen, nOutputLen);

	if(0 >= nOutputLen)
	{
		return 0;
	}

	m_pOutput = new unsigned char[nOutputLen + 1];
	memset(m_pOutput, 0x00, nOutputLen + 1);
	m_nOutputLen = nOutputLen;
	memcpy(m_pOutput, pInvCipher, nOutputLen);
	return m_pOutput;
}




#include "stdafx.h"
#include "HYDarkText.h"

int _tmain(int argc, _TCHAR* argv[])
{
	char chPassword[32];
	memset(chPassword, 0x00, 32);
	int nLenPassword = sprintf_s(chPassword, "abcdefg");

	char chTemp[256];
	memset(chTemp, 0x00, 256);
	unsigned int nLen = sprintf_s(chTemp, "0987654321A");

	CHYDarkText hyDarkText;	
	hyDarkText.Init((unsigned char *)chPassword, nLenPassword);
	std::string strDarkText = hyDarkText.MakeDarkText((unsigned char *)chTemp, nLen);
	
	unsigned int nOutputLen = 0;
	unsigned char* pOutput = hyDarkText.ReMakeDarkText(strDarkText, nOutputLen);
	return 0;
}




                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值