phpjavamd5
那位大神能把我這個Java的md5的加密方法寫個php版本哦,感激噢,Java代碼如下!
寫這個的人 描述的思路如下
1.將秘鑰、源串分別轉換byte數組
2.聲明2個64位元組 將key的byte數組分別做異或運算填充進去 並分別補充 54、92 補滿64長度
3.獲得md5摘要演算法的MessageDigest 對象
4.使用其中一個數組及源串的數組更新MessageDigest 摘要 完成雜湊計算
5.重設摘要
6.使用另一個數組更新摘要 使用4中結果 從0到16開始更新摘要 完成雜湊計算
7.轉換字串
public String cryptMd5(String source, String key) {
byte[] k_ipad = new byte[64];
byte[] k_opad = new byte[64];
byte[] keyb;
byte[] value;
try { byte[] keyb = key.getBytes("UTF-8");
value = source.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e)
{
byte[] value;
keyb = key.getBytes();
value = source.getBytes();
}
Arrays.fill(k_ipad, keyb.length, 64, 54);
Arrays.fill(k_opad, keyb.length, 64, 92);
for (int i = 0; i < keyb.length; i++)
{
k_ipad[i] = (byte)(keyb[i] ^ 0x36);
k_opad[i] = (byte)(keyb[i] ^ 0x5C);
}
MessageDigest md = null;
try
{
md = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
return null;
}
md.update(k_ipad);
md.update(value);
byte[] dg = md.digest();
md.reset();
md.update(k_opad);
md.update(dg, 0, 16);
dg = md.digest();
return toHex(dg); }
public static String toHex(byte[] input)
{
if (input == null) {
return null;
}
StringBuffer output = new StringBuffer(input.length * 2);
for (int i = 0; i < input.length; i++)
{
int current = input[i] & 0xFF;
if (current < 16)
output.append("0");
output.append(Integer.toString(current, 16));
}
return output.toString();
}