Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PHP函数转换为Java_Java_Php_Function_Email - Fatal编程技术网

将PHP函数转换为Java

将PHP函数转换为Java,java,php,function,email,Java,Php,Function,Email,我一直在尝试将PHP代码转换为Java,但它没有按预期工作。在char nextchar=inprogresskey.charAtranpos上运行了几次之后,我在循环中遇到了一个错误,字符串索引超出了范围 PHP代码是: function munge($address) { $address = strtolower($address); $coded = ""; $unmixedkey = "ABCDEFGHIJKLMNOPQRSTUV

我一直在尝试将PHP代码转换为Java,但它没有按预期工作。在char nextchar=inprogresskey.charAtranpos上运行了几次之后,我在循环中遇到了一个错误,字符串索引超出了范围

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

谢谢

我怀疑上次通过循环时unshuffled等于0,所以charAt-1失败了

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

int ranpos=random.nextintunsuffled-1

至少ranpos=1

你正在做下一步1-1

这条线以上的地方会给你们错误

您需要做的是:

为int i=0更新for循环;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;
        }

你可以把它贴到代码审查上,我想它会更明确。。。代码审查是针对需要清理/重构等工作的代码。如果代码还不能工作,那么它就脱离主题了。
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;
        }