123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- #include "stdafx.h"
- #include "lyfzHDSerial.h"
- #include "yxyDES2.h"
- #include <iphlpapi.h>
- #pragma comment(lib,"iphlpapi.lib")
- #include <comdef.h>
- #include <wbemidl.h>
- #include <Wbemcli.h>
- #pragma comment(lib,"Wbemuuid.lib")
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- char bitsCiphertextAnyLength[32768] = {0};
- char hexCiphertextAnyLength[16384] = {0};
- yxyDES2 g_DES2;
- void ConvertCiphertext2OtherFormat(int iBitsLen,char *szCipherInBytes);
- int ConvertOtherFormat2Ciphertext(char *szCipher, char* pCiphertext);
- BEGIN_MESSAGE_MAP(ClyfzHDSerialApp, CWinApp)
- END_MESSAGE_MAP()
- ClyfzHDSerialApp::ClyfzHDSerialApp()
- {
-
-
- }
- ClyfzHDSerialApp::~ClyfzHDSerialApp()
- {
-
- }
- ClyfzHDSerialApp theApp;
- BOOL ClyfzHDSerialApp::InitInstance()
- {
- CWinApp::InitInstance();
- return TRUE;
- }
- int ClyfzHDSerialApp::ExitInstance()
- {
- return CWinApp::ExitInstance();
- }
- void _getWmiInfo(IWbemClassObject *pClassObject, LPCTSTR lpszName, CString& str);
- extern "C" INT PASCAL EXPORT GetSeiralNumberOrID(IN LPWSTR lpszClass, IN LPWSTR lpszName, OUT LPWSTR str)
- {
-
- if(str == NULL)
- return 0;
-
- if (CoInitializeEx(0, COINIT_MULTITHREADED) != S_OK)
- return 0;
-
- if( CoInitializeSecurity(
- NULL,
- -1,
- NULL,
- NULL,
- RPC_C_AUTHN_LEVEL_DEFAULT,
- RPC_C_IMP_LEVEL_IMPERSONATE,NULL,
- EOAC_NONE,
- 0 ) != S_OK )
- {
- AfxMessageBox(_T("访问权限失败"));
- return 0;
- }
-
- CComPtr<IWbemLocator> spWbemLocator;
- if( spWbemLocator.CoCreateInstance(
- CLSID_WbemAdministrativeLocator,
- 0 ,
- CLSCTX_INPROC_SERVER| CLSCTX_LOCAL_SERVER
- ) != S_OK )
- {
- AfxMessageBox(_T(" 获取系统信息失败"));
- return 0;
- }
-
- CComPtr<IWbemServices> spWbemServices;
- if( spWbemLocator->ConnectServer(
- _T("root\\cimv2"),
- NULL,
- NULL,
- NULL,
- 0,
- NULL,
- NULL,
- &spWbemServices) != S_OK )
- {
- spWbemLocator.Release();
- AfxMessageBox(_T(" 获取系统信息失败"));
- return 0;
- }
-
- HRESULT hres = CoSetProxyBlanket(
- spWbemServices,
- RPC_C_AUTHN_WINNT,
- RPC_C_AUTHZ_NONE,
- NULL,
- RPC_C_AUTHN_LEVEL_CALL,
- RPC_C_IMP_LEVEL_IMPERSONATE,
- NULL,
- EOAC_NONE
- );
- if(FAILED(hres))
- {
- spWbemServices.Release();
- spWbemLocator.Release();
- AfxMessageBox(_T(" 获取系统信息失败"));
- return 0;
- }
-
-
- USES_CONVERSION ;
- CComPtr<IEnumWbemClassObject> spEnumWbemClassObject;
- CComBSTR bstrQuery ( _T("Select * from "));
- bstrQuery += T2OLE((LPTSTR)lpszClass) ;
-
- if( spWbemServices->ExecQuery( _T("WQL"), bstrQuery, WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &spEnumWbemClassObject) != S_OK )
- {
- spWbemServices.Release();
- spWbemLocator.Release();
- AfxMessageBox(_T(" 获取系统信息失败"));
- return 0;
- }
- CComPtr<IWbemClassObject> spClassObject;
- ULONG uCount = 1, uReturned;
- if( spEnumWbemClassObject->Reset() != S_OK)
- {
- spEnumWbemClassObject.Release();
- spWbemServices.Release();
- spWbemLocator.Release();
- AfxMessageBox(_T(" 获取系统信息失败"));
- return 0;
- }
- int iEnumIdx = 0;
- while( spEnumWbemClassObject->Next(WBEM_INFINITE,uCount, &spClassObject, &uReturned) == S_OK )
- {
- CString strtmp = _T("");
- _getWmiInfo( spClassObject, lpszName, strtmp);
- memcpy(str,strtmp.GetString(), strtmp.GetLength()*2);
- spClassObject.Release() ;
- }
- spEnumWbemClassObject.Release();
- spWbemServices.Release();
- spWbemLocator.Release();
- CoUninitialize();
- return 1;
- }
- void _getWmiInfo(IWbemClassObject *pClassObject, LPCTSTR lpszName, CString& str)
- {
- CComVariant varValue;
- CComBSTR bstrName(lpszName);
- if( pClassObject->Get( bstrName , 0 , &varValue , NULL , 0 ) == S_OK )
- {
-
- if( varValue.vt == VT_BSTR )
- {
- str = CString(varValue.bstrVal) ;
- }
- else if( varValue.vt == VT_ARRAY )
- {
- long iLowBound = 0 , iUpBound = 0 ;
- SafeArrayGetLBound( varValue.parray , 1 , &iLowBound ) ;
- SafeArrayGetUBound( varValue.parray , 1 , &iUpBound ) ;
- for( long j = iLowBound ; j <= iUpBound ; j ++ )
- {
- VARIANT *pvar = NULL ;
- long temp = j ;
- if( SafeArrayGetElement( varValue.parray , &temp , pvar ) == S_OK && pvar )
- {
- CComVariant varTemp ;
- if( varTemp.ChangeType( VT_BSTR , pvar ) == S_OK )
- {
- if( !str.IsEmpty() )
- str += _T(",") ;
- str += varTemp.bstrVal ;
- }
- }
- }
- }
- else
- {
- switch( varValue.vt)
- {
- case VT_I4:
- str.Format(_T("%d"), varValue.lVal);
- break;
- case VT_I8:
- str.Format(_T("%l"), varValue.llVal);
- break;
- case VT_I2:
- str.Format(_T("%d"), varValue.iVal);
- break;
- case VT_UI1:
- str.Format(_T("%uc"), varValue.bVal);
- break;
- case VT_R4:
- str.Format(_T("%f"), varValue.fltVal);
- break;
- case VT_R8:
- str.Format(_T("%lf"), varValue.dblVal);
- break;
- case VT_I1:
- str.Format(_T("%c"), varValue.cVal);
- break;
- case VT_UI2:
- str.Format(_T("%ud"), varValue.uiVal);
- break;
- case VT_UI4:
- str.Format(_T("%ud"), varValue.ulVal);
- break;
- case VT_UI8:
- str.Format(_T("%ul"), varValue.ullVal);
- break;
- case VT_BOOL:
-
- break;
- case VT_NULL:
- str = _T("NULL");
- break;
- default:
- if( varValue.ChangeType( VT_BSTR ) == S_OK )
- str = varValue.bstrVal ;
- }
- }
-
-
- }
- else
- str = _T("");
- }
- extern "C" VOID PASCAL EXPORT GetLocalMacAddr(OUT LPSTR pMac)
- {
- if(pMac == NULL)
- return;
-
- IP_ADAPTER_INFO adapter[5];
- memset(adapter, 0, sizeof(IP_ADAPTER_INFO)*5);
- DWORD buflen = sizeof(adapter);
- DWORD status = GetAdaptersInfo(adapter,&buflen);
- if(status != ERROR_SUCCESS)
- return;
- BYTE s[8] = {0};
- PIP_ADAPTER_INFO painfo = adapter;
- memcpy(s, painfo->Address, 6);
- sprintf(pMac, "%02x-%02x-%02x-%02x-%02x-%02x", s[0], s[1], s[2], s[3], s[4], s[5]);
-
- }
- extern "C" VOID PASCAL EXPORT DESEncrypt(IN LPCSTR pPlaintext, IN LPCSTR pKey, OUT LPSTR pCiphertext)
- {
- if(pCiphertext == NULL || pPlaintext == NULL || pKey == NULL)
- return;
- g_DES2.InitializeKey((char*)pKey, 0);
- g_DES2.EncryptAnyLength((char*)pPlaintext, strlen(pPlaintext), 0);
- char* ptext = g_DES2.GetCiphertextAnyLength();
- ConvertCiphertext2OtherFormat(strlen(pPlaintext) % 8 == 0 ? strlen(pPlaintext) << 3 : ((strlen(pPlaintext)>>3) + 1) << 6, ptext);
- if(ptext == NULL)
- return;
-
- memcpy(pCiphertext, hexCiphertextAnyLength, strlen(hexCiphertextAnyLength));
- }
- extern "C" VOID PASCAL EXPORT DESDecrypt(IN LPCSTR pCiphertext, IN LPCSTR pKey, OUT LPSTR pPlaintext)
- {
- if(pCiphertext == NULL || pKey == NULL || pPlaintext == NULL)
- return;
- char szCiphertextData[8192] = {0};
- g_DES2.InitializeKey((char*)pKey, 0);
- int nLen = ConvertOtherFormat2Ciphertext((char*)pCiphertext, szCiphertextData);
- g_DES2.DecryptAnyLength(szCiphertextData, nLen, 0);
- char* ptext = g_DES2.GetPlaintextAnyLength();
- if(ptext == NULL)
- return;
-
- memcpy(pPlaintext, ptext, strlen(ptext));
- }
- void ConvertCiphertext2OtherFormat(int iBitsLen, char* szCipherInBytes)
- {
- memset(hexCiphertextAnyLength,0,16384);
- memset(bitsCiphertextAnyLength,0,32768);
- g_DES2.Bytes2Bits(szCipherInBytes, bitsCiphertextAnyLength, iBitsLen);
- g_DES2.Bits2Hex(hexCiphertextAnyLength, bitsCiphertextAnyLength, iBitsLen);
- for(int i=0; i<iBitsLen; i++)
- bitsCiphertextAnyLength[i] += 48;
- }
- int ConvertOtherFormat2Ciphertext(char* szCipher, char* pCiphertext)
- {
- if(pCiphertext == NULL)
- return 0;
- int iLen = 0;
- memset(bitsCiphertextAnyLength,0,32768);
-
- iLen = ((strlen(szCipher)>>2) + (strlen(szCipher) % 4 == 0 ? 0 : 1))<<4;
- memcpy(hexCiphertextAnyLength, szCipher, strlen(szCipher));
- g_DES2.Hex2Bits(hexCiphertextAnyLength, bitsCiphertextAnyLength, iLen);
- g_DES2.Bits2Bytes(pCiphertext, bitsCiphertextAnyLength, iLen);
-
-
- return iLen>>3;
- }
|