www.pudn.com > cfgcrypt.rar > TCrypt.cpp


#include 
#include 

#include "TCrypt.h"
#include "Des.h"
//#include "Base64.h"
#include "Coder.h"


//const char key[]={0,2,0,0,9,3,5,1,9,8,0,0,9,1,7};
const char key[]={0,2,0,0,9,3,5};


bool TCrypt::Encrypt(char const * const in, char *out, long outlen)
{
	if(in == NULL || in[0] == '\0' || out == NULL || outlen <= 0) return false;

	int deslen =  ((strlen(in) + 7) / 8) * 8; //计算3Des需要的长度
	int baselen = (deslen + 2)/3*4; //计算Base64需要的长度

	if(outlen <= baselen) return false;

	char* ostr = new char[baselen + 1];

	if(false == Des_Go(ostr, (char*)in, strlen(in), 
		key, sizeof(key), ENCRYPT)) 
		return false;

	/*
		int len  = Base64Encod(ostr, strlen(ostr), out);
		out[len] = '\0';
	*/

	CCoder::base64_encode(ostr, deslen, out);

	delete [] ostr;

	return true;
}

bool TCrypt::Decrypt(char const * const in, char *out, long outlen)
{
	if(in == NULL || in[0] == '\0' || out == NULL || outlen <= 0) return false;
	if(outlen < (long) strlen(in)) return false;

	memset(out, '\0', outlen);

	/*
	int len = Base64Decod((char*)in, strlen(in), out);
	out[len] = '\0';
	*/

	int len = CCoder::base64_decode((char*)in , strlen(in), out);
	out[len] = '\0';

	if(false == Des_Go(out, out, len, key, sizeof(key), DECRYPT)) {
		return false;
	}
	return true;
}