c# - 如何使用 C# 为我的 WinRT 应用程序生成 MD5 哈希码?

标签 c# .net windows-runtime microsoft-metro

我正在创建一个 MetroStyle 应用程序,我想为我的字符串生成一个 MD5 代码。到目前为止,我用过这个:

    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 的异常,并显示以下错误消息:

目标多字节代码页中不存在 Unicode 字符的映射。 (HRESULT 异常:0x80070459)

我在这里做错了什么?

最佳答案

好的。我找到了如何做到这一点。这是最终代码:

    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;
    }

关于c# - 如何使用 C# 为我的 WinRT 应用程序生成 MD5 哈希码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8299142/

相关文章:

C# SqlCommand 命名约定

c# - 使用 .NET C# 连接到 Interbase 7.1 的最佳方式

.net - 在哪里存储加密 key

c# - 我可以向 C# MessageBox 添加控件吗?

xaml - 在 Windows 8 Metro 应用程序 (C#/XAML) 中创建表 (DataGrid)

c# - 在 AspNetCore 中构建/实例化静态类的正确方法

c# - WCF Web 服务中的安全上下文 token 无效或过期

c# - ItemsSource 绑定(bind)不更新值

c# - 在 ComboBoxItem 上绑定(bind) IsEnabled 不起作用

silverlight - 将 Windows Phone 7 应用程序移植到 Windows 8 平板电脑上的 Metro 有多容易?