//---------------------------------------------------------------------------- // EgalTech 2013-2023 //---------------------------------------------------------------------------- // File : KeyProcess.cpp Data : 02.01.23 Versione : 2.5a1 // Contenuto : Implementazione funzioni costruzione chiave. // // // // Modifiche : 11.09.14 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "KeyMaker.h" #include "/EgtDev/Include/SELkLockId.h" #include "/EgtDev/Include/SELkKeyProc.h" #include "/EgtDev/Include/EGnStringUtils.h" #include "/EgtDev/Include/EgtIniFile.h" #include using namespace std ; //---------------------------------------------------------------------------- #ifdef _DEBUG #define new DEBUG_NEW #endif //---------------------------------------------------------------------------- KeyMaker::KeyMaker( void) { m_sCustomer.clear() ; m_sLockId.clear() ; m_sLockId2.clear() ; m_sScramKey.clear() ; m_nProd = 0 ; m_nVer = 0 ; m_nLev = 0 ; m_nExpDays = 0 ; m_nOpt1 = 0 ; m_nOpt2 = 0 ; m_nOptExpDays = 0 ; } //---------------------------------------------------------------------------- bool KeyMaker::SetCustomer( const string& sCustomer) { // verifico validità nome come nome di file if ( ! IsValidFileName( sCustomer)) return false ; m_sCustomer = sCustomer ; return true ; } //---------------------------------------------------------------------------- bool KeyMaker::SetLockId( const string& sLockId2) { // se identificativo di chiave, è direttamente in chiaro if ( sLockId2.find( KEY_LOCK_HW_START) != string::npos) { string sTrueLockId, sAddrPort ; SplitFirst( sLockId2, "@", sTrueLockId, sAddrPort) ; m_sLockId2 = sLockId2 ; m_sLockId = sTrueLockId ; // calcolo chiave di scramble string sScramKey ; if ( ! ConvertLockIdToScramKey( m_sLockId, sScramKey)) return false ; m_sScramKey = sScramKey ; } // altrimenti else { // calcolo identificativo macchina in chiaro string sLockId ; if ( ! ConvertLockId2ToLockId( sLockId2, sLockId)) return false ; // calcolo chiave di scramble string sScramKey ; if ( ! ConvertLockId2ToScramKey( sLockId2, sScramKey)) return false ; m_sLockId2 = sLockId2 ; m_sLockId = sLockId ; m_sScramKey = sScramKey ; } return true ; } //---------------------------------------------------------------------------- bool KeyMaker::SetProduct( const std::string& sProd) { int nProd ; if ( ! FromString( sProd, nProd) || nProd < 100 || nProd > 9999) return false ; m_nProd = nProd ; return true ; } //---------------------------------------------------------------------------- bool KeyMaker::SetVersion( const std::string& sVer) { int nVer ; if ( ! FromString( sVer, nVer) || nVer < 15 || nVer > 9999) return false ; m_nVer = nVer ; return true ; } //---------------------------------------------------------------------------- bool KeyMaker::SetLevel( const std::string& sLev) { int nLev ; if ( ! FromString( sLev, nLev) || nLev < 1 || nLev > 99) return false ; m_nLev = nLev ; return true ; } //---------------------------------------------------------------------------- bool KeyMaker::SetExpirDays( int nExpDays) { if ( nExpDays < 0 || nExpDays > 999999) return false ; m_nExpDays = nExpDays ; return true ; } //---------------------------------------------------------------------------- bool KeyMaker::SetOptions( unsigned int nOpt1, unsigned int nOpt2) { m_nOpt1 = nOpt1 ; m_nOpt2 = nOpt2 ; return true ; } //---------------------------------------------------------------------------- bool KeyMaker::SetOptExpirDays( int nOptExpDays) { if ( nOptExpDays < 0 || nOptExpDays > 999999) return false ; m_nOptExpDays = nOptExpDays ; return true ; } //---------------------------------------------------------------------------- bool KeyMaker::CalcKey( void) { string sKey ; if ( ! MakeKey( m_nProd, m_nVer, m_nLev, m_nExpDays, m_nOpt1, m_nOpt2, m_nOptExpDays, m_sScramKey, sKey)) return false ; m_sKey = sKey ; return true ; } //---------------------------------------------------------------------------- bool KeyMaker::GetKey( string& sKey) { if ( m_sKey.empty()) return false ; sKey = m_sKey ; return true ; } //---------------------------------------------------------------------------- bool KeyMaker::SaveData( bool bSpecial) { // direttorio di salvataggio string sDataDir = GetPrivateProfileStringUtf8( "General", "DataDir", "C:/EgtProg/KeyGenerator", AfxGetApp()->m_pszProfileName) ; TrimRight( sDataDir, "/\\") ; // path file di log string sLogFile = sDataDir + "\\" + m_sCustomer + ".Kge" ; // leggo indice ultimo salvataggio int nLast = GetPrivateProfileInt( "Index", "Last", 0, sLogFile.c_str()) ; // assegno nome sezione string sSec = "Licence" + ToString( nLast) ; // se cambiati i dati, aggiungo nuova versione di licenza if ( DiffLicence( sLogFile.c_str())) { // salvo nuovo indice salvataggio if ( ! bSpecial) ++ nLast ; WritePrivateProfileInt( "Index", "Last", nLast, sLogFile.c_str()) ; // aggiorno nome sezione sSec = "Licence" + ToString( nLast) ; // salvo i nuovi dati if ( ! WriteLicence( sSec.c_str(), sLogFile.c_str(), true)) return false ; } // path file licenza string sLicFile = sDataDir + "\\" + m_sCustomer + ( bSpecial ? "" : "_" + sSec) + ".lic" ; // linea per evitare problemi con BOM ofstream ofOut( stringtoW( sLicFile), ios_base::out | ios_base::trunc) ; if ( ! ofOut.good()) return false ; ofOut << "; Commento per evitare BOM con UTF-8" << endl ; ofOut.close() ; // salvo i nuovi dati return WriteLicence( "Licence", sLicFile.c_str(), false) ; } //---------------------------------------------------------------------------- bool KeyMaker::DiffLicence( const char* szFile) { // leggo indice ultimo salvataggio int nLast = GetPrivateProfileInt( "Index", "Last", 0, szFile) ; // sezione ultimo salvataggio string sSec = "Licence" + ToString( nLast) ; // leggo i dati dell'ultimo salvataggio e li confronto con gli attuali string sCust = GetPrivateProfileStringUtf8( sSec.c_str(), "Customer", "", szFile) ; if ( sCust != m_sCustomer) return true ; string sLockId = GetPrivateProfileStringUtf8( sSec.c_str(), "LockId", "", szFile) ; if ( sLockId != m_sLockId2) return true ; string sClearLockId = GetPrivateProfileStringUtf8( sSec.c_str(), "ClearLockId", "", szFile) ; if ( sClearLockId != m_sLockId) return true ; int nProd = GetPrivateProfileInt( sSec.c_str(), "Product", 0, szFile) ; if ( nProd != m_nProd) return true ; int nVer = GetPrivateProfileInt( sSec.c_str(), "Ver", 0, szFile) ; if ( nVer != m_nVer) return true ; int nLev = GetPrivateProfileInt( sSec.c_str(), "Lev", 0, szFile) ; if ( nLev != m_nLev) return true ; int nExpDays = GetPrivateProfileInt( sSec.c_str(), "ExpDays", 0, szFile) ; if ( nExpDays != m_nExpDays) return true ; int nOpt1 = GetPrivateProfileInt( sSec.c_str(), "Opt1", 0, szFile) ; if ( nOpt1 != m_nOpt1) return true ; int nOpt2 = GetPrivateProfileInt( sSec.c_str(), "Opt2", 0, szFile) ; if ( nOpt2 != m_nOpt2) return true ; int nOptExpDays = GetPrivateProfileInt( sSec.c_str(), "OptExpDays", 0, szFile) ; if ( nOptExpDays != m_nOptExpDays) return true ; string sKey = GetPrivateProfileStringUtf8( sSec.c_str(), "Key", "", szFile) ; if ( sKey != m_sKey) return true ; return false ; } //---------------------------------------------------------------------------- bool KeyMaker::WriteLicence( const char* szSec, const char* szFile, bool bAll) { if ( bAll) WritePrivateProfileStringUtf8( szSec, "Date", CurrDateTime().c_str(), szFile) ; WritePrivateProfileStringUtf8( szSec, "Customer", m_sCustomer.c_str(), szFile) ; WritePrivateProfileStringUtf8( szSec, "LockId", m_sLockId2.c_str(), szFile) ; if ( bAll) WritePrivateProfileStringUtf8( szSec, "ClearLockId", m_sLockId.c_str(), szFile) ; WritePrivateProfileInt( szSec, "Product", m_nProd, szFile) ; WritePrivateProfileInt( szSec, "Ver", m_nVer, szFile) ; WritePrivateProfileInt( szSec, "Lev", m_nLev, szFile) ; WritePrivateProfileInt( szSec, "ExpDays", m_nExpDays, szFile) ; WritePrivateProfileInt( szSec, "Opt1", m_nOpt1, szFile) ; WritePrivateProfileInt( szSec, "Opt2", m_nOpt2, szFile) ; WritePrivateProfileInt( szSec, "OptExpDays", m_nOptExpDays, szFile) ; WritePrivateProfileStringUtf8( szSec, "Key", m_sKey.c_str(), szFile) ; return true ; }