【问题标题】:Trying to convert Javascript recursion to PHP (easy)尝试将 Javascript 递归转换为 PHP(简单)
【发布时间】:2016-07-10 21:18:08
【问题描述】:

我必须为学校项目将一些 javascript 转换为 PHP,但我似乎缺少一些东西,因为完全相同的代码突然过早退出。

for(var j=0; j<asciiArray.length;j++) {
    passwordFound[location] = String.fromCharCode(asciiArray[j]);

    console.log(passwordFound.join(""));

    if (password === passwordFound.join("")) {
        document.getElementById("password").innerHTML = 'Password: ' + passwordFound.join("");
        return true;
    }
    else if (location < 2) {
        var newlocation = location+1;

        if (characterDecryptFunction(newlocation,asciiArray,passwordFound,password)) return true;
    }

}

这是PHP:

function characterDecryptFunction($index, $maxIndex, $asciiArray, $passwordFound, $password)  { 
for ($j=0;$j<count($asciiArray);++$j)
{
    $passwordFound[$index] = chr($asciiArray[$j]);

    echo "<br>" . implode("", $passwordFound);

    if ($password === implode("",$passwordFound)) {
        echo "<br>Password is:" . implode($passwordFound);
        return true;
    }

    elseif ($index < $maxIndex)
    {
        $index = $index+1;

        if (characterDecryptFunction($index,$maxIndex, $asciiArray, $passwordFound, $password) == true) return true;
    }
}
return false;}

编辑:

javascript 是这样调用的:

function decryptFunction() {
var x,y,z,password,asciiArray=[],passwordFound=[];

password="abc";
asciiArray.push(0);
asciiArray.push(32);

for (x=48;x!=58;x++) {
    asciiArray.push(x);
}
for (y=97;y!=123;y++) {
    asciiArray.push(y);
}
for (z=65;z!=91;z++) {
    asciiArray.push(z);
}

characterDecryptFunction(0, asciiArray, passwordFound,password);}

还有 PHP:

function decryptFunction() {
$password = $_POST["password"];
$asciiArray=array();
$passwordFound=array();

for($x=48;$x!=58;$x++) 
{
    array_push($asciiArray, $x);
}

for($x=97;$x!=123;$x++) 
{
    array_push($asciiArray, $x);
}

for($x=65;$x!=91;$x++) 
{
    array_push($asciiArray, $x);
}

for ($x=0;$x<count($asciiArray);$x++) 
{
    echo $asciiArray[$x];
}

echo $password . "<br>";

characterDecryptFunction(0, 2, $asciiArray, $passwordFound, $password); }

【问题讨论】:

  • 过早地说,是什么意思?此外,您会注意到 PHP 版本有额外的回报。我不知道这是否是错误,只是一个明显的差异。
  • 原来的代码遍历了所有的选项,这个突然停在了 01Z。它只是一个基本的破解算法,但似乎不是从 01x 到 02x。额外的回报也在原件中,但我忘记发布了。
  • 01x02x 是什么意思?
  • 好吧,程序的功能是这样的:有一个数组,其中填充了可能与大写、小写和数字相关的 ascii 代码。通过递归,我尝试猜测 $password 变量。所以 000, 001, 002, 003 等等。原来的 javascript 代码可以工作,但是 PHP 代码突然停在 01Z,这是它应该转到 020 的截止点。
  • 您能否编辑问题,添加代码,以便我们查看您如何调用该函数。

标签: javascript php recursion


【解决方案1】:

不更新 $index 似乎很重要,就像在 JavaScript 中不更新 location 一样。于是改PHP代码,引入$new_index

    $new_index = $index+1;

    if (characterDecryptFunction($new_index,$maxIndex, $asciiArray, $passwordFound, $password)) return true;

当算法从递归调用返回时,即当它回溯到之前的字符时,它应该从中断的地方继续。对于那个 $index 必须保留它以前的值。

注意:请注意,在 PHP 中,就像在 JavaScript 中一样,当您知道表达式是布尔值时,您无需在 if 语句中与 true 进行比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 2015-05-30
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多