【发布时间】: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。额外的回报也在原件中,但我忘记发布了。
-
01x和02x是什么意思? -
好吧,程序的功能是这样的:有一个数组,其中填充了可能与大写、小写和数字相关的 ascii 代码。通过递归,我尝试猜测 $password 变量。所以 000, 001, 002, 003 等等。原来的 javascript 代码可以工作,但是 PHP 代码突然停在 01Z,这是它应该转到 020 的截止点。
-
您能否编辑问题,添加代码,以便我们查看您如何调用该函数。
标签: javascript php recursion