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

  • SEARCH
  • Home
  • 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 7189943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:24:35+00:00 2026-05-28T19:24:35+00:00

I am implementing a method to encrypt with a key and i made a

  • 0

I am implementing a method to encrypt with a key and i made a call like this:

Crypto c = new Crypto("mysecretkey");
String enc = c.encrypt("mytext");

But i am getting an exception

“crypto encrypt error: String index out of range: -1”

at this part:

String sKeyChar = getKey().substring((i % getKey().length()) - 1, 1);

And I don’t know what I am doing wrong because I made the same thing in PHP and works good. Maybe this is simple but I am stuck, this is my method:

public String encrypt(String sData) {
        String sEncrypted = null;
        try {
            String sResult = null;
            for (int i = 0; i < sData.length(); i++) {
                String sChar = sData.substring(i, 1);
                String sKeyChar = getKey().substring((i % getKey().length()) - 1, 1);
                char c = (char) (ord(sChar) - ord(sKeyChar));
                String sPart = (new StringBuffer().append(c)).toString();
                sResult += sPart;
            }
            byte[] sResultBuff = sResult.getBytes("UTF-8");
            sEncrypted = Base64.encode(sResultBuff);
        } catch (Exception e) {
            System.out.println("crypto encrypt error: " + e.getMessage());
            sEncrypted = null;
        }
        return sEncrypted;
    }

Other method needed:

public int ord(String sChar) {
    int ascii_code = 0;
    try {
        ascii_code = String.valueOf(sChar.charAt(0)).codePointAt(0);
    } catch (Exception e) {
        System.out.println("crypto ord error: " + e.getMessage());
        ascii_code = 0;
    }
    return ascii_code;
}

PHP equivalent method:

function encrypt($sData, $sKey='mysecretkey'){ 
    $sResult = ''; 
    for($i=0;$i<strlen($sData);$i++){ 
        $sChar    = substr($sData, $i, 1); 
        $sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1); 
        $sChar    = chr(ord($sChar) + ord($sKeyChar)); 
        $sResult .= $sChar; 
    } 
    return encode_base64($sResult); 
} 

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-05-28T19:24:35+00:00Added an answer on May 28, 2026 at 7:24 pm

    You see the difference between PHP and Java because PHP’s substr understands negative numbers, but Java’s substring does not: it throws an exception.

    In PHP, passing negative 1 to substr means “get me the last character”, but in Java you need to pass the index of the last character (i.e. str.length()-1) to achieve the same effect.

    If this is not a mistake, and this is precisely the effect that you wanted to achieve, you can address this issue with an if condition:

    int pos = (i % getKey().length()) - 1;
    if (pos == -1) {
        pos = getKey().length() - 1;
    }
    // EDIT: Second argument needs to be pos+1, not 1. Thomas pointed out this error
    String sKeyChar = getKey().substring(pos, pos+1);
    

    EDIT As Thomas correctly pointed out, the other difference between PHP version of substr and Java’s substring is in their treatment of the second argument: PHP thinks it’s length; Java thinks it’s the index of the last character plus one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm having a problem implementing this method. I have added using System.Management but
When implementing this method of NSCopying in a class to enable copy, what is
I'm implementing a method that is returning a json result like: public JsonResult MethodName(Guid
I'm new to the UITableView and noticed that w/out implementing a method the UITableViewCell
I'm new to Java and wondering how I could go about implementing a method
UPDATE: I tried implementing the method specified by Peter and am getting incorrect shadowing.
I'm updating my code generator and have a choice between implementing a method stub
I'm implementing type-independent method for filling DataRow with values. Intended functionality is quite straight
In Twisted when implementing the dataReceived method, there doesn't seem to be any examples
What is the preferred method for implementing such geometric distortions as pinch/fisheye/etc. using the

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.