求大神帮忙看看c#与java md5加密后结果不一样,该怎么改java代码

Mosthandsomeykh 2018-05-16 11:07:58
c#代码
public static string MD5Encrypt16(string toCryString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string result = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(toCryString)), 4, 8);
result = result.Replace("-", "");
return result;
}


java代码
public static String MD5(String inStr) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];

for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];

byte[] md5Bytes = md5.digest(byteArray);

StringBuffer hexValue = new StringBuffer();

for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
...全文
1047 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
maradona1984 2018-05-16
  • 打赏
  • 举报
回复
这个你得选择一致的算法,MD5也有长度,大小写的区别的,搞成一致就行了
Mosthandsomeykh 2018-05-16
  • 打赏
  • 举报
回复
我需要的是改java代码 c#的不用改,希望各位大神帮忙看看
Mosthandsomeykh 2018-05-16
  • 打赏
  • 举报
回复
已解决,在c#中是采用了分割,我在java中也加了一个分割字符串的就好了
天行归来 2018-05-16
  • 打赏
  • 举报
回复

	public static String MD5(String inStr){
		if ((inStr==null)||(inStr.length()==0)) return "";

		try{
			MessageDigest md5 = null;
			md5 = MessageDigest.getInstance("MD5");
			byte[] byteArray = inStr.getBytes("UTF-8");
			byte[] md5Bytes = md5.digest(byteArray);
			StringBuffer hexValue = new StringBuffer();
			for (int i=0; i<md5Bytes.length; i++){
				int val = md5Bytes[i] & 0xff;
				if (val < 16) {
					hexValue.append("0");
				}
				hexValue.append(Integer.toHexString(val));
			}
            return hexValue.toString();
		}catch(Exception e){
			e.printStackTrace();
        	return "";
        }
    }

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧