求大牛将此段JAVA翻译为C#

.Net技术 码拜 8年前 (2016-05-15) 1005次浏览
求哪位大牛将此段JAVA翻译为C#,代码如下:

public class DigestUtil {
	private static String encodingCharset = "UTF-8";
	public static String hmacSign(String aValue, String aKey) {
		byte k_ipad[] = new byte[64];
		byte k_opad[] = new byte[64];
		byte keyb[];
		byte value[];
		try {
			keyb = aKey.getBytes(encodingCharset);
			value = aValue.getBytes(encodingCharset);
		} catch (UnsupportedEncodingException e) {
			keyb = aKey.getBytes();
			value = aValue.getBytes();
		}
		Arrays.fill(k_ipad, keyb.length, 64, (byte) 54);
		Arrays.fill(k_opad, keyb.length, 64, (byte) 92);
		for (int i = 0; i < keyb.length; i++) {
			k_ipad[i] = (byte) (keyb[i] ^ 0x36);
			k_opad[i] = (byte) (keyb[i] ^ 0x5c);
		}
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
		md.update(k_ipad);
		md.update(value);
		byte dg[] = md.digest();
		md.reset();
		md.update(k_opad);
		md.update(dg, 0, 16);
		dg = md.digest();
		return toHex(dg);
	}
	public static String toHex(byte input[]) {
		if (input == null)
			return null;
		StringBuffer output = new StringBuffer(input.length * 2);
		for (int i = 0; i < input.length; i++) {
			int current = input[i] & 0xff;
			if (current < 16)
				output.append("0");
			output.append(Integer.toString(current, 16));
		}
		return output.toString();
	}
	public static String getHmac(String[] args, String key) {
		if (args == null || args.length == 0) {
			return (null);
		}
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < args.length; i++) {
			str.append(args[i]);
		}
		return (hmacSign(str.toString(), key));
	}
	public static String digest(String aValue) {
		aValue = aValue.trim();
		byte value[];
		try {
			value = aValue.getBytes(encodingCharset);
		} catch (UnsupportedEncodingException e) {
			value = aValue.getBytes();
		}
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("SHA");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
		return toHex(md.digest(value));
	}

	public static String removeHmacContext(String xml) {
		return xml.replaceAll("<Hmac>.+</Hmac>", "<Hmac></Hmac>");
	}

	public static String addHmacContext(String xml,String aKey) {
		return xml.replaceAll("<Hmac></Hmac>", "<Hmac>" + hmacSign(xml,aKey) + "</Hmac>");
	}

	public static String getHmacFromContext(String xml){
		int s =  xml.indexOf("<Hmac>")+6;
		int i =  xml.indexOf("</Hmac>");
		return xml.substring(s, i);

	}
	public static void main(String[] args) {
		String s = "123456";
		String res = hmacSign(s, "E83Jjif8e83jJt");
		System.out.println("res:"+res);
	}
}
解决方案

100

HMACMD5 md5 = new HMACMD5(Encoding.UTF8.GetBytes("E83Jjif8e83jJt"));
var tmp = md5.ComputeHash(Encoding.UTF8.GetBytes("123456"));
Console.WriteLine(BitConverter.ToString(tmp).Replace("-", string.Empty).ToLower());
//输出:8ead2d78450a46b9d86cf2c705d78748

结果一致吗?


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求大牛将此段JAVA翻译为C#
喜欢 (0)
[1034331897@qq.com]
分享 (0)