I am migrating my PHP code to Google API Engine - Java.
I need PHP's crypt function equivalent to Java,
Since I have stored all the passwords of registered users - using crypt in my DB
1 Edit : My PHP code for encrypting the password is:
$ password = "test123";
$ pwd = crypt ($ password), $ password);
echo $ pwd;
On output ( Windows as well as on a Linux-based server at HostMonser ): temjCCsjBECmU
< P> Can anyone give me a composite Java code?I have tried various permutations;
The combination of MessageDigest class, but it can not be found properly ..
Edit 2 :
Here is sample code that I thought would work But no:
try {{string password = "test 123"; Message digest digest = message digest.gate instance ("MD5"); Byte [] passwordBytes = password.getBytes (); Digest.reset (); Digest.update (passwordBytes); Digest.update (passwordBytes); Byte [] message = digest.digest (); Stringbuffer hextrestring = new stringbuffer (); For (int i = 0; i & lt; message.length; i ++) {hexstring.append (integer.ostostrying (0xff and message [i])); } String encrypted = hex string.stosting (); Println (encrypted); }} Hold (NoSuchAlgorithmException e1) {// TODO Auto-Generated Cache Block e1.printStackTrace (); }
You need to know how PHP crypt implementation has been implemented (MD5 ? SHA256? SHA512?) Because there are many based on your OS:
Java is the equivalent class when you create an example of this class, then you provide the hash algorithm, for example:
MessageDigest md = MessageDigest.getInstance ("MD5"); MessageDigest md2 = MessageDigest.getInstance ("SHA-256"); MessageDigest md3 = MessageDigest.getInstance ("SHA-512"); // etc byte [] encrypted password = md.digest ("yourPassword" .getBytes ());
Comments
Post a Comment