-1

我一直在尝试将 PHP 代码转换为 Java,但它没有按预期工作。运行几次后,循环中出现“字符串索引超出范围”错误char nextchar = inprogresskey.charAt(ranpos);

PHP代码是:

function munge($address)
    {
        $address = strtolower($address);
        $coded = "";
        $unmixedkey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.@";
        $inprogresskey = $unmixedkey;
        $mixedkey="";
        $unshuffled = strlen($unmixedkey);
        for ($i = 0; $i <= strlen($unmixedkey); $i++)
        {
            $ranpos = rand(0,$unshuffled-1);
            $nextchar = $inprogresskey{$ranpos};
            $mixedkey .= $nextchar;
            $before = substr($inprogresskey,0,$ranpos);
            $after = substr($inprogresskey,$ranpos+1,$unshuffled-($ranpos+1));
            $inprogresskey = $before.''.$after;
            $unshuffled -= 1;
        }
        $cipher = $mixedkey;

        $shift = strlen($address);

        for ($j=0; $j<strlen($address); $j++)
        {
            if (strpos($cipher,$address{$j}) == -1 )
            {
                    $chr = $address{$j};
                    $coded .= $address{$j};
            }
            else
            {
                    $chr = (strpos($cipher,$address{$j}) + $shift) % strlen($cipher);
                    $coded .= $cipher{$chr};
            }
        }

        $txt = "<script type=\"text/javascript\" language=\"javascript\">\n";
        $txt .= "\ncoded = \"" . $coded . "\"\n" .
            "  key = \"".$cipher."\"\n".
            "  shift=coded.length\n".
            "  link=\"\"\n".
            "  for (i=0; i<coded.length; i++) {\n" .
            "    if (key.indexOf(coded.charAt(i))==-1) {\n" .
            "      ltr = coded.charAt(i)\n" .
            "      link += (ltr)\n" .
            "    }\n" .
            "    else {     \n".
            "      ltr = (key.indexOf(coded.charAt(i))-
    shift+key.length) % key.length\n".
            "      link += (key.charAt(ltr))\n".
            "    }\n".
            "  }\n".
            "document.write(\"<a href='mailto:\"+link+\"'>\"+link+\"</a>\")\n" .
            "\n".
            "//-"."->\n" .
            "<" . "/script><noscript>N/A" .
            "<"."/noscript>";
        return $txt;
    }

我的Java代码是:

private String encryptEmail(String email)
    {
        String address = email.toLowerCase();
        String coded = "";
        String unmixedkey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.@";
        String inprogresskey = unmixedkey;
        String mixedkey = "";
        int unshuffled = unmixedkey.length();

        for (int i = 0; i <= unmixedkey.length(); i++) {
           Random random = new Random();
           int ranpos = random.nextInt(unshuffled - 1);

           char nextchar = inprogresskey.charAt(ranpos);
           mixedkey += nextchar;

           String before = StringUtils.substring(inprogresskey, 0, ranpos);
           String after = StringUtils.substring(inprogresskey, ranpos + 1, unshuffled - (ranpos + 1));
           inprogresskey = before + "" + after;
           unshuffled -= 1;
        }

        String cipher = mixedkey;
        int shift = address.length();

        for (int j = 0; j < address.length(); j++) {
            int chr = -1;
            if (StringUtils.indexOf(cipher, address.substring(j - 1, j)) == -1) {
                coded += address.charAt(j);
            } else {
                chr = (cipher.charAt(j + shift)) % cipher.length();
                coded += cipher.charAt(chr);
            }
       }

       StringBuilder sb = new StringBuilder();
       sb.append("<script type=\"text/javascript\">\n");
       sb.append("var coded = \"" + coded + "\";\n");
       sb.append("var key = \"" + cipher + "\";\n");
       sb.append("var shift = coded.length;\n");
       sb.append("var link = \"\";\n");
       sb.append("for (i = 0; i < coded.length; i++) {\n");
       sb.append("  if (key.indexOf(coded.charAt(i))==-1) {\n");
       sb.append("      ltr = coded.charAt(i);\n");
       sb.append("      link += (ltr);\n");
       sb.append("  }\n");
       sb.append("  else {\n");
       sb.append("      ltr = (key.indexOf(coded.charAt(i))-shift+key.length) % key.length;\n");
       sb.append("      link += (key.charAt(ltr));\n");
       sb.append("  }");
       sb.append("}");
       sb.append("document.write(\"<a rel='nofollow' href='mailto:\" + link + \"'>\" + link + \"</a>\");\n");
       sb.append("</script>");

       return sb.toString();
    }

我错过了一些功能(charAt,indexOf)吗?

谢谢

4

2 回答 2

1

我怀疑这unshuffled等于0最后一次通过循环,所以charAt(-1)失败了。

您应该看看像 Eclipse 和调试器这样的 Java IDE。添加断点将使您能够在代码运行时单步执行代码,并查看所有变量的值,这将是未来解决此类问题的最快方法。

于 2012-06-26T07:57:45.590 回答
1

int ranpos = random.nextInt(unshuffled - 1);

终于ranpos = 1

你正在做nextInt(1 - 1)

char nextchar = inprogresskey.charAt(ranpos)

这就是上面的方式给你错误

你需要做的是:

更新你的 for 循环 for (int i = 0; i < unmixedkey.length(); i++)

并在循环内添加以下代码行

    if(unshuffled==1)
      {
          ranpos = 1;
      }     
    else {
         ranpos = random.nextInt(unshuffled - 1);
        }

以下是循环代码的完整功能。

for (int i = 0; i < unmixedkey.length(); i++) {
           Random random = new Random();
           int ranpos=0;
           if(unshuffled==1)
           {
               ranpos = 1;
           }else{
               ranpos = random.nextInt(unshuffled - 1);
           }
           char nextchar = inprogresskey.charAt(ranpos);
           mixedkey += nextchar;
           String before = StringUtils.substring(inprogresskey, 0, ranpos);
           String after = StringUtils.substring(inprogresskey, ranpos + 1, unshuffled - (ranpos + 1));
           inprogresskey = before + "" + after;
           unshuffled -= 1;
        }
于 2012-06-26T08:06:29.653 回答