import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;public class KeyedDigestMD5 {
public static byte[] getKeyedDigest(byte[] buffer, byte[] key) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(buffer);
return md5.digest(key);
} catch (NoSuchAlgorithmException e) {
}
return null;
}
public static String getKeyedDigest(String strSrc, String key) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(strSrc.getBytes("UTF8"));
String result="";
byte[] temp;
temp=md5.digest(key.getBytes("UTF8"));
for (int i=0; i<temp.length; i++){
result+=Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);
}
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}catch(Exception e)
{
e.printStackTrace();
}
return null;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String mi;
String s = "hf1000";
//第二个参数请填空字符串
mi=KeyedDigestMD5.getKeyedDigest(s,"");
System.out.println("mi:"+mi);
}}
import java.security.NoSuchAlgorithmException;public class KeyedDigestMD5 {
public static byte[] getKeyedDigest(byte[] buffer, byte[] key) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(buffer);
return md5.digest(key);
} catch (NoSuchAlgorithmException e) {
}
return null;
}
public static String getKeyedDigest(String strSrc, String key) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(strSrc.getBytes("UTF8"));
String result="";
byte[] temp;
temp=md5.digest(key.getBytes("UTF8"));
for (int i=0; i<temp.length; i++){
result+=Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);
}
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}catch(Exception e)
{
e.printStackTrace();
}
return null;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String mi;
String s = "hf1000";
//第二个参数请填空字符串
mi=KeyedDigestMD5.getKeyedDigest(s,"");
System.out.println("mi:"+mi);
}}
首先
using System.Security.Cryptography;
using System.Text; 然后
MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
string source="hf1000";
byte[] message;
message=Encoding.Default.GetBytes(source);
md5.ComputeHash(message);
Console.WriteLine(Convert.ToBase64String(md5.Hash));
这段,我就是不明白这段程序执行的操作是什么。
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(strSrc.getBytes("UTF8"));
String result="";
byte[] temp;
temp=md5.digest(key.getBytes("UTF8"));
for (int i=0; i <temp.length; i++){
result+=Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);
}
关键就是这段代码如何用C#写出来。其余的不管。
byte[] message = Encoding.ASCII.GetBytes(Source); byte[] temp= md5.ComputeHash(message);
string result=""; for (int i = 0; i < temp.Length; i++)
{ result +=Convert.ToString( ((0x000000ff & temp[i]) | 0xffffff00),16).Substring(6);
}
return result;
可以了,多谢。