Java MD5 vs JavaScript MD5
843810Nov 2 2004 — edited Nov 19 2007Hi All,
I am trying to encrypt a SSN on the client side with Javascript and store it in a cookie.
Next, I am trying to read this cookie value in my Java Code and compare it with the SSN from the session that I already have.
I am using http://pajhome.org.uk/crypt/md5/md5src.html JS to encrypt the SSN on the client side and JDK1.4's MessageDigest on the server side.
For some inputs, THERE IS A ZERO MISSING IN THE JAVA ENCRYPTED STRING and hence the comparisions are failing.
What am I doing wrong...
Any help would be greatly appreciated.
JavaScript Code:
<script language="JavScript" src="/md5.js" //This is the downloaded file from the URL specified above
var val = hex_md5("173247267");
encryptedValue: 99e498d5d560024c4aaae48ae32bed28
-------------------------
JavaCode:
From the code below, I get :
encryptedValue: 99e498d5d56024c4aaae48ae32bed28
public String getEncVal("173247267")
{
if(userSSN == null || "".equals(userSSN))
return "";
byte buf[] = userSSN.getBytes();
StringBuffer hexString = new StringBuffer();
try
{
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(buf);
byte[] digest = algorithm.digest();
for(int i=0; i< digest.length; i++)
{
hexString.append(Integer.toHexString(0xFF & digest));
}
}
catch(Exception ex)
{
ex.printStackTrace();
return "";
}
return hexString.toString();
}
--
Regards,
Venkat.