求助,如何用PHP5实现.net中MD5.ComputerHash() ?最近在写一个PHP发送飞信的类。我希望把一个C#的示例修改成PHP的,但是在用户密码加密部分遇到了问题。
关键的地方是.net中的MD5函数。.net的MD5的参数是一个byte数组,而php中没有这类用法。希望得到大家的帮助,谢谢C#代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;namespace MyMd5Test
{
class Program
{
static void Main(string[] args)
{
Console.Write("{0}", fetion_sipc_gen_digest());
Console.Read();
}
private static string fetion_sipc_gen_digest()
{
string g_cnonce = "AAB3238922BCC25A6F606EB525FFDC56";
string g_nonce = "1BCFA7A67AB2091C76C791EB6D5CB26C"; string Fetion_Number = "838713940";
string FETION_DOMAIN_URL = "fetion.com.cn";
string Mobile_Pass = "a123456789A"; byte[] key = md5(String.Format("{0}:{1}:{2}", Fetion_Number, FETION_DOMAIN_URL, Mobile_Pass)); byte[] h1 = md5(MergeByteArray(key, String2Byte(String.Format(":{0}:{1}", g_nonce, g_cnonce))));
string H1 = Byte2HexString(h1).ToUpper();
//H1 = "1ED7375C290E6425567253FDD5A4DDAB" return H1; }
public static byte[] MergeByteArray(byte[] a, byte[] b)
{
byte[] n = new byte[a.Length + b.Length];
a.CopyTo(n, 0);
b.CopyTo(n, a.Length);
return n;
}
public static byte[] md5(byte[] bytes)
{
MD5 MD5Instance = MD5.Create();
return MD5Instance.ComputeHash(bytes); // 这个地方非常关键,不知道如何用php实现这段代码
} public static byte[] md5(String s)
{;
return md5(String2Byte(s));
} public static byte[] String2Byte(string s)
{
return ((new System.Text.ASCIIEncoding()).GetBytes(s));
} public static string Byte2String(byte[] bytes)
{
return ((new System.Text.ASCIIEncoding()).GetString(bytes));
} public static string Byte2HexString(byte[] bytes)
{
string r = "";
for (int i = 0; i < bytes.Length; i++)
{
r += bytes[i].ToString("x").PadLeft(2, '0');
}
return r;
}
}
}希望用php实现函数fetion_sipc_gen_digest(),请各位帮忙。
C#示例代码:http://xqin.googlecode.com/svn/trunk/Programming%20Language/C_Sharp/WebFetion/
关键的地方是.net中的MD5函数。.net的MD5的参数是一个byte数组,而php中没有这类用法。希望得到大家的帮助,谢谢C#代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;namespace MyMd5Test
{
class Program
{
static void Main(string[] args)
{
Console.Write("{0}", fetion_sipc_gen_digest());
Console.Read();
}
private static string fetion_sipc_gen_digest()
{
string g_cnonce = "AAB3238922BCC25A6F606EB525FFDC56";
string g_nonce = "1BCFA7A67AB2091C76C791EB6D5CB26C"; string Fetion_Number = "838713940";
string FETION_DOMAIN_URL = "fetion.com.cn";
string Mobile_Pass = "a123456789A"; byte[] key = md5(String.Format("{0}:{1}:{2}", Fetion_Number, FETION_DOMAIN_URL, Mobile_Pass)); byte[] h1 = md5(MergeByteArray(key, String2Byte(String.Format(":{0}:{1}", g_nonce, g_cnonce))));
string H1 = Byte2HexString(h1).ToUpper();
//H1 = "1ED7375C290E6425567253FDD5A4DDAB" return H1; }
public static byte[] MergeByteArray(byte[] a, byte[] b)
{
byte[] n = new byte[a.Length + b.Length];
a.CopyTo(n, 0);
b.CopyTo(n, a.Length);
return n;
}
public static byte[] md5(byte[] bytes)
{
MD5 MD5Instance = MD5.Create();
return MD5Instance.ComputeHash(bytes); // 这个地方非常关键,不知道如何用php实现这段代码
} public static byte[] md5(String s)
{;
return md5(String2Byte(s));
} public static byte[] String2Byte(string s)
{
return ((new System.Text.ASCIIEncoding()).GetBytes(s));
} public static string Byte2String(byte[] bytes)
{
return ((new System.Text.ASCIIEncoding()).GetString(bytes));
} public static string Byte2HexString(byte[] bytes)
{
string r = "";
for (int i = 0; i < bytes.Length; i++)
{
r += bytes[i].ToString("x").PadLeft(2, '0');
}
return r;
}
}
}希望用php实现函数fetion_sipc_gen_digest(),请各位帮忙。
C#示例代码:http://xqin.googlecode.com/svn/trunk/Programming%20Language/C_Sharp/WebFetion/
string g_cnonce = "AAB3238922BCC25A6F606EB525FFDC56";
string g_nonce = "1BCFA7A67AB2091C76C791EB6D5CB26C"; string Fetion_Number = "838713940";
string FETION_DOMAIN_URL = "fetion.com.cn";
string Mobile_Pass = "a123456789A";这几个是必要的参数。这几个是参数
$g_cnonce = 'AAB3238922BCC25A6F606EB525FFDC56';
$g_nonce = '1BCFA7A67AB2091C76C791EB6D5CB26C'; $Fetion_Number = '838713940';
$FETION_DOMAIN_URL = 'fetion.com.cn';
$Mobile_Pass = 'a123456789A'; $key = md5($Fetion_Number . ':' . $FETION_DOMAIN_URL . ':' . $Mobile_Pass, true);
return strtoupper(md5($key . ':' . $g_nonce . ':' . $g_cnonce));
}
我对PHP的认识和理解还是不够深入,要向专家学习!
近期我就把这个飞信的php类发上来和大家交流。