richard hunter: Übersetzten einer JS-Funktion in PHP

Ich habe folgendes Problem: Ich brauche die unten stehende JavaScript Funktion als PHP Funktion. Leider scheitere ich kläglich an der erfolgreichen Übersetztung der while-schleife. Ich verwende folgende Testdaten:

encryptedMessage = "4ac6fd43c3c0fc37bd2d53c6023d59c5"
password = "password"
originalMessage = "PHP is great"

die JavaScript Funktion...

function decrypt(str, pwd) {
          if(str == null || str.length < 8) {
                alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
                return;
          }
          if(pwd == null || pwd.length <= 0) {
                alert("Please enter a password with which to decrypt the message.");
                return;
          }
          var prand = "";
          for(var i=0; i<pwd.length; i++) {
                prand += pwd.charCodeAt(i).toString();
          }
          var sPos = Math.floor(prand.length / 5);
          var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
          var incr = Math.round(pwd.length / 2);
          var modu = Math.pow(2, 31) - 1;
          var salt = parseInt(str.substring(str.length - 8, str.length), 16);
          str = str.substring(0, str.length - 8);
          prand += salt;
          while(prand.length > 10) {
                prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
          }
          prand = (mult * prand + incr) % modu;
          var enc_chr = "";
          var enc_str = "";
          for(var i=0; i<str.length; i+=2) {
                enc_chr = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255));
                enc_str += String.fromCharCode(enc_chr);
                prand = (mult * prand + incr) % modu;
          }
          return enc_str;
        }

die PHP Funktion (soweit geschafft)...

function decrypt($str, $pwd) {
                $prand = "";
                for($i=0; $i<strlen($pwd); $i++) {
                        $prand = $prand.ord($pwd{$i});
                }
                $sPos = floor(strlen($prand) / 5);
                $mult = $prand{$sPos}.$prand{$sPos*2}.$prand{$sPos*3}.$prand{$sPos*4}.$prand{$sPos*5};
                $incr = round(strlen($pwd) / 2);
                $modu = pow(2, 31) - 1;
                $salt = hexdec(substr($str, strlen($str)-8, strlen($str)-(strlen($str)-8)));
                $str = substr( $str, 0, (strlen($str)-8) );
                $prand = $prand.$salt;
                while(strlen($prand) > 10) {
                        $prand = "".((int)(substr($prand, 0, 10)) + ((int)substr($prand, 10, strlen($prand)-10))); # does not work!
                }
                # ...
        }

Vielen Dank für die Mühe!
Ric

  1. Hallo Richard,

    $prand = "".((int)(substr($prand, 0, 10)) + ((int)substr($prand, 10, strlen($prand)-10))); # does not work!

    Verwende statt eines "+"-Operators einen "."-Operator, um die Zeichenketten aneinanderzufügen. Also )) . (( statt )) + (( nehmen.

    Viele Grüße,
    Christian