java和C#md5算法互通

package com.wujialiang.test;

import java.security.MessageDigest;

public class Md5Util {
	/**
     * @param srcSignString
     * @param key
     * @param charset
     * @return
     */
    public static String getMd5(String srcSignString, String key, String charset) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(srcSignString.getBytes(charset));//相当于添加字符串

            StringBuilder result = new StringBuilder();
            byte[] temp;
            temp = md5.digest(key.getBytes(charset));//添加字符串之后计算
            for (byte b : temp) {
                result.append(Integer.toHexString((0x000000ff & b) | 0xffffff00).substring(6));
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

对应的C#代码如下


public static string Md5Hash(string input)
{
    MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
    byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(input));
    StringBuilder sBuilder = new StringBuilder();
    for (int i = 0; i < data.Length; i++)
    {
        sBuilder.Append(data[i].ToString("x2"));
    }
    return sBuilder.ToString();
}

注意这里需要自己拼接字符串,比如java代码如下

Md5Util.getMd5("8xxxsdfsdfswerwse2345325","1233456", "utf-8")

对应的C#代码如下

Md5Hash("8xxxsdfsdfswerwse2345325"+"1233456");

你可能感兴趣的:(java,dotnet,java,c#,python)