0

I'm working on a java project that needs to have an authentication from accounts created on a local website. Those account's passwords are encrypted by a php function before inserted to the database :

public function crypter($mdp){
    $j = 0;
    $tmp = 0;
    $key = $mdp;

    $res = "";

    for($i = 0; $i < strlen($mdp) ; $i++)
    {
        $tmp = ord($mdp[$i]) + ord($key[$i]);
        if($tmp > 255){
            $tmp = $tmp - 256;
        }

        $res[$i] = chr($tmp);

        if($j == strlen($key)-1){
            $j = 0;
        }
        else{
            $j = (($j % (strlen($key))) +1 );
        }
    }

    $res = base64_encode($res);

    return $res;
}

NOTE : This function has not been written by me, so if you guys figure out what the $j variable is used for let me know. I also (obviously) can not change this code.

So I've tried to translate that function in Java which gave this :

public String cryptMdp(String mdp){
    String res = "";
    String key = mdp;

    int j = 0;
    int tmp = 0;

    for (int i = 0; i < mdp.length(); i++) {
        char c = mdp.charAt(i);
        char c2 = key.charAt(i);

        tmp = (int) c + (int) c2;
        if (tmp > 255) {
            tmp = tmp - 256;
        }

        res += (char) tmp;

        if (j == key.length() - 1){
            j = 0;
        }
        else{
            j = (j % key.length()) + 1;
        }
    }
    return Base64.getMimeEncoder().encodeToString(res.getBytes());
}

Unfortunately, those do not return the same string (ex: For the string a, php returns wg== and Java returns w4I=.), even though according to this question this is supposed to be a standard.

Can you guys see what I'm doing wrong ?

3
  • Chk this link stackoverflow.com/questions/11002603/… Commented Jul 2, 2018 at 12:42
  • @Penguine Already saw it, but this guy solution was the use of hmac, which I'm not using in my case Commented Jul 2, 2018 at 12:43
  • Check charset in java and php. They can be different. And another thing - java use wchar in its string. Commented Jul 2, 2018 at 13:16

2 Answers 2

0

Use the existing Java functionality for Base64: https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html

You are probably looking for this method:

encode(byte[] src) Encodes all bytes from the specified byte array into a newly-allocated byte array using the Base64 encoding scheme.

Sign up to request clarification or add additional context in comments.

4 Comments

Sill not working :( I've tried Base64.getMimeEncoder().encode(), Base64.getMimeEncoder().encodeToString(), Base64.getEncoder().encode() and Base64.getEncoder().encodeToString() I'm getting the same result with all of them
Are you 150% sure that the inputs are the same?
Compare them directly and make sure there are no invisible characters anywhere.
You could also try debugging both methods to see at which point exactly the outputs start deviating. That should give you a hint where to find the problem.
0

I've been able to do what I want using URLConnection to send a HTTP request to my PHP page, like so :

public String cryptMdp(String mdp){
    try {
        URL url = new URL("http://myaddress/someproject/create_pwd.php?pwd=" + mdp);
        URLConnection conn = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        String inputline = "";

        while((inputline = in.readLine()) != null){
            return inputline;
        }
        in.close();
    } catch (MalformedURLException ex) {
        System.out.println("error URL");
    } catch (IOException ex) {
        System.out.println("error connection");
    }
    return null;
}

The problem was that the PHP was using unknown characters before encoding them, which means that all of the passwords are incorrectly crypted...

EDIT :

Actually the password is crypted differently in the IDE (Netbeans) and in the builded app (.jar), so Base64.getMimeEncoder().encodeToString(res.getBytes()); actually produce the same as the PHP method...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.