Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8132267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:18:25+00:00 2026-06-06T09:18:25+00:00

I’ve been trying to convert a PHP code to Java, but its not working

  • 0

I’ve been trying to convert a PHP code to Java, but its not working as intended. I get an error in the loop with “String index out of range” after a few runs on char nextchar = inprogresskey.charAt(ranpos);

The PHP code is:

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;
    }

And my Java code is:

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();
    }

Am I missing out on some functions (charAt, indexOf)?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:18:26+00:00Added an answer on June 6, 2026 at 9:18 am

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

    atlast ranpos = 1

    and you are doing nextInt(1 - 1)

    char nextchar = inprogresskey.charAt(ranpos)
    

    that’s way above line gives you error

    what you need to do is:

    update your for loop for (int i = 0; i < unmixedkey.length(); i++)

    and inside the loop add the below line of code

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

    The below is fully functional for loop code.

    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;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.