我一直在尝试将 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)吗?
谢谢