【问题标题】:Cannot find type System.Security.Cryptography.SHA256 on Windows Phone 8.1在 Windows Phone 8.1 上找不到类型 System.Security.Cryptography.SHA256
【发布时间】:2014-11-17 18:31:03
【问题描述】:

我想在 windows phone 8.1 c# xaml 上使用 SHA 256 加密。 但我收到以下错误: 在模块 mscorlib.dll 中找不到类型 System.Security.Cryptography.HashAlgorithm

而且确实 System.Security.Cryptography 在 Windows Phone(或 Windows 8 商店)上不可用。 然而,Windows.Security.Cryptography 是可用的,但我在那里也找不到这个类。

我需要使用 SHA 256 加密吗?有没有 dll 呢?

谢谢

【问题讨论】:

    标签: c# encryption windows-phone-8.1 windows-8.1 sha


    【解决方案1】:

    如果你在做通用应用程序,那就有点困难了。但是为了让你开始

    using Windows.Security.Cryptography;
    using Windows.Security.Cryptography.Core;
    
    HashAlgorithmProvider hap = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);   
    CryptographicHash ch = hap.CreateHash();            
    
    // read in bytes from file then append with ch.Append(data)           
    
    Windows.Storage.Streams.IBuffer b_hash = ch.GetValueAndReset();       // hash it
    string hash_string = CryptographicBuffer.EncodeToHexString(b_hash);   // encode it to a hex string for easy reading
    

    您必须创建一个 HashAlgorithmProvider 并为其提供您要使用的 HashAlgorithm

    查看HashAlgorithmNames. 命名空间以了解所有支持的哈希

    然后你从上面创建一个CryptographicHash。

    然后你使用.Append(data)将字节添加到CryptographicHash

    然后你计算哈希。

    然后你可以根据需要将其编码为十六进制字符串。


    我的哈希应用的无耻截图:)

    Click Here for Fullsize Image

    【讨论】:

    • 感谢您抽出宝贵时间回答我的问题。所以我可以使用这个链接的帮助msdn.microsoft.com/en-us/library/…对吗?
    • @asitis 是的,看起来是正确的。如果你愿意,你基本上可以复制我的代码,你只需要填写缓冲区。我这样做是为了轻松散列 2GB 以上的大文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多