【问题标题】:How to generate persisted key for AES如何为 AES 生成持久密钥
【发布时间】:2017-12-26 21:59:51
【问题描述】:

在 .NET Framework 4.6.2 Microsoft added support for persisted-key symmetric encryption 中使用 CNG 密钥存储提供程序和 AesCng 算法。 documentation for instantiating the AesCng class 使用现有密钥非常清楚 - 只需将密钥名称传递给构造函数即可。

但是如何生成新的持久密钥以用于 AES 加密?我在发行说明或文档中找不到说明。这甚至可以从 C# 中实现吗?

【问题讨论】:

  • 您是否尝试过仅使用键名调用构造函数,然后调用GenerateKey?是的,Microsoft 加密 API 文档肯定是 foobar。
  • 是的。构造函数抛出 CryptographicException 'Keyset does not exist'。

标签: c# .net encryption


【解决方案1】:

虽然没有很好地记录,但事实证明这很容易做到。您只需使用 CngKey.Create 方法。困难在于 AES 加密没有 CngAlgorithm 属性。这看起来像是 Microsoft .NET Framework 团队的疏忽。但是,您可以通过名称构造自己的 CngAlgorithm:

    public static string CreateKey(string name)
    {
        CngKey.Create(new CngAlgorithm("AES"), name);
    }

另请参阅:Asymetric cryptography example in c#

【讨论】:

  • 太好了,感谢您的反馈!不幸的是,我已经向微软提出了许多改进文档的提示(似乎没有采取任何行动);它仍然非常稀疏。
猜你喜欢
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 2011-07-13
相关资源
最近更新 更多