网上看到java实现php算法,该怎么实现?
$passwd = “haka”;
$str_md5 = md5($passwd,true);
echo $str_md5;
echo ” <br>+— <br>”;
print_r($str_md5);
echo ” <br>+— <br>”;
$str_md5_default = md5($passwd);
echo $str_md5_default;
echo ” <br>+— <br>”;
$str_pack = pack(“H*”,$str_md5_default);
print_r($str_pack);
echo ” <br>+— <br>”;
$str_base64 = base64_encode ($str_md5);
echo ” <br>+— <br>”;
echo $str_base64;
echo ” <br>+— <br>”;
echo base64_decode(“e01ENX1xVkZjVVM0a1B4Tk1pdzFIRUVBWG13PT0=”);运行结果:
©Q\Q.$?L‹ G@›
+—
©Q\Q.$?L‹ G@›
+—
a9515c512e243f134c8b0d471040179b
+—
©Q\Q.$?L‹ G@›
+—+—
qVFcUS4kPxNMiw1HEEAXmw==
+—
{MD5}qVFcUS4kPxNMiw1HEEAXmw==
+—
{MD5}g+Spau2WQ2xiG5gJ4lizCQ==算法:将密码经过 MD5 运算,得到 32 字节的字符串,然后每2个字节压缩成一个十六进制字符,这样得到16字节的字符串,最后经过Base64编码。按照算法写出java代码

解决方案 »

  1.   


    public static String createEncryptPSW(String psw) throws Exception {
    MessageDigest messagedigest = null;
    try {
    messagedigest = MessageDigest.getInstance("MD5");
    messagedigest.update(psw.getBytes("UTF8"));
    byte abyte0[] = messagedigest.digest();
    return (new BASE64Encoder()).encode(abyte0);
    } catch (NoSuchAlgorithmException e) {
    throw new Exception("MD5加密异常!", e);
    } catch (UnsupportedEncodingException e) {
    throw new Exception("MD5加密异常!", e);
    }
    }
      

  2.   


    谢谢,按照上面已经可以实现,但有个问题是已经知道MD5加密后的密文怎么得到结果,因为现在是从原有系统数据库中转到ldap服务器
    如上,知道a9515c512e243f134c8b0d471040179b
    怎么得出结果?
      

  3.   

    实现了package test;import sun.misc.BASE64Encoder;public class TestLDAPMD5 { public static void main(String args[]) { String md5Str = "e10adc3949ba59abbe56e057f20f883e";
    String ldapStr = (new BASE64Encoder()).encode(hexStringToBytes(md5Str));
    System.out.println(ldapStr);

    } public static byte[] hexStringToBytes(String hexString) {
    if (hexString == null || hexString.equals("")) {
    return null;
    }
    hexString = hexString.toUpperCase();
    int length = hexString.length() / 2;
    char[] hexChars = hexString.toCharArray();
    byte[] d = new byte[length];
    for (int i = 0; i < length; i++) {
    int pos = i * 2;
    d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
    }
    return d;
    } public static byte charToByte(char c) {
    return (byte) "0123456789ABCDEF".indexOf(c);
    }}