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

williamwsj 2016-07-28 02:42:47
求哪位大侠将此段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);
}
}
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
williamwsj 2016-07-28
  • 打赏
  • 举报
回复
引用 3 楼 starfd 的回复:
这个就是简单的sha1摘要,你网上随便找下就行了
果然有大侠风范!结贴
  • 打赏
  • 举报
回复
这个就是简单的sha1摘要,你网上随便找下就行了
williamwsj 2016-07-28
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
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
结果一致吗?
public static String hmacSign(String aValue, String aKey) 这个结果一致。 大侠,还有这个呢,麻烦一并翻译成c#吧,感谢! 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)); }
  • 打赏
  • 举报
回复
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
结果一致吗?

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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