How to generate persisted key for AES

最后都变了- 提交于 2021-02-07 08:13:24

问题


In the .NET Framework 4.6.2 Microsoft added support for persisted-key symmetric encryption using a CNG key storage provider and the AesCng algorithm. The documentation for instantiating the AesCng class using an existing key is pretty clear - just pass the key name into the constructor.

But how do I generate a new persisted key to use for AES encryption? I can't find the instructions in the release notes or the documentation. Is this even possible from C#?


回答1:


While not documented well, this turns out to be very easy to do. You just use the CngKey.Create method. The difficulty is that there is no CngAlgorithm property for AES encryption. This looks like an oversight from the .NET Framework team at Microsoft. However, you can construct your own CngAlgorithm by name:

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

See also: Asymetric cryptography example in c#



来源:https://stackoverflow.com/questions/45227683/how-to-generate-persisted-key-for-aes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!