echo md5(chr(142));
java代码:
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class utils {
public static void main(String[] args) {
char ss=(char)142;//这里换成56后md5后和php版的 md5后的结果一样
System.out.println(md5(ss+""));
}
public static String md5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
经过测试 获取142的char类型的md5后数值不一样,获取56 的char 后md5的值 一样,这是怎么回事,如何解决(都是utf-8编码)
8 个解决方案
#1
plainText.getBytes(
"GBK");
#2
这个方法不行,试过了
#3
不知道为什么要
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
而且终值还始终在变
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
而且终值还始终在变
#4
public static String getMd5(byte[] buffer) throws NoSuchAlgorithmException{
String s = null;
char hexDigist[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(buffer);
byte[] datas = md.digest(); //16个字节的长整数
char[] str = new char[2*16];
int k = 0;
for(int i=0;i<16;i++){
byte b = datas[i];
str[k++] = hexDigist[b>>>4 & 0xf];//高4位
str[k++] = hexDigist[b & 0xf];//低4位
}
s = new String(str);
return s;
}
java中的MD5返回的是一个128位的长整形数,即16个字节,一个字节映射成2个字符,所以就是32个字符,md5 不可能不一样的都是遵循md5协议实现的,只是PHP在底层用C语言实现了
这里测试下:
java:
public static void main(String[] args) {
try {
System.out.println(getMd5("123".getBytes()));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
输出:
202cb962ac59075b964b07152d234b70
PHP代码:
echo md5("123");
输出:
202cb962ac59075b964b07152d234b70
#5
还是不行,我把php和java代码全部贴出来吧,php结果是正确的,java代码如何改呢
<?php
//php为正确的 结果:CA15B8C6D72A4442942045956DD371F8
$password="123456";
$pt='\x00\x00\x00\x00\x16\x9d\x56\x75';
$vc='!PRY';
$passwd = jspassword($password,$pt, $vc);
echo "<br>结果:".$pt."<br>".$vc."<br>密码:".$passwd."<br><br>";
function jspassword($p,$pt,$vc,$md5 = true)
{
echo $p.":".$pt.":".$vc;
if($md5)
{
$p = strtoupper(md5($p));
}
//echo "<br>".$p;exit;
$len = strlen($p);
$temp = null;
//echo "<br>md5Password:".$p."<br>";
for ($i=0; $i < $len ; $i = $i + 2)
{
//echo "<br>i:".$i;
$temp .= '\x'.substr($p, $i,2);
}
//echo "<br>".$temp."<br>";
//echo "<br><br><br>--->>>".md5(hex2asc($temp).hex2asc($pt));
//$str=hex2asc($temp).hex2asc($pt);
//echo "<br>内部:".$str."-->".md5(hex2asc($temp))."-->".md5(hex2asc($pt));
return strtoupper(md5(strtoupper(md5(hex2asc($temp).hex2asc($pt))).$vc));
}
/**
* 十六进制转字符
*
* @access private
* @param string $str
* @return string
*/
function hex2asc($str)
{
//echo "处理前:".$str."<br>";
//print_r( explode('\x', $str));
$str = join('', explode('\x', $str));
//echo "<br>处理后:".$str;
$len = strlen($str);
$data = null;
for ($i=0;$i<$len;$i+=2)
{
//echo "<br>::".substr($str,$i,2);
echo "<br>".hexdec(substr($str,$i,2)).":::".chr(hexdec(substr($str,$i,2)))."->".md5(chr(hexdec(substr($str,$i,2))));
$data.=chr(hexdec(substr($str,$i,2)));
}
echo "<br>".md5($data)."<br>";
return $data;
}
?>
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class utils {
public static void main(String[] args) {
utils u=new utils();
String up=u.jspassword("123456", "\\x00\\x00\\x00\\x00\\x16\\x9d\\x56\\x75", "!PRY", true);
System.out.println("-----------------------------");
System.out.println(up);
}
public String jspassword(String password,String pt,String vc,boolean md5){
if(md5)
{
password = utils.md5(password).toUpperCase();
}
int len =password.length();
String temp="";
for (int i=0; i < len ; i = i + 2)
{
temp += "\\x"+password.substring(i, i+2);
}
return (utils.md5(utils.md5(utils.hex2asc(temp)+utils.hex2asc(pt)) .toUpperCase()+vc) ).toUpperCase();
}
public static String hex2asc(String str){
String [] s=str.trim().split("\\\\x");
//System.out.println(s.length);
StringBuffer sb=new StringBuffer();
for(String sItem:s){
//System.out.println(sItem);
sb.append(sItem);
}
int len = sb.toString().length();
//String data = null;
StringBuffer sb1=new StringBuffer();
for (int i=0;i<len;i+=2)
{
int x=Integer.parseInt(sb.toString().substring(i, i+2),16);
char ss=(char) x;
System.out.println(x+"::"+ss+"转换后->"+utils.md5(String.valueOf(ss)));
sb1.append(String.valueOf(ss));
}
return sb1.toString();
}
public static String md5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
#6
我把php和java代码全部贴出来吧
<?php
//php为正确的 结果:CA15B8C6D72A4442942045956DD371F8
$password="123456";
$pt='\x00\x00\x00\x00\x16\x9d\x56\x75';
$vc='!PRY';
$passwd = jspassword($password,$pt, $vc);
echo "<br>结果:".$pt."<br>".$vc."<br>密码:".$passwd."<br><br>";
function jspassword($p,$pt,$vc,$md5 = true)
{
echo $p.":".$pt.":".$vc;
if($md5)
{
$p = strtoupper(md5($p));
}
//echo "<br>".$p;exit;
$len = strlen($p);
$temp = null;
//echo "<br>md5Password:".$p."<br>";
for ($i=0; $i < $len ; $i = $i + 2)
{
//echo "<br>i:".$i;
$temp .= '\x'.substr($p, $i,2);
}
//echo "<br>".$temp."<br>";
//echo "<br><br><br>--->>>".md5(hex2asc($temp).hex2asc($pt));
//$str=hex2asc($temp).hex2asc($pt);
//echo "<br>内部:".$str."-->".md5(hex2asc($temp))."-->".md5(hex2asc($pt));
return strtoupper(md5(strtoupper(md5(hex2asc($temp).hex2asc($pt))).$vc));
}
/**
* 十六进制转字符
*
* @access private
* @param string $str
* @return string
*/
function hex2asc($str)
{
//echo "处理前:".$str."<br>";
//print_r( explode('\x', $str));
$str = join('', explode('\x', $str));
//echo "<br>处理后:".$str;
$len = strlen($str);
$data = null;
for ($i=0;$i<$len;$i+=2)
{
//echo "<br>::".substr($str,$i,2);
echo "<br>".hexdec(substr($str,$i,2)).":::".chr(hexdec(substr($str,$i,2)))."->".md5(chr(hexdec(substr($str,$i,2))));
$data.=chr(hexdec(substr($str,$i,2)));
}
echo "<br>".md5($data)."<br>";
return $data;
}
?>
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class utils {
public static void main(String[] args) {
utils u=new utils();
String up=u.jspassword("123456", "\\x00\\x00\\x00\\x00\\x16\\x9d\\x56\\x75", "!PRY", true);
System.out.println("-----------------------------");
System.out.println(up);
}
public String jspassword(String password,String pt,String vc,boolean md5){
if(md5)
{
password = utils.md5(password).toUpperCase();
}
int len =password.length();
String temp="";
for (int i=0; i < len ; i = i + 2)
{
temp += "\\x"+password.substring(i, i+2);
}
return (utils.md5(utils.md5(utils.hex2asc(temp)+utils.hex2asc(pt)) .toUpperCase()+vc) ).toUpperCase();
}
public static String hex2asc(String str){
String [] s=str.trim().split("\\\\x");
//System.out.println(s.length);
StringBuffer sb=new StringBuffer();
for(String sItem:s){
//System.out.println(sItem);
sb.append(sItem);
}
int len = sb.toString().length();
//String data = null;
StringBuffer sb1=new StringBuffer();
for (int i=0;i<len;i+=2)
{
int x=Integer.parseInt(sb.toString().substring(i, i+2),16);
char ss=(char) x;
System.out.println(x+"::"+ss+"转换后->"+utils.md5(String.valueOf(ss)));
sb1.append(String.valueOf(ss));
}
return sb1.toString();
}
public static String md5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
#7
function jspassword($p,$pt,$vc,$md5 = true)
{
echo $p.":".$pt.":".$vc;
if($md5)
{
$p = strtoupper(md5($p));
}
//echo "<br>".$p;exit;
$len = strlen($p);
$temp = null;
//echo "<br>md5Password:".$p."<br>";
for ($i=0; $i < $len ; $i = $i + 2)
{
//echo "<br>i:".$i;
$temp .= '\x'.substr($p, $i,2);
}
//echo "<br>".$temp."<br>";
//echo "<br><br><br>--->>>".md5(hex2asc($temp).hex2asc($pt));
//$str=hex2asc($temp).hex2asc($pt);
//echo "<br>内部:".$str."-->".md5(hex2asc($temp))."-->".md5(hex2asc($pt));
return strtoupper(md5(strtoupper(md5(hex2asc($temp).hex2asc($pt))).$vc));
}
/**
* 十六进制转字符
*
* @access private
* @param string $str
* @return string
*/
function hex2asc($str)
{
//echo "处理前:".$str."<br>";
//print_r( explode('\x', $str));
$str = join('', explode('\x', $str));
//echo "<br>处理后:".$str;
$len = strlen($str);
$data = null;
for ($i=0;$i<$len;$i+=2)
{
//echo "<br>::".substr($str,$i,2);
echo "<br>".hexdec(substr($str,$i,2)).":::".chr(hexdec(substr($str,$i,2)))."->".md5(chr(hexdec(substr($str,$i,2))));
$data.=chr(hexdec(substr($str,$i,2)));
}
echo "<br>".md5($data)."<br>";
return $data;
}
?>
[/code]
java在测试的时候传入的byte数组哦,要调用string.getBytes()方法 ,这个我测试是可以通过的 我也是这两天才开始学习java的 如果是编码的问题我就不知道怎么解决了
{
echo $p.":".$pt.":".$vc;
if($md5)
{
$p = strtoupper(md5($p));
}
//echo "<br>".$p;exit;
$len = strlen($p);
$temp = null;
//echo "<br>md5Password:".$p."<br>";
for ($i=0; $i < $len ; $i = $i + 2)
{
//echo "<br>i:".$i;
$temp .= '\x'.substr($p, $i,2);
}
//echo "<br>".$temp."<br>";
//echo "<br><br><br>--->>>".md5(hex2asc($temp).hex2asc($pt));
//$str=hex2asc($temp).hex2asc($pt);
//echo "<br>内部:".$str."-->".md5(hex2asc($temp))."-->".md5(hex2asc($pt));
return strtoupper(md5(strtoupper(md5(hex2asc($temp).hex2asc($pt))).$vc));
}
/**
* 十六进制转字符
*
* @access private
* @param string $str
* @return string
*/
function hex2asc($str)
{
//echo "处理前:".$str."<br>";
//print_r( explode('\x', $str));
$str = join('', explode('\x', $str));
//echo "<br>处理后:".$str;
$len = strlen($str);
$data = null;
for ($i=0;$i<$len;$i+=2)
{
//echo "<br>::".substr($str,$i,2);
echo "<br>".hexdec(substr($str,$i,2)).":::".chr(hexdec(substr($str,$i,2)))."->".md5(chr(hexdec(substr($str,$i,2))));
$data.=chr(hexdec(substr($str,$i,2)));
}
echo "<br>".md5($data)."<br>";
return $data;
}
?>
[/code]
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class utils {
public static void main(String[] args) {
utils u=new utils();
String up=u.jspassword("123456", "\\x00\\x00\\x00\\x00\\x16\\x9d\\x56\\x75", "!PRY", true);
System.out.println("-----------------------------");
System.out.println(up);
}
public String jspassword(String password,String pt,String vc,boolean md5){
if(md5)
{
password = utils.md5(password).toUpperCase();
}
int len =password.length();
String temp="";
for (int i=0; i < len ; i = i + 2)
{
temp += "\\x"+password.substring(i, i+2);
}
return (utils.md5(utils.md5(utils.hex2asc(temp)+utils.hex2asc(pt)) .toUpperCase()+vc) ).toUpperCase();
}
public static String hex2asc(String str){
String [] s=str.trim().split("\\\\x");
//System.out.println(s.length);
StringBuffer sb=new StringBuffer();
for(String sItem:s){
//System.out.println(sItem);
sb.append(sItem);
}
int len = sb.toString().length();
//String data = null;
StringBuffer sb1=new StringBuffer();
for (int i=0;i<len;i+=2)
{
int x=Integer.parseInt(sb.toString().substring(i, i+2),16);
char ss=(char) x;
System.out.println(x+"::"+ss+"转换后->"+utils.md5(String.valueOf(ss)));
sb1.append(String.valueOf(ss));
}
return sb1.toString();
}
public static String md5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
java在测试的时候传入的byte数组哦,要调用string.getBytes()方法 ,这个我测试是可以通过的 我也是这两天才开始学习java的 如果是编码的问题我就不知道怎么解决了
#8
编码不对 换下试试
#1
plainText.getBytes(
"GBK");
#2
plainText.getBytes( "GBK");
这个方法不行,试过了
#3
不知道为什么要
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
而且终值还始终在变
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
而且终值还始终在变
#4
public static String getMd5(byte[] buffer) throws NoSuchAlgorithmException{
String s = null;
char hexDigist[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(buffer);
byte[] datas = md.digest(); //16个字节的长整数
char[] str = new char[2*16];
int k = 0;
for(int i=0;i<16;i++){
byte b = datas[i];
str[k++] = hexDigist[b>>>4 & 0xf];//高4位
str[k++] = hexDigist[b & 0xf];//低4位
}
s = new String(str);
return s;
}
java中的MD5返回的是一个128位的长整形数,即16个字节,一个字节映射成2个字符,所以就是32个字符,md5 不可能不一样的都是遵循md5协议实现的,只是PHP在底层用C语言实现了
这里测试下:
java:
public static void main(String[] args) {
try {
System.out.println(getMd5("123".getBytes()));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
输出:
202cb962ac59075b964b07152d234b70
PHP代码:
echo md5("123");
输出:
202cb962ac59075b964b07152d234b70
#5
public static String getMd5(byte[] buffer) throws NoSuchAlgorithmException{
String s = null;
char hexDigist[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(buffer);
byte[] datas = md.digest(); //16个字节的长整数
char[] str = new char[2*16];
int k = 0;
for(int i=0;i<16;i++){
byte b = datas[i];
str[k++] = hexDigist[b>>>4 & 0xf];//高4位
str[k++] = hexDigist[b & 0xf];//低4位
}
s = new String(str);
return s;
}
java中的MD5返回的是一个128位的长整形数,即16个字节,一个字节映射成2个字符,所以就是32个字符,md5 不可能不一样的都是遵循md5协议实现的,只是PHP在底层用C语言实现了
这里测试下:
java:public static void main(String[] args) {
try {
System.out.println(getMd5("123".getBytes()));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
输出:
202cb962ac59075b964b07152d234b70
PHP代码:echo md5("123");
输出:
202cb962ac59075b964b07152d234b70
还是不行,我把php和java代码全部贴出来吧,php结果是正确的,java代码如何改呢
<?php
//php为正确的 结果:CA15B8C6D72A4442942045956DD371F8
$password="123456";
$pt='\x00\x00\x00\x00\x16\x9d\x56\x75';
$vc='!PRY';
$passwd = jspassword($password,$pt, $vc);
echo "<br>结果:".$pt."<br>".$vc."<br>密码:".$passwd."<br><br>";
function jspassword($p,$pt,$vc,$md5 = true)
{
echo $p.":".$pt.":".$vc;
if($md5)
{
$p = strtoupper(md5($p));
}
//echo "<br>".$p;exit;
$len = strlen($p);
$temp = null;
//echo "<br>md5Password:".$p."<br>";
for ($i=0; $i < $len ; $i = $i + 2)
{
//echo "<br>i:".$i;
$temp .= '\x'.substr($p, $i,2);
}
//echo "<br>".$temp."<br>";
//echo "<br><br><br>--->>>".md5(hex2asc($temp).hex2asc($pt));
//$str=hex2asc($temp).hex2asc($pt);
//echo "<br>内部:".$str."-->".md5(hex2asc($temp))."-->".md5(hex2asc($pt));
return strtoupper(md5(strtoupper(md5(hex2asc($temp).hex2asc($pt))).$vc));
}
/**
* 十六进制转字符
*
* @access private
* @param string $str
* @return string
*/
function hex2asc($str)
{
//echo "处理前:".$str."<br>";
//print_r( explode('\x', $str));
$str = join('', explode('\x', $str));
//echo "<br>处理后:".$str;
$len = strlen($str);
$data = null;
for ($i=0;$i<$len;$i+=2)
{
//echo "<br>::".substr($str,$i,2);
echo "<br>".hexdec(substr($str,$i,2)).":::".chr(hexdec(substr($str,$i,2)))."->".md5(chr(hexdec(substr($str,$i,2))));
$data.=chr(hexdec(substr($str,$i,2)));
}
echo "<br>".md5($data)."<br>";
return $data;
}
?>
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class utils {
public static void main(String[] args) {
utils u=new utils();
String up=u.jspassword("123456", "\\x00\\x00\\x00\\x00\\x16\\x9d\\x56\\x75", "!PRY", true);
System.out.println("-----------------------------");
System.out.println(up);
}
public String jspassword(String password,String pt,String vc,boolean md5){
if(md5)
{
password = utils.md5(password).toUpperCase();
}
int len =password.length();
String temp="";
for (int i=0; i < len ; i = i + 2)
{
temp += "\\x"+password.substring(i, i+2);
}
return (utils.md5(utils.md5(utils.hex2asc(temp)+utils.hex2asc(pt)) .toUpperCase()+vc) ).toUpperCase();
}
public static String hex2asc(String str){
String [] s=str.trim().split("\\\\x");
//System.out.println(s.length);
StringBuffer sb=new StringBuffer();
for(String sItem:s){
//System.out.println(sItem);
sb.append(sItem);
}
int len = sb.toString().length();
//String data = null;
StringBuffer sb1=new StringBuffer();
for (int i=0;i<len;i+=2)
{
int x=Integer.parseInt(sb.toString().substring(i, i+2),16);
char ss=(char) x;
System.out.println(x+"::"+ss+"转换后->"+utils.md5(String.valueOf(ss)));
sb1.append(String.valueOf(ss));
}
return sb1.toString();
}
public static String md5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
#6
不知道为什么要
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
而且终值还始终在变
public static String getMd5(byte[] buffer) throws NoSuchAlgorithmException{
String s = null;
char hexDigist[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(buffer);
byte[] datas = md.digest(); //16个字节的长整数
char[] str = new char[2*16];
int k = 0;
for(int i=0;i<16;i++){
byte b = datas[i];
str[k++] = hexDigist[b>>>4 & 0xf];//高4位
str[k++] = hexDigist[b & 0xf];//低4位
}
s = new String(str);
return s;
}
java中的MD5返回的是一个128位的长整形数,即16个字节,一个字节映射成2个字符,所以就是32个字符,md5 不可能不一样的都是遵循md5协议实现的,只是PHP在底层用C语言实现了
这里测试下:
java:public static void main(String[] args) {
try {
System.out.println(getMd5("123".getBytes()));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
输出:
202cb962ac59075b964b07152d234b70
PHP代码:echo md5("123");
输出:
202cb962ac59075b964b07152d234b70
我把php和java代码全部贴出来吧
<?php
//php为正确的 结果:CA15B8C6D72A4442942045956DD371F8
$password="123456";
$pt='\x00\x00\x00\x00\x16\x9d\x56\x75';
$vc='!PRY';
$passwd = jspassword($password,$pt, $vc);
echo "<br>结果:".$pt."<br>".$vc."<br>密码:".$passwd."<br><br>";
function jspassword($p,$pt,$vc,$md5 = true)
{
echo $p.":".$pt.":".$vc;
if($md5)
{
$p = strtoupper(md5($p));
}
//echo "<br>".$p;exit;
$len = strlen($p);
$temp = null;
//echo "<br>md5Password:".$p."<br>";
for ($i=0; $i < $len ; $i = $i + 2)
{
//echo "<br>i:".$i;
$temp .= '\x'.substr($p, $i,2);
}
//echo "<br>".$temp."<br>";
//echo "<br><br><br>--->>>".md5(hex2asc($temp).hex2asc($pt));
//$str=hex2asc($temp).hex2asc($pt);
//echo "<br>内部:".$str."-->".md5(hex2asc($temp))."-->".md5(hex2asc($pt));
return strtoupper(md5(strtoupper(md5(hex2asc($temp).hex2asc($pt))).$vc));
}
/**
* 十六进制转字符
*
* @access private
* @param string $str
* @return string
*/
function hex2asc($str)
{
//echo "处理前:".$str."<br>";
//print_r( explode('\x', $str));
$str = join('', explode('\x', $str));
//echo "<br>处理后:".$str;
$len = strlen($str);
$data = null;
for ($i=0;$i<$len;$i+=2)
{
//echo "<br>::".substr($str,$i,2);
echo "<br>".hexdec(substr($str,$i,2)).":::".chr(hexdec(substr($str,$i,2)))."->".md5(chr(hexdec(substr($str,$i,2))));
$data.=chr(hexdec(substr($str,$i,2)));
}
echo "<br>".md5($data)."<br>";
return $data;
}
?>
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class utils {
public static void main(String[] args) {
utils u=new utils();
String up=u.jspassword("123456", "\\x00\\x00\\x00\\x00\\x16\\x9d\\x56\\x75", "!PRY", true);
System.out.println("-----------------------------");
System.out.println(up);
}
public String jspassword(String password,String pt,String vc,boolean md5){
if(md5)
{
password = utils.md5(password).toUpperCase();
}
int len =password.length();
String temp="";
for (int i=0; i < len ; i = i + 2)
{
temp += "\\x"+password.substring(i, i+2);
}
return (utils.md5(utils.md5(utils.hex2asc(temp)+utils.hex2asc(pt)) .toUpperCase()+vc) ).toUpperCase();
}
public static String hex2asc(String str){
String [] s=str.trim().split("\\\\x");
//System.out.println(s.length);
StringBuffer sb=new StringBuffer();
for(String sItem:s){
//System.out.println(sItem);
sb.append(sItem);
}
int len = sb.toString().length();
//String data = null;
StringBuffer sb1=new StringBuffer();
for (int i=0;i<len;i+=2)
{
int x=Integer.parseInt(sb.toString().substring(i, i+2),16);
char ss=(char) x;
System.out.println(x+"::"+ss+"转换后->"+utils.md5(String.valueOf(ss)));
sb1.append(String.valueOf(ss));
}
return sb1.toString();
}
public static String md5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
#7
function jspassword($p,$pt,$vc,$md5 = true)
{
echo $p.":".$pt.":".$vc;
if($md5)
{
$p = strtoupper(md5($p));
}
//echo "<br>".$p;exit;
$len = strlen($p);
$temp = null;
//echo "<br>md5Password:".$p."<br>";
for ($i=0; $i < $len ; $i = $i + 2)
{
//echo "<br>i:".$i;
$temp .= '\x'.substr($p, $i,2);
}
//echo "<br>".$temp."<br>";
//echo "<br><br><br>--->>>".md5(hex2asc($temp).hex2asc($pt));
//$str=hex2asc($temp).hex2asc($pt);
//echo "<br>内部:".$str."-->".md5(hex2asc($temp))."-->".md5(hex2asc($pt));
return strtoupper(md5(strtoupper(md5(hex2asc($temp).hex2asc($pt))).$vc));
}
/**
* 十六进制转字符
*
* @access private
* @param string $str
* @return string
*/
function hex2asc($str)
{
//echo "处理前:".$str."<br>";
//print_r( explode('\x', $str));
$str = join('', explode('\x', $str));
//echo "<br>处理后:".$str;
$len = strlen($str);
$data = null;
for ($i=0;$i<$len;$i+=2)
{
//echo "<br>::".substr($str,$i,2);
echo "<br>".hexdec(substr($str,$i,2)).":::".chr(hexdec(substr($str,$i,2)))."->".md5(chr(hexdec(substr($str,$i,2))));
$data.=chr(hexdec(substr($str,$i,2)));
}
echo "<br>".md5($data)."<br>";
return $data;
}
?>
[/code]
java在测试的时候传入的byte数组哦,要调用string.getBytes()方法 ,这个我测试是可以通过的 我也是这两天才开始学习java的 如果是编码的问题我就不知道怎么解决了
{
echo $p.":".$pt.":".$vc;
if($md5)
{
$p = strtoupper(md5($p));
}
//echo "<br>".$p;exit;
$len = strlen($p);
$temp = null;
//echo "<br>md5Password:".$p."<br>";
for ($i=0; $i < $len ; $i = $i + 2)
{
//echo "<br>i:".$i;
$temp .= '\x'.substr($p, $i,2);
}
//echo "<br>".$temp."<br>";
//echo "<br><br><br>--->>>".md5(hex2asc($temp).hex2asc($pt));
//$str=hex2asc($temp).hex2asc($pt);
//echo "<br>内部:".$str."-->".md5(hex2asc($temp))."-->".md5(hex2asc($pt));
return strtoupper(md5(strtoupper(md5(hex2asc($temp).hex2asc($pt))).$vc));
}
/**
* 十六进制转字符
*
* @access private
* @param string $str
* @return string
*/
function hex2asc($str)
{
//echo "处理前:".$str."<br>";
//print_r( explode('\x', $str));
$str = join('', explode('\x', $str));
//echo "<br>处理后:".$str;
$len = strlen($str);
$data = null;
for ($i=0;$i<$len;$i+=2)
{
//echo "<br>::".substr($str,$i,2);
echo "<br>".hexdec(substr($str,$i,2)).":::".chr(hexdec(substr($str,$i,2)))."->".md5(chr(hexdec(substr($str,$i,2))));
$data.=chr(hexdec(substr($str,$i,2)));
}
echo "<br>".md5($data)."<br>";
return $data;
}
?>
[/code]
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class utils {
public static void main(String[] args) {
utils u=new utils();
String up=u.jspassword("123456", "\\x00\\x00\\x00\\x00\\x16\\x9d\\x56\\x75", "!PRY", true);
System.out.println("-----------------------------");
System.out.println(up);
}
public String jspassword(String password,String pt,String vc,boolean md5){
if(md5)
{
password = utils.md5(password).toUpperCase();
}
int len =password.length();
String temp="";
for (int i=0; i < len ; i = i + 2)
{
temp += "\\x"+password.substring(i, i+2);
}
return (utils.md5(utils.md5(utils.hex2asc(temp)+utils.hex2asc(pt)) .toUpperCase()+vc) ).toUpperCase();
}
public static String hex2asc(String str){
String [] s=str.trim().split("\\\\x");
//System.out.println(s.length);
StringBuffer sb=new StringBuffer();
for(String sItem:s){
//System.out.println(sItem);
sb.append(sItem);
}
int len = sb.toString().length();
//String data = null;
StringBuffer sb1=new StringBuffer();
for (int i=0;i<len;i+=2)
{
int x=Integer.parseInt(sb.toString().substring(i, i+2),16);
char ss=(char) x;
System.out.println(x+"::"+ss+"转换后->"+utils.md5(String.valueOf(ss)));
sb1.append(String.valueOf(ss));
}
return sb1.toString();
}
public static String md5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
不知道为什么要
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
而且终值还始终在变
public static String getMd5(byte[] buffer) throws NoSuchAlgorithmException{
String s = null;
char hexDigist[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(buffer);
byte[] datas = md.digest(); //16个字节的长整数
char[] str = new char[2*16];
int k = 0;
for(int i=0;i<16;i++){
byte b = datas[i];
str[k++] = hexDigist[b>>>4 & 0xf];//高4位
str[k++] = hexDigist[b & 0xf];//低4位
}
s = new String(str);
return s;
}
java中的MD5返回的是一个128位的长整形数,即16个字节,一个字节映射成2个字符,所以就是32个字符,md5 不可能不一样的都是遵循md5协议实现的,只是PHP在底层用C语言实现了
这里测试下:
java:public static void main(String[] args) {
try {
System.out.println(getMd5("123".getBytes()));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
输出:
202cb962ac59075b964b07152d234b70
PHP代码:echo md5("123");
输出:
202cb962ac59075b964b07152d234b70
我把php和java代码全部贴出来吧
<?php
//php为正确的 结果:CA15B8C6D72A4442942045956DD371F8
$password="123456";
$pt='\x00\x00\x00\x00\x16\x9d\x56\x75';
$vc='!PRY';
$passwd = jspassword($password,$pt, $vc);
echo "<br>结果:".$pt."<br>".$vc."<br>密码:".$passwd."<br><br>";
function jspassword($p,$pt,$vc,$md5 = true)
{
echo $p.":".$pt.":".$vc;
if($md5)
{
$p = strtoupper(md5($p));
}
//echo "<br>".$p;exit;
$len = strlen($p);
$temp = null;
//echo "<br>md5Password:".$p."<br>";
for ($i=0; $i < $len ; $i = $i + 2)
{
//echo "<br>i:".$i;
$temp .= '\x'.substr($p, $i,2);
}
//echo "<br>".$temp."<br>";
//echo "<br><br><br>--->>>".md5(hex2asc($temp).hex2asc($pt));
//$str=hex2asc($temp).hex2asc($pt);
//echo "<br>内部:".$str."-->".md5(hex2asc($temp))."-->".md5(hex2asc($pt));
return strtoupper(md5(strtoupper(md5(hex2asc($temp).hex2asc($pt))).$vc));
}
/**
* 十六进制转字符
*
* @access private
* @param string $str
* @return string
*/
function hex2asc($str)
{
//echo "处理前:".$str."<br>";
//print_r( explode('\x', $str));
$str = join('', explode('\x', $str));
//echo "<br>处理后:".$str;
$len = strlen($str);
$data = null;
for ($i=0;$i<$len;$i+=2)
{
//echo "<br>::".substr($str,$i,2);
echo "<br>".hexdec(substr($str,$i,2)).":::".chr(hexdec(substr($str,$i,2)))."->".md5(chr(hexdec(substr($str,$i,2))));
$data.=chr(hexdec(substr($str,$i,2)));
}
echo "<br>".md5($data)."<br>";
return $data;
}
?>
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class utils {
public static void main(String[] args) {
utils u=new utils();
String up=u.jspassword("123456", "\\x00\\x00\\x00\\x00\\x16\\x9d\\x56\\x75", "!PRY", true);
System.out.println("-----------------------------");
System.out.println(up);
}
public String jspassword(String password,String pt,String vc,boolean md5){
if(md5)
{
password = utils.md5(password).toUpperCase();
}
int len =password.length();
String temp="";
for (int i=0; i < len ; i = i + 2)
{
temp += "\\x"+password.substring(i, i+2);
}
return (utils.md5(utils.md5(utils.hex2asc(temp)+utils.hex2asc(pt)) .toUpperCase()+vc) ).toUpperCase();
}
public static String hex2asc(String str){
String [] s=str.trim().split("\\\\x");
//System.out.println(s.length);
StringBuffer sb=new StringBuffer();
for(String sItem:s){
//System.out.println(sItem);
sb.append(sItem);
}
int len = sb.toString().length();
//String data = null;
StringBuffer sb1=new StringBuffer();
for (int i=0;i<len;i+=2)
{
int x=Integer.parseInt(sb.toString().substring(i, i+2),16);
char ss=(char) x;
System.out.println(x+"::"+ss+"转换后->"+utils.md5(String.valueOf(ss)));
sb1.append(String.valueOf(ss));
}
return sb1.toString();
}
public static String md5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
java在测试的时候传入的byte数组哦,要调用string.getBytes()方法 ,这个我测试是可以通过的 我也是这两天才开始学习java的 如果是编码的问题我就不知道怎么解决了
#8
编码不对 换下试试