samoht Posted October 15, 2008 Share Posted October 15, 2008 Hello all, I have a client that designs software that wants to have my website generate a proper key code so that when users down load the software they can plug in the key I generate and email to them from the website and it will match what the software would have generated. here is there code // AccessKeyGenerator.cpp : Defines the entry point for the console application. // #include "stdafx.h" BOOL ComputeMD5(BYTE *bHash, char *pBuffer, unsigned int uLength) { HCRYPTPROV hCryptProvider; HCRYPTHASH hHash; BOOL bResult = FALSE; if (CryptAcquireContext(&hCryptProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) { if (CryptCreateHash(hCryptProvider, CALG_MD5, 0, 0, &hHash)) { if (CryptHashData(hHash, (BYTE*)pBuffer, uLength, 0)) { DWORD dwHashLength = 16; if (CryptGetHashParam(hHash, HP_HASHVAL, bHash, &dwHashLength, 0)) bResult = TRUE; } CryptDestroyHash(hHash); } CryptReleaseContext(hCryptProvider, 0); } return bResult; } unsigned int ComputeFoldedMD5(CString& strCheck) { CT2CA strAscii(strCheck); BYTE bHash[16] = { 0 }; if (!ComputeMD5(bHash, strAscii, strlen(strAscii))) return 0; unsigned int a = (bHash[0x0] << 24) | (bHash[0x1] << 16) | (bHash[0x2] << | bHash[0x3]; unsigned int b = (bHash[0x4] << 24) | (bHash[0x5] << 16) | (bHash[0x6] << | bHash[0x7]; unsigned int c = (bHash[0x8] << 24) | (bHash[0x9] << 16) | (bHash[0xA] << | bHash[0xB]; unsigned int d = (bHash[0xC] << 24) | (bHash[0xD] << 16) | (bHash[0xE] << | bHash[0xF]; return a ^ b ^ c ^ d; } /* * Arguments are <login> <password> <day> <month> <year> <type> */ int _tmain(int argc, _TCHAR* argv[]) { if (argc != 7) { fprintf(stderr, "Usage: AccessKeyGenerator.exe login password day month year type\n"); fprintf(stderr, "\n"); fprintf(stderr, " login user name\n"); fprintf(stderr, " password user password\n"); fprintf(stderr, " day, month, year expiration date\n"); fprintf(stderr, " type license type\n"); fprintf(stderr, " STANDARD\n"); fprintf(stderr, " ADVANCED\n"); fprintf(stderr, " TRIAL\n"); fprintf(stderr, " USB\n"); exit(1); } CString strLogin = argv[1]; CString strPassword = argv[2]; unsigned int nDay = (unsigned int)_ttoi(argv[3]); unsigned int nMonth = (unsigned int)_ttoi(argv[4]); unsigned int nYear = (unsigned int)_ttoi(argv[5]); unsigned int nType = 0; if (!_tcsicmp(argv[6], _T("USB"))) nType = 0; else if (!_tcsicmp(argv[6], _T("TRIAL"))) nType = 1; else if (!_tcsicmp(argv[6], _T("STANDARD"))) nType = 2; else if (!_tcsicmp(argv[6], _T("ADVANCED"))) nType = 3; srand(time(NULL)); unsigned int nRandom = rand() & 0xFFFF; // Compute hashes unsigned int nLogin = ComputeFoldedMD5(strLogin); unsigned int nPassword = ComputeFoldedMD5(strPassword); // Combine date and flags unsigned int nClearDate = (nType << 13) | ((nYear - 2000) << 9) | (nMonth << 5) | nDay; // Obfuscate date data unsigned int nDate = (nClearDate ^ nRandom) | (nRandom << 16); // Create the check code unsigned int nStep = nLogin ^ nPassword ^ nDate ^ 0x845E2F66; unsigned int nUKey = (((nStep >> 28) & 0xF) << 12) | (((nStep >> 20) & 0xF) << | (((nStep >> 12) & 0xF) << 4) | (((nStep >> 4) & 0xF) << 0); unsigned int nVKey = (((nStep >> 24) & 0xF) << 12) | (((nStep >> 16) & 0xF) << | (((nStep >> & 0xF) << 4) | (((nStep >> 0) & 0xF) << 0); unsigned int nCheck = ((nVKey << 16) | nUKey) ^ 0x98FE1A5B; fprintf(stdout, "Expiration: %d-%d-%d\n", nDay, nMonth, nYear); fprintf(stdout, "Salt: 0x%04x\n", nRandom); fprintf(stdout, "Login: %S [0x%08x]\n", strLogin, nLogin); fprintf(stdout, "Password: %S [0x%08x]\n", strPassword, nPassword); fprintf(stdout, "Secret: 0x%08x [u=0x%04x v=0x%04x]\n", nCheck, nUKey, nVKey); fprintf(stdout, "Key: %08x%08x%08x%08x\n", nLogin, nPassword, nDate, nCheck); return 0; } I need to do the same thing in php but this is written in C++ anyone know how to help me with the conversion?? Link to comment https://forums.phpfreaks.com/topic/128582-help-creating-a-license-key-from-php/ Share on other sites More sharing options...
samoht Posted October 17, 2008 Author Share Posted October 17, 2008 Ok, This is the function that I got - which seems to work - but the key generation is not quite right? Can anyone see what might be a problem?? public function getProductKey( ){ $data = $this->getLicenseData(); $strLogin = $data['username']; $strPassword = $data['password']; /* get the type code */ switch( strtoupper( $data['type'] ) ){ case "BASIC": //used to be USB - changed by Andy $nType = 0; break; case "TRIAL": $nType = 1; break; case "STANDARD": $nType = 2; break; case "ADVANCED": $nType = 3; break; default: return 0; break; } //$nType = 3; /* Convert date inputs to integer values */ $nDay = date( 'd', $data['start_time'] ); $nMonth = date( 'm',$data['start_time'] ); $nYear = date( 'Y', $data['start_time'] ); /* Generate a random number */ //$nRandom = $rand & OxFFFF; $nRandom = 0x5045; /* Generate hashes of login and password */ $nLogin = $this->getFoldedMD5( trim($strLogin) ); $nPassword = $this->getFoldedMD5( trim($strPassword) ); /* Combine date and flags */ $nClearDate = ($nType << 13) | (($nYear - 2000) << 9) | ($nMonth << 5) | $nDay; /* Obfuscate date data */ $nDate = ($nClearDate ^ $nRandom) | ($nRandom << 16); /* Create the check code */ //$nStep = $nLogin ^ $nPassword ^ $nDate ^ 0x845E2F66; $nStep = 0x6c0b277b ^ 0xaab8585f ^ $nDate ^ 0x845E2F66; $nUKey = ((($nStep >> 28) & 0xF) << 12) | ((($nStep >> 20) & 0xF) << | ((($nStep >> 12) & 0xF) << 4) | ((($nStep >> 4) & 0xF) << 0); $nVKey = ((($nStep >> 24) & 0xF) << 12) | ((($nStep >> 16) & 0xF) << | ((($nStep >> & 0xF) << 4) | ((($nStep >> 0) & 0xF) << 0); $nCheck = (($nVKey << 16) | $nUKey) ^ 0x98FE1A5B; $key = sprintf("%08x%08x%08x%08x", $nLogin, $nPassword, $nDate, $nCheck); return $key; } static function getFoldedMD5( $word ){ /* Convert string to ANSI */ $bHash = md5( $word, TRUE ); $a = ( (ord($bHash[0x0]) << 24) | (ord($bHash[0x1]) << 16) | (ord($bHash[0x2]) << | (ord($bHash[0x3])) ); $b = ( (ord($bHash[0x4]) << 24) | (ord($bHash[0x5]) << 16) | (ord($bHash[0x6]) << | (ord($bHash[0x7])) ); $c = ( (ord($bHash[0x8]) << 24) | (ord($bHash[0x9]) << 16) | (ord($bHash[0xA]) << | (ord($bHash[0xB])) ); $d = ( (ord($bHash[0xC]) << 24) | (ord($bHash[0xD]) << 16) | (ord($bHash[0xE]) << | (ord($bHash[0xF])) ); return $a ^ $b ^ $c ^ $d; } the code looks similar to the c++ but it sees to be outputting a little different? Link to comment https://forums.phpfreaks.com/topic/128582-help-creating-a-license-key-from-php/#findComment-667939 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.