From: <php...@li...> - 2007-04-26 17:12:01
|
Hi folks ? This may be out of scope of the PJB project, but I?m having some =20 trouble translating Java into PHP and this is because I?m rather new =20 at Java (it?s been a trial, to say the least!). I?m trying to figure out why a string encoding function in Java would =20 return a different value as a seemingly logical reproduction in PHP. =20 It doesn?t make sense to me so I?m trying to see what the Java =20 function will return via PHP first, but I?m having issues. If this =20 isn?t too off-topic, please keep reading? 1: a java application takes the string ?admin? and turns it into a =20 hash via md5 and then encodes it via base64 ? and comes up with this: ISMvKXpXpadDiUoOSoAfww=3D=3D 2: instructions from the developers of this application write ?There =20 are 2 parts to the password, one is the hash, the second is you need =20 to Base64 encode that hash.? 3: I ran the string ?admin? through md5() and then ran that through =20 base64_encode() functions in php. So base64_encode(md5(?admin)), =20 effectively. I receive this value: =20 MjEyMzJmMjk3YTU3YTVhNzQzODk0YTBlNGE4MDFmYzM=3D 4: The different return values are confusing me. 5: I have grabbed code provided as an example with their application =20 and it reads like this: import java.io.*; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import javax.mail.MessagingException; import javax.mail.internet.MimeUtility; ? other stuff snipped private static String encodePassword(String password) { MessageDigest algorithm; try { algorithm =3D MessageDigest.getInstance(ENCRYPTION_TYPE); algorithm.reset(); algorithm.update(password.getBytes()); byte[] encrypted =3D algorithm.digest(); ByteArrayOutputStream out =3D new ByteArrayOutputStream(); OutputStream encoder =3D MimeUtility.encode(out, ENCODING_TYPE)= ; encoder.write(encrypted); encoder.flush(); return new String(out.toByteArray()); } catch (NoSuchAlgorithmException e) { return "Bad Encryption"; } catch (MessagingException e) { return "Bad Encryption"; } catch (IOException e) { return "Bad Encryption"; } } I translated this as: $password =3D "admin"; print "encoded: ". encodePassword($password)."<br>"; function encodePassword($password) { $message =3D new Java('java.security.MessageDigest'); $str =3D new Java('java.lang.String'); try { $algorithm =3D $message->getInstance('md5'); $reset =3D $algorithm->reset(); $update =3D $algorithm->update($str->getBytes($password)); //$update =3D $algorithm->update($password); $encrypted =3D $algorithm->digest(); $mu =3D new java('javax.mail.internet.MimeUtility'); $out =3D new Java('java.io.ByteArrayOutputStream'); $encoder =3D $mu->encode($out, 'base64'); $encoder->write($encrypted); $encoder->flush(); return $out->toByteArray(); } catch (Exception $e) { print_r($e); // for debugging purposes return "<br>Bad Encryption"; } } And I get this in response: JavaException Object ( [message:protected] =3D> [string:private] =3D> =20 [code:protected] =3D> 0 [file:protected] =3D> =20 /usr/java/tomcat-5.5/webapps/JavaBridge/test_class.php =20 [line:protected] =3D> 25 [trace:private] =3D> Array ( [0] =3D> Array ( =20 [file] =3D> /usr/java/tomcat-5.5/webapps/JavaBridge/test_class.php =20 [line] =3D> 25 [function] =3D> __call [class] =3D> Java [type] =3D> -> [args= ] =20 =3D> Array ( [0] =3D> getBytes [1] =3D> Array ( [0] =3D> admin ) ) ) [1] =3D= > =20 Array ( [file] =3D> =20 /usr/java/tomcat-5.5/webapps/JavaBridge/test_class.php [line] =3D> 25 =20 [function] =3D> getBytes [class] =3D> Java [type] =3D> -> [args] =3D> Array = ( =20 [0] =3D> admin ) ) [2] =3D> Array ( [file] =3D> =20 /usr/java/tomcat-5.5/webapps/JavaBridge/test_class.php [line] =3D> 17 =20 [function] =3D> encodePassword [args] =3D> Array ( [0] =3D> admin ) ) ) ) = =20 encoded: Bad Encryption What I think this is telling me is that on line 25 (which is this: =20 $update =3D $algorithm->update($str->getBytes($password));) something is =20 amiss. Line 17 is the print out the encoded password line (print =20 "encoded: ". encodePassword($password)."<br>";), which is, obviously, =20 having issues. *Any* help as I?m trying to learn how to translate Java into PHP would =20 be smashingly appreciated. And please advise if this is totally out =20 of bounds for this forum. TIA, Kate ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program. |