Equivalent of PHP's crypt function in Java

后端 未结 7 2208
青春惊慌失措
青春惊慌失措 2020-12-16 14:24

I am migrating my PHP code to Google App Engine - Java.
So I need an equivalent of PHP\'s crypt function in Java,
since I have stored all the passwords of registered

相关标签:
7条回答
  • 2020-12-16 14:44

    Well, PHP's crypt isn't actually encryption as far as I know. It's just a wrapper around some one-way hashing functions I believe, so if your current PHP site's using crypt's MD5 or SHA256 or whatever, I'd expect that you could find those equivalent hashing classes/functions in Java.

    0 讨论(0)
  • 2020-12-16 14:47

    It seems you have to work with a legacy database already populated with passwords you cannot discard, so you can't just switch to a salted MessageDigest, preferably using SHA-1. And your problem gets more complicated, since PHP's crypt is a wrapper that might use one of several algorithms. But let's assume your PHP uses the original DES-based UNIX crypt, then all you need is an implementation of that in Java. As far as i know, there is no implementation of UNIX's crypt in the standard Java installation, but you might want to look here for a list of options.

    0 讨论(0)
  • 2020-12-16 14:48

    I can recommend this: MD5Crypt implementation

    MD5Crypt.crypt("youPassword"); // output: $1$zSiw0koU$i3Srfmoxx4VPePJHWqAuK/
    

    This is one of the few implementations, which works for me.

    0 讨论(0)
  • 2020-12-16 14:53

    This is an old thread but I ran into the same issue and found a different solution. You can use the UnixCrypt/Md5Crypt classes in the Apache Commons Codec 1.7 library.

    For example you can call

    UnixCrypt.crypt(string, salt)
    

    OR

    Md5Crypt.md5Crypt(byte[], salt)
    

    I haven't looked into the other encryption types but I imagine their are other utilities as well.

    • org.apache.commons.codec.digest.UnixCrypt
    • org.apache.commons.codec.digest.Md5Crypt
    0 讨论(0)
  • 2020-12-16 14:54

    You need to take a look at the java.security classes (what used to tbe the JCE):

    In there you'll find everything you need to do what you want (depending on which algorithm you need).

    http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/security/package-summary.html

    e.g. MessageDigest for MD5/SHA etc:

    http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/security/MessageDigest.html

    Check these against the Google App Engine whitelist here, I'm not sure what's supported and what isn't.

    http://code.google.com/appengine/docs/java/jrewhitelist.html

    The java.security stuff can be a bit of a pain to work with sometimes, you may alternatively want to use Jasypt - which is a more simplified API that works with any JCE:

    http://www.jasypt.org/

    0 讨论(0)
  • 2020-12-16 15:01

    PHP's crypt supports multiple hash functions. If you use the MD5 version (hash starts with $1$), you can find a Java implementation here,

    http://www.java2s.com/Open-Source/Java-Document/Groupware/LibreSource/md5/MD5Crypt.java.htm

    Please notice that they use their own MD5 class. I am not sure if it's the same as standard MD5.

    I am sure you can find Java implementation for other hash algorithms too.

    0 讨论(0)
提交回复
热议问题