Home  >  Article  >  Backend Development  >  Convert java version MD5 to php version

Convert java version MD5 to php version

巴扎黑
巴扎黑Original
2016-11-09 13:19:071818browse

java code

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 code:

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);


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn