那位大神能把我这个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();}
3 回答
慕容森
TA贡献1853条经验 获得超18个赞
function cryptMd5($source, $key) { if(! mb_check_encoding($source, 'utf-8')) $source = mb_convert_encoding($source, "utf-8", "auto"); if(! mb_check_encoding($key, 'utf-8')) $key = mb_convert_encoding($key, "utf-8", "auto"); $k_ipad = str_pad($key, 64, chr(54)); $k_opad = str_pad($key, 64, chr(92)); for($i=0; $i<strlen($key); $i++) { $k_ipad{$i} = $key{$i} ^ chr(0x36); $k_opad{$i} = $key{$i} ^ chr(0x5c); } $dg = md5($source . substr($k_ipad, strlen($source)), true); $dg = md5(substr($dg, 0, 16) . substr($k_opad, 16), true); return bin2hex($dg); }
开心每一天1111
TA贡献1836条经验 获得超13个赞
我想说你的代码里好像有点问题。所以对理解你的代码有点困难。有些可以大体猜到你的目的。但是下边这两句就很难猜了:
Arrays.fill(k_ipad, keyb.length, 64, 54);
Arrays.fill(k_opad, keyb.length, 64, 92);
最后一个参数应该是byte变量吧。
慕后森
TA贡献1802条经验 获得超5个赞
public function md5Encrypt($encryptString='', $encryptKey='') { $keyByte = $this->getBytes($encryptKey); $keyIpad = array(); for ($i = 0; $i < 64; $i++) { $keyIpad[] = (isset($keyByte[$i]) ? $keyByte[$i] : 0) ^ 54; } $keyOpad = array(); for ($i = 0; $i < 64; $i++) { $keyOpad[] = (isset($keyByte[$i]) ? $keyByte[$i] : 0) ^ 92; } $string = md5($this->byteToString($keyIpad) . $encryptString); $string = substr(hex2bin($string), 0, 16); $signature = md5($this->byteToString($keyOpad). ($string)); return $signature; } private function getBytes($string) { $bytes = array(); for ($i = 0; $i < strlen($string); $i++) { $bytes[] = ord($string[$i]); } return $bytes; } private function byteToString($args = array()){ $str = ''; for ($i = 0; $i < count($args); $i++) { $str .= chr($args[$i]); } return $str; }
添加回答
举报
0/150
提交
取消