www.gusucode.com > 一些VC++加密算法+实例源码源码程序 > 一些VC++加密算法+实例源码/优化后的加密注册模块/优化后的加密注册模块/Inv(optimized)/PublicKey.cpp

    // PublicKey.cpp : Get the public key for communication.
// Download by http://www.codesc.net
#include "stdafx.h"
#include "am_lv1.h"
#include "PublicKey.h"
#include "Encry.h"

//************************************************************************
char *getErrorMessage(HRESULT hr)
{
 LPVOID lpMsgBuf;
 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM ,
    NULL,
 hr,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    (LPTSTR) &lpMsgBuf,    
 0,
    NULL );
 
 return LPTSTR(lpMsgBuf);
}

//************************************************************************
int GetPublicKey(char *pKey)
{
	CoInitialize(NULL);	//initialize COM

	unsigned int i=0;

	const char subkey[]="SOFTWARE\\ARKO\\ARKOMASTER\\CONSOLE";
	const char valuename[]="Confpath";
	char CNF[260];
	unsigned int cbsize=260;
	if(am_LocaConfFile(subkey, valuename, CNF, &cbsize))
		return ERROR_INIFILE;
	strcat(CNF,"\\ARKODB.INI");	//get the path of config file

	char dbip[IPLEN];
	strcpy(dbip, GetDBIP(CNF));		//read the config file to get db ip
	if(!dbip)
		return ERROR_DBIP;

	char md5_dbip[MD5KLEN];
	i = md5hash((unsigned char *)dbip, strlen(dbip), (unsigned char *)md5_dbip);	//encrypt db ip by MD5
	if(i!=0)
		return ERROR_MD5_DBIP;

	char localip[IPLEN], md5_localip[MD5KLEN];
	BSTR wdes_localip=BSTRAS, wlocalip=BSTRAS;

	strcpy((char *)localip, GetLocalIP());		//get local ip
	wlocalip = c_MakeWideStrFromAnsi(localip);

	i = DesBSTREn(wlocalip, &wdes_localip, (unsigned char *)md5_dbip);	//encrypt local ip by DES
	if(i!=0)
		return ERROR_DES_LOCALIP;


	//connect to sqlservice
	IDBObject *m_SQLObject=NULL;	
	COSERVERINFO ServerInfo;
	memset(&ServerInfo, 0, sizeof(ServerInfo));
	ServerInfo.pwszName=new wchar_t[16];
	mbstowcs(ServerInfo.pwszName, dbip, 16);

	MULTI_QI mqi[]={
		{&IID_IDBObject,NULL,0}};

	HRESULT hRes=CoCreateInstanceEx(CLSID_DBObject,			
										NULL,
										CLSCTX_ALL,
										&ServerInfo,
										sizeof(mqi)/sizeof(mqi[0]),
										mqi);		
	
	if(hRes==0 && SUCCEEDED(mqi[0].hr))
		m_SQLObject=(IDBObject*)mqi[0].pItf;
	if(hRes!=S_OK || m_SQLObject==NULL)
	{
//		MessageBox(NULL, getErrorMessage(hRes), NULL, NULL);
		return ERROR_CONDB;
	}

	BSTR wencrypt_pkey=BSTRAS;
	//call the interface of sqlservice to get encrypted public key
	hRes = m_SQLObject->GetCryptKey(wdes_localip, &wencrypt_pkey); 
	if(hRes!=0)
	{
//		MessageBox(NULL, getErrorMessage(hRes), NULL, NULL);
		return ERROR_ENCRYPT_PKEY;
	}

	//encrypt local ip by MD5
	i = md5hash((unsigned char *)localip, strlen(localip), (unsigned char *)md5_localip);
	if(i!=0)
		return ERROR_MD5_LOCALIP;

	BSTR wdecrypt_pkey=BSTRAS;
	//use encrypted local ip to decrypt public key
	i = DesBSTRDe(wencrypt_pkey, &wdecrypt_pkey, (unsigned char *)md5_localip);
	if(i!=0)
		return ERROR_PKEY;

	char tkey[9];
	//get public key
	WideCharToMultiByte(CP_ACP, 0, wdecrypt_pkey, -1, tkey , 9 ,NULL, NULL);
	memcpy(pKey,tkey,8);

	m_SQLObject->Release();

	CoUninitialize();

	return SUCCEED;
}


//**************************************************************************
char * GetDBIP(char *filename)
{
	char *buf;
	char ch;
	int  i=0;
	fstream fp;

	fp.open(filename, ios::in|ios::nocreate);	//open the file
	if(!fp) return NULL;

	if((buf=(char *)malloc(IPLEN))==NULL)
		return NULL;

	while(!isdigit(ch=fp.peek()))
		fp.get();

	fp.getline(buf, IPLEN);
	while(i++,buf[i]!=' '&&buf[i]!=0)
		;
	 buf[i]=0;

	fp.close();

	return buf;
}


//*****************************************************************************
char *GetLocalIP()
{
	struct in_addr inaddr;
	int wVerReg;
	WSADATA wsaData;
	struct hostent *htpr;
	char hostname[MAX_HOSTNAME_LEN];

	wVerReg = 0x0002;
	if(WSAStartup(wVerReg, &wsaData))
		return NULL;

	if(gethostname(hostname, MAX_HOSTNAME_LEN))
		return NULL;

	if(!(htpr=gethostbyname(hostname)))
		return NULL;

	memcpy((char *)&inaddr, htpr->h_addr_list[0], htpr->h_length);
	WSACleanup();

	char *ipaddress;
	if((ipaddress=(char *)malloc(16))==NULL)
		return NULL;
	strcpy(ipaddress, inet_ntoa(inaddr));

	return ipaddress;
}