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 ?