c# - System.Security.Cryptography 与 Windows.Security.Cryptography

标签 c# encryption windows-8

我是一名新的 Windows 8 开发人员,我有一些代码是为 Linux 设计的,但只要安装了 GTK# 也可以在 Windows 上运行。

我目前正在将该应用程序作为现代 UI (Metro) 应用程序移植到 Windows 8。一切顺利,除了,当我尝试导入我的 key 派生代码(获取用户密码并从中派生 key 256 位 key )时,Visual Studio Ultimate 2013 指示它无法识别 using System.安全.密码学

在查看 Windows 8 开发者网站后,我发现有一个新类 Windows.Security.Cryptography 可用,但是,Visual Studio 似乎也无法识别它。

那么,既然你已经有了背景,我有几个问题:

  1. System.Security.Cryptography 在 Windows 8 中可用吗?如果可以,是否支持 RT 版本?如何让 Visual Studio 识别它?
  2. Windows.System.Security 有何不同,是否有与 Rfc2898DeriveBytes 兼容的类/方法?兼容,我的意思是给定相同的密码和 salt 是否有办法获得相同的 key 作为结果。

为了澄清我想做什么,我的 key 派生代码贴在下面:

public class GetKey
{
    // constructor
    public GetKey (bool use=true, string password="none")
    {   if (use == true)
        {
            this.genSalt();
            byte[] salt = this.salt;
            Rfc2898DeriveBytes pwdKey = new Rfc2898DeriveBytes(password, salt, 4000);
            this.key = pwdKey.GetBytes(32);
            this.iv = pwdKey.GetBytes(16);
        }
    }

    // properties
    private byte[] key;
    private byte[] iv;
    private byte[] salt;

    // methods
    public void retrieveKey(string password)
    {
        try 
        {
            byte[] salt = this.salt;
            Rfc2898DeriveBytes pwdKey = new Rfc2898DeriveBytes(password, salt, 4000);
            this.key = pwdKey.GetBytes(32);
            this.iv = pwdKey.GetBytes(16);
        }
        catch (Exception e)
        {
            GenericDialog win = new GenericDialog("Unknown Error: " + e.Message, "Error Notice", "Unknown Error");
            win.Show();
        }
    }

    public void genSalt()
    {
        string sSalt = UtilityClass.randString(16);
        byte[] salt = UtilityClass.ToByteArray(sSalt);
        this.salt = salt;
    }   

    public byte[] returnKey()
    {
        return this.key;
    }

    public byte[] returnIv()
    {
        return this.iv;
    }

    public byte[] returnSalt()
    {
        return this.salt;
    }
    public bool setSalt(string salt)
    {
        try 
        {
            this.salt = Convert.FromBase64String(salt);
        }
        catch
        {
            GenericDialog win = new GenericDialog("Decryption failed because the salt was invalid.", "Error Notice", "Invalid Salt");
            win.Show();
            return false;
        }
        return true;
    }
}

最佳答案

1) System.Security.Cryptography 在 Windows 应用商店应用程序中不可用,因此您必须使用 Windows.Security.Cryptography。有关使用 .NET 可移植库为不同目标框架重用类库的详细解释,请参见下面的链接。如果需要,您始终可以使用您最喜欢的 IoC 容器注入(inject)抽象。

http://www.hanselman.com/blog/HiddenGemsInVisualStudio11BetaNETPortableClassLibraries.aspx

2) 我没有在 Windows.Security.Cryptography 或类似的东西中看到 Rfc2898DeriveBytes 的实现。见下文。

http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.symmetricalgorithmnames.aspx

关于c# - System.Security.Cryptography 与 Windows.Security.Cryptography,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12790459/

相关文章:

c# - 基于任务的编程在 ASP.Net 页面或异步页面中是否可接受是首选方法

php - 如何在 php 中加密 mysql 密码(或如何打败自动代码扫描器红旗)

windows-8 - 签名工具错误: No certificates were found that met all given criteria with a Windows Store App?

c++ - Shell执行未定义的错误

javascript - WinJS 中未处理的异常

c# - VB.NET 比 C# 更便携吗?

c# - 从生成的表中检索数据时对象名称 'dbo.TableName' 无效

c# - 以编程方式在 IIS 中注册 Silverlight MIME 类型

ios - soundcloud iOS 加密

php - 敏感数据的基本安全加密?