JoshGlzBrk Posted September 9, 2009 Share Posted September 9, 2009 I'M trying to convert this c++ code into PHP and have tried to do so, but I cannot seem to get the result I'M looking for. Here is the original C++ code. void EncryptMXUDP(unsigned char *pBuf, const int iLen) { unsigned int dwMax = (unsigned int)iLen + ((unsigned int)iLen << 4); if(!dwMax) return; unsigned int dwCnt = 1; while(dwCnt < dwMax) { pBuf[dwCnt % (unsigned int)iLen] = MXUDPTable_3[pBuf[dwCnt % (unsigned int)iLen]] ^ MXUDPTable_1[MXUDPTable_0[(dwCnt - 1) & 0xFF] ^ pBuf[(dwCnt - 1) % (unsigned int)iLen]]; dwCnt++; } } Here is the translated PHP Code: function EncryptMXUDP(&$data) { global $WPNEncryption7, $WPNEncryption8, $WPNEncryption10; $len = strlen($data); $dwMax = $len + ($len << 4); $dwCnt = 1; $pBuf = Bin2Array($data); while($dwCnt < $dwMax) { // pBuf[dwCnt % (unsigned int)iLen] = MXUDPTable_3[pBuf[dwCnt % (unsigned int)iLen]] ^ MXUDPTable_1[MXUDPTable_0[(dwCnt - 1) & 0xFF] ^ pBuf[(dwCnt - 1) % (unsigned int)iLen]]; $pBuf[($dwCnt % $len)] = $WPNEncryption10[$pBuf[($dwCnt % $len)]] & $WPNEncryption8[$WPNEncryption7[($dwCnt - 1) & 0xFF] ^ $pBuf[($dwCnt - 1) % $len]]; $dwCnt++; } $data = Array2Bin($pBuf); } function Bin2Array($data) { $byteArray = Array(); for ($i=0;$i < strlen($data); $i++) $byteArray[$i] = ord($data[$i]); return $byteArray; } function Array2Bin($data) { // return implode("", $data); $returnVariable = ""; foreach ($data as $d) $returnVariable .= chr($d); return $returnVariable; } I cannot seem to get the result I want. Have I converted something wrong? Link to comment https://forums.phpfreaks.com/topic/173612-check-my-conversion-please/ Share on other sites More sharing options...
corbin Posted September 9, 2009 Share Posted September 9, 2009 I really only see one difference, although I'm not looking very closely. In the C++ function, you're operating on pBuf as you go, but in the PHP function, you're operating on a temporary variable then setting the variable passed as reference to that. If the same index is ever operated on, that will mean the results will not be the same. Also, try echoing out data every iteration to see if you can track down when the operation is going wrong. Link to comment https://forums.phpfreaks.com/topic/173612-check-my-conversion-please/#findComment-915142 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.