【问题标题】:How to explicitly delete an AES key generated in .NET using AESManaged?如何使用 AESManaged 显式删除在 .NET 中生成的 AES 密钥?
【发布时间】:2020-02-12 01:41:30
【问题描述】:

我正在尝试在.NET 中生成和删除 AES 密钥。这只是一个自学练习,不会部署到任何地方。下面的代码使用AESManaged生成一个AES密钥

using System;
using System.Security.Cryptography;
using System.Text;

namespace genkeys
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("generating keys with .NET");

                AesManaged aes = new AesManaged();
                aes.GenerateKey();
                aes.GenerateIV();
                ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
                Console.ReadLine(); 
                // how to delete the generated key here?

        }
    }
}

理论上,我了解正确的密钥卫生要求我们在完成后立即从 RAM 中删除密钥。在C++ 中,对CryptDestroyKey (https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptdestroykey) 的显式调用允许我们擦除密钥。 .NET 中是否有等效电话。换句话说,如何删除上面代码中生成的密钥? (我试过cngkey.Delete(),但在这种情况下不起作用)。

【问题讨论】:

    标签: c# .net encryption cryptography


    【解决方案1】:

    你会使用aes.Clear();

    希望对你有帮助:)

    【讨论】:

    • 谢谢。我试过了,但似乎在aes.Clear!
    • 好吧,否则,根据我对 C#AES 的理解,您不能。
    • 我会再研究一些,几分钟后回复你。
    • 你可以将aes设置成null,然后往里面吐出一些随机值,然后调用GC.Collect()
    • AesManagedIDisposable, using 它应该清除密钥吗? (github.com/microsoft/referencesource/blob/master/System.Core/…)
    猜你喜欢
    • 2012-12-30
    • 1970-01-01
    • 2018-05-31
    • 2020-02-24
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    相关资源
    最近更新 更多