如何生成MD5哈希值$ C $下使用C#我的WinRT的应用程序吗? [英] How to generate MD5 hash code for my WinRT app using C#?

查看:126
本文介绍了如何生成MD5哈希值$ C $下使用C#我的WinRT的应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个MetroStyle应用程序,我想生成一个MD5 $ C $下我的字符串。到目前为止,我已经用这样的:

I'm creating a MetroStyle app and I want to generate a MD5 code for my string. So far I've used this:

    public static string ComputeMD5(string str)
    {
        try
        {
            var alg = HashAlgorithmProvider.OpenAlgorithm("MD5");
            IBuffer buff = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
            var hashed = alg.HashData(buff);
            var res = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, hashed);
            return res;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

但它抛出一个异常类型的 System.ArgumentOutOfRangeException ,出现以下错误消息:

but it throws an exception of type System.ArgumentOutOfRangeException with the following error message:

没有映射为统一code字符存在于多字节的目标code页面。 (从HRESULT异常:0x80070459)

我在做什么错在这里?

推荐答案

确定。我发现如何做到这一点。这里是最后的code:

OK. I've found how to do this. Here's the final code:

    public static string ComputeMD5(string str)
    {
        var alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
        IBuffer buff = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
        var hashed = alg.HashData(buff);
        var res = CryptographicBuffer.EncodeToHexString(hashed);
        return res;
    }

这篇关于如何生成MD5哈希值$ C $下使用C#我的WinRT的应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆