Why encoding a string in php and in java is different from each other? -
php
function encrypt($string) { $key_para = "aibl_#0#0!0!@"; $result = ''; $test = ""; for($i=0; $i<strlen($string); $i++) { $char = substr($string, $i, 1); $a = ($i % strlen($key_para))-1; $keychar = substr($key_para, $a, 1); $inte = ord($char)+ord($keychar); echo $i.'='; echo $inte; echo '</br>'; $char = chr($inte); $result.=$char; } echo '</br>'; echo $result; $base64t = base64_encode($result); $res = urlencode($base64t); $f_data = str_replace("%", "_", $res); return $f_data; }
java
public static string encrypt(string str) throws unsupportedencodingexception { string key_para= "aibl_#0#0!0!@"; string result =""; for(int i=0; < str.length(); i++) { char ch = str.charat(i); int = (i % key_para.length()) -1 ; char keychar ; if(a < 0) { keychar = key_para.charat(key_para.length()+a); } else if(a > 0) { keychar = key_para.charat(a); } else { keychar = key_para.charat(0); } int temp = ((int) ch + (int) keychar); system.out.println(i+"="+temp); ch = (char) temp; system.out.println(i+"="+ch); result += ch; } system.out.println(result); string enc=java.util.base64.getencoder().encodetostring(result.getbytes()); string res = urlencoder.encode(enc, "utf-8"); string f_data = res.replace('%','_') ; return f_data; }
string encrypted:
"mobileno=01911222333&accountno=0021120125225&pin=1234"
output should be:
"rbcrq7jekz9gyfjpunfze3r_2fklzwhjoen5autbexiy9tylrhu2bscnz7digfk5mrbvjivhq_3d"
java output:
"rbcrq7jepz9gyfjpunfze3r_2fp1zwpz8_2fpz_2butbexpz9tylrhu2bscnz7dd8_2fpz8_2fbvjivhq_3d"
the logic in both languages same output different.
doing wrong?
Comments
Post a Comment