简体   繁体   English

Java中PHP的crypt函数的等价物

[英]Equivalent of PHP's crypt function in Java

I am migrating my PHP code to Google App Engine - Java. 我正在将我的PHP代码迁移到Google App Engine - Java。
So I need an equivalent of PHP's crypt function in Java, 所以我需要Java中相当于PHP的crypt函数,
since I have stored all the passwords of registered users 因为我已经存储了注册用户的所有密码
using crypt in my DB. 在我的数据库中使用crypt。

Edit 1 : Here is my php code for encrypting passwords : 编辑1 :这是我加密密码的PHP代码:

$password = "test123"; $ password =“test123”;
$pwd = crypt($password,$password); $ pwd = crypt($ password,$ password);
echo $pwd; echo $ pwd;

Output is (On Windows as well as a linux based server on HostMonser ): 输出是(在Windows上以及HostMonser上的基于Linux的服务器):
temjCCsjBECmU temjCCsjBECmU

Can someone give me equivalted java code? 有人可以给我相同的java代码吗?
I have tried various permutations & combinations with 我尝试了各种排列和组合
MessageDigest class, but can't get it right.. MessageDigest类,但无法正确...

Edit 2 : 编辑2
Here is sample code which I thought would work but did not: 这是示例代码,我认为它可以工作,但没有:

try {
                {
                    String password = "test123";
                    MessageDigest digest = MessageDigest.getInstance( "MD5" ); 
                    byte[] passwordBytes = password.getBytes( ); 

                    digest.reset( );
                    digest.update( passwordBytes );
                    digest.update( passwordBytes );
                    byte[] message = digest.digest( );

                    StringBuffer hexString = new StringBuffer();
                    for ( int i=0; i < message.length; i++) 
                    {
                        hexString.append( Integer.toHexString(
                            0xFF & message[ i ] ) );
                    }
                    String encrypted = hexString.toString();
                    System.out.println(encrypted);
                  } } catch (NoSuchAlgorithmException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

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. 您可以在Apache Commons Codec 1.7库中使用UnixCrypt / Md5Crypt类。

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. 我没有考虑其他加密类型,但我想他们也是其他的实用程序。

You have to know what implementation of PHP crypt has been used (MD5? SHA256? SHA512?) because there are several, depending on your OS : http://php.net/manual/fr/function.crypt.php 您必须知道使用了什么PHP crypt实现(MD5?SHA256?SHA512?),因为有几种,具体取决于您的操作系统: http//php.net/manual/fr/function.crypt.php

The Java equivalent class is MessageDigest . Java等效类是MessageDigest When you create an instance of this class, 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[] encryptedPassword = md.digest("yourPassword".getBytes());

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. 您似乎必须使用已填充了无法丢弃的密码的旧数据库,因此您不能只使用SHA-1 切换到盐渍MessageDigest And your problem gets more complicated, since PHP's crypt is a wrapper that might use one of several algorithms . 而且你的问题变得更加复杂,因为PHP的crypt是一个可能使用多种算法之一的包装器。 But let's assume your PHP uses the original DES-based UNIX crypt, then all you need is an implementation of that in Java. 但是我们假设您的PHP使用原始的基于DES的UNIX crypt,那么您所需要的只是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. 据我所知,在标准Java安装中没有实现UNIX的crypt,但您可能希望在此处查看选项列表。

You need to take a look at the java.security classes (what used to tbe the JCE): 你需要看一下java.security类(曾经是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 http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/security/package-summary.html

eg MessageDigest for MD5/SHA etc: 例如,MessageDigest for MD5 / SHA等:

http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/security/MessageDigest.html 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. 在这里针对Google App Engine白名单进行检查,我不确定支持什么,不支持什么。

http://code.google.com/appengine/docs/java/jrewhitelist.html 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: java.security的东西有时可能会有点麻烦,你可能还想使用Jasypt - 这是一个更简化的API,适用于任何JCE:

http://www.jasypt.org/ http://www.jasypt.org/

PHP's crypt supports multiple hash functions. PHP的crypt支持多个哈希函数。 If you use the MD5 version (hash starts with $1$), you can find a Java implementation here, 如果你使用MD5版本(hash以$ 1 $开头),你可以在这里找到一个Java实现,

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

Please notice that they use their own MD5 class. 请注意他们使用自己的MD5课程。 I am not sure if it's the same as standard MD5. 我不确定它是否与标准MD5相同。

I am sure you can find Java implementation for other hash algorithms too. 我相信你也可以找到其他哈希算法的Java实现。

I can recommend this: MD5Crypt implementation 我可以推荐这个: MD5Crypt实现

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

This is one of the few implementations, which works for me. 这是为数不多的实现之一,对我有用。

Well, PHP's crypt isn't actually encryption as far as I know. 好吧,就我所知,PHP的crypt实际上并不是加密。 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. 它只是我相信的一些单向散列函数的包装器,所以如果你当前的PHP站点使用crypt的MD5或SHA256或其他什么,我希望你可以在Java中找到那些等效的散列类/函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM