首頁 >後端開發 >php教程 >java版MD5轉換成php版

java版MD5轉換成php版

巴扎黑
巴扎黑原創
2016-11-09 13:19:071844瀏覽

java程式碼

public static String encryptMD5_Salt(String content) {
    String resultString = "";
    String appkey = "acdf,kef";
    byte[] a = appkey.getBytes();
    byte[] datSource = content.getBytes();
    byte[] b = new byte[a.length + 4 + datSource.length];
    int i;
    for (i = 0; i < datSource.length; i++) {
        b[i] = datSource[i];
    }
    b[i++] = (byte) 143;
    b[i++] = (byte) 112;
    b[i++] = (byte) 131;
    b[i++] = (byte) 143;
    for (int k = 0; k < a.length; k++) {
        b[i] = a[k];
        i++;
    }
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(b);
        resultString = new HexBinaryAdapter().marshal(md5.digest());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resultString.toLowerCase();
}

php程式碼:

function javaMd5($data) {
   assert(is_array($data));
   $dataString = byteArrayToString($data);
   $hashString = md5($dataString);
   return $hashString;
}
function byteArrayToString($b) {
   assert(is_array($b));
   $asciiString = &#39;&#39;;
   for ($i = 0; $i < count($b); $i++) {
      $asciiString .= chr($b[$i]);
   }
   return $asciiString;
}
$re=array_merge
   (unpack("c*", pack("a*", $content)),
   pack("c", pack("l", 143)),
   unpack("c", pack("l", 112)),
   unpack("c", pack("l", 131)),
   unpack("c", pack("l", 143)),
   unpack("c*", pack("a*", "acdf,kef"))
   );
$re = javaMd5($re);


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn