Java写法改成C#

【Help】 2016-07-15 02:52:55

[code=csharp]
private static byte[] MD5(String data) throws IOException {
byte[] bytes = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
bytes = md.digest(data.getBytes(Constants.CHARSET_UTF8));
} catch (GeneralSecurityException gse) {
String msg = getStringFromException(gse);
throw new IOException(msg);
}
return bytes;
}

private static String B(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toUpperCase());
}
return sign.toString();
}


[/code]
...全文
145 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
这个就不知道你具体是哪种HMAC了 微软默认提供了 HMACMD5 HMACSHA1 HMACSHA256 HMACSHA384 HMACSHA512 HMACRIPEMD160
【Help】 2016-07-15
  • 打赏
  • 举报
回复
引用 2 楼 starfd 的回复:
按你的java代码,MD5Of(string text)这个方法就可以了,转换结果也是大写的
public static String encryptHMAC(String data, String key) { if (Strings.isNullOrEmpty(data)) { return null; } byte[] bytes = encryptHMAC(data.getBytes(), key); return byteArrayToHexString(bytes); }这种呢?
  • 打赏
  • 举报
回复
按你的java代码,MD5Of(string text)这个方法就可以了,转换结果也是大写的
  • 打赏
  • 举报
回复
md5算法?这个不用换了吧?直接C#代码就可以
public class Digest
    {
        public static string MD5Of(string text)
        {
            return MD5Of(text, Encoding.UTF8);
        }
        public static string MD5Of(string text, Encoding enc)
        {
            return HashOf<MD5CryptoServiceProvider>(text, enc);
        }
        public static string SHA1Of(string text)
        {
            return SHA1Of(text, Encoding.UTF8);
        }
        public static string SHA1Of(string text, Encoding enc)
        {
            return HashOf<SHA1CryptoServiceProvider>(text, enc);
        }

        public static string SHA384Of(string text)
        {
            return SHA384Of(text, Encoding.UTF8);
        }
        public static string SHA384Of(string text, Encoding enc)
        {
            return HashOf<SHA384CryptoServiceProvider>(text, enc);
        }

        public static string SHA512Of(string text)
        {
            return SHA512Of(text, Encoding.UTF8);
        }
        public static string SHA512Of(string text, Encoding enc)
        {
            return HashOf<SHA512CryptoServiceProvider>(text, enc);
        }

        public static string SHA256Of(string text)
        {
            return SHA256Of(text, Encoding.UTF8);
        }
        public static string SHA256Of(string text, Encoding enc)
        {
            return HashOf<SHA256CryptoServiceProvider>(text, enc);
        }

        public static string HashOf<TP>(string text, Encoding enc)
            where TP : HashAlgorithm, new()
        {
            return BitConverter.ToString(HashBytes<TP>(text, enc)).Replace("-", "");
        }
        public static byte[] HashBytes<TP>(string text, Encoding enc)
            where TP : HashAlgorithm, new()
        {
            var buffer = enc.GetBytes(text);
            var provider = new TP();
            return provider.ComputeHash(buffer);
        }
    }

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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