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

需要集成一些接口,对方用的是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" };
}

3个回答

package com.ego.commons.utils;

import java.security.MessageDigest;

/**

  • 密码加密工具
    */
    public class PasswordDigest {

    /**

    • MD5加密
    • @param password 要加密的源数据.
    • @return 定长32的字符串. */ public static String digestPassword(String password){ try{ MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] temp = digest.digest(password.getBytes()); StringBuilder builder = new StringBuilder(""); for(byte b : temp){ String t = Integer.toHexString(b & 0xFF); if(t.length() == 1){ builder.append("0"); } builder.append(t); } return builder.toString(); }catch(Exception e){ return null; } }

}
调用这个方法就可以了

你好,我是要java代码转成C#的语言

对 换成C#语句会好一些

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!