首页新闻招聘找找看知识库

哪位大神会java啊,看看这个MD5加密

0
悬赏园豆:40 [待解决问题]

需要集成一些接口,对方用的是java md5的加密方式,我尝试用C#方式进行加密一直比对失败,下面是对方要求的JAVA MD5的源码,哪位大神转成C#,不胜感激

 

package com.pay.util;

import java.security.MessageDigest;

public class MD5Util {

private static String byteArrayToHexString(byte b[]) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++)
resultSb.append(byteToHexString(b[i]));

return resultSb.toString();
}

private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n += 256;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}

public static String MD5Encode(String origin, String charsetname) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname))
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
else
resultString = byteArrayToHexString(md.digest(resultString
.getBytes(charsetname)));
} catch (Exception exception) {
}
return resultString;
}

private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

}

Vict💋r的主页 Vict💋r | 初学一级 | 园豆:164
提问于:2017-10-13 16:03
< >
分享
所有回答(1)
0

java test

    public static void main(String[] args)
    { 
        String str = "hello,你好,(@^0^@)"; 
        System.out.println(MD5Encode(str, "utf-8"));//009ad7b19678fe7931d489b5b4b11028
        System.out.println(MD5Encode(str, "gbk"));//d43cbb676804c966f75b3f00fda7ad4e
    }

 

c# test

 1   class TestMD5
 2     {
 3         private static readonly String[] hexDigits =
 4     { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e",
 5             "f" };
 6 
 7         private static String byteArrayToHexString(byte[] b)
 8         { 
 9             StringBuilder resultSb = new StringBuilder();
10             for (int i = 0; i < b.Length; i++)
11             {
12                 resultSb.Append(byteToHexString(b[i]));
13             }
14             return resultSb.ToString();
15         }
16         private static String byteToHexString(byte b)
17         {
18             int n = b;
19             if (n < 0)
20             {
21                 n += 256;
22             }
23             int d1 = n / 16;
24             int d2 = n % 16;
25             return hexDigits[d1] + hexDigits[d2];
26         }
27         public static String MD5Encode(String origin, String charsetname)
28         {
29             byte[] input;
30             try
31             {
32 
33                 if (charsetname == null || "".Equals(charsetname))
34                 {
35                     input = Encoding.Default.GetBytes(origin);
36                //     Console.WriteLine("系统默认编码:" + Encoding.Default.EncodingName);
37                 }
38                 else
39                 {
40                     input = Encoding.GetEncoding(charsetname).GetBytes(origin);
41                 }
42                 MD5CryptoServiceProvider md = new MD5CryptoServiceProvider(); 
43                 return byteArrayToHexString(md.ComputeHash(input));
44             }
45             catch (Exception exception)
46             {
47                 Console.WriteLine(exception);
48             }
49             return null;
50         }
51         public static void Main(String[] args)
52         {
53             String str = "hello,你好,(@^0^@)"; 
54             Console.WriteLine(MD5Encode(str, "utf-8"));//009ad7b19678fe7931d489b5b4b11028
55             Console.WriteLine(MD5Encode(str, "gbk"));//d43cbb676804c966f75b3f00fda7ad4e
56             Console.WriteLine();
57         }
58     }
TCG2008 | 园豆:845 (小虾三级) | 2017-10-13 22:37
   您需要登录以后才能回答,未注册用户请先注册