System.Security.Cryptography与Windows.Security.Cryptography

Raz*_*ick 12 c# encryption windows-8

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

我目前正在将该应用程序移植到Windows 8作为Modern UI(Metro)应用程序.它很顺利,除了当我尝试导入我的密钥派生代码(它获取用户的密码并从中获取密钥256位密钥)时,Visual Studio Ultimate 2013表明它无法识别using System.Security.Cryptography.

在查看Windows 8开发人员网站后,我发现有一个新类Windows.Security.Cryptography,但是,它似乎也没有被Visual Studio识别.

所以,既然你有背景,我有几个问题:

  1. System.Security.Cryptography在Windows 8中可用吗?如果是这样,是否支持RT版本?如何让Visual Studio识别它?
  2. Windows.System.Security是如何不同的,是否有与Rfc2898DeriveBytes兼容的类/方法?通过兼容,我的意思是给出相同的密码和盐是有一种方法来获得相同的密钥作为结果.

为了澄清我想要做什么,我的密钥派生代码发布如下:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 11

1)System Store上没有System.Security.Cryptography,因此您必须使用Windows.Security.Cryptography.有关使用.NET可移植库重用不同目标框架的类库的详细说明,请参阅下面的链接.如果需要,您可以使用您最喜欢的IoC容器注入抽象.

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


mil*_*ner 4

Windows.Security.Cryptography 及其子命名空间可能是最佳选择。

请参阅http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.cryptographyengine.derivekeymaterial.aspx,了解使用多种不同算法派生密钥材料的方法。

  • 我真的很讨厌 MSDN 不清楚我需要添加什么翻转参考才能激活这个“使用”。 (2认同)