C#从内存中安全删除变量 [英] C# Securely delete variable from memory

查看:435
本文介绍了C#从内存中安全删除变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个与安全性相关的程序,并且我想确保自己以正确的方式进行操作.当有人使用密码登录时,密钥将被解密并存储在变量中.当他们注销时,我希望该密钥的内存中的数据被完全擦除,而不仅仅是标记为已删除.我目前正在执行以下操作:

I'm making a security related program and I want to be sure that I'm doing this the right way. When someone logs in with a password, a secret key is decrypted and stored in a variable. When they log out, I want the data in memory for that secret key to be completely erased, not just marked as deleted. I'm currently doing the following:

public void Logout()
{
    RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
    for (var i = 0; i < 3; i++)
    {
        byte[] data = new byte[(int) Math.Round((double) (_phraseHash.Count()))];
        rngCsp.GetBytes(data);
        int randomNum = BitConverter.ToInt32(data, 0);
        _phraseHash = randomNum.ToString();
    }
    LoggedIn = false;
    _phraseHash = null;
}

我想知道的是,这是否足以从系统中完全清除密钥(_phraseHash).

What I want to know is if this will be sufficient to completely erase the secret key (_phraseHash) from the system.

这甚至是必要的吗?我实际上并不了解如何删除内存中的数据,我只是假设这与硬盘驱动器的工作方式有些相似,在硬盘驱动器上,字节被标记为已删除并在其他空间需要时被重写.

Also is this even necessary? I don't actually know much about how data in memory is deleted, I just assumed it would be somewhat similar to how hard drives work where bytes are just marked deleted and rewritten when something else needs the space.

推荐答案

我认为您可能对

System.String类的实例是不可变的,并且在不再需要时,无法以编程方式安排进行垃圾回收;也就是说,实例在创建后是只读的,无法预测何时将实例从计算机内存中删除.由于System.String实例是不可变的,因此似乎会修改现有实例的操作实际上会创建该实例的副本以进行操作.因此,如果String对象包含敏感信息,例如密码,信用卡号或个人数据,则使用该信息后可能会泄露该信息,因为您的应用程序无法从计算机内存中删除数据.

An instance of the System.String class is both immutable and, when no longer needed, cannot be programmatically scheduled for garbage collection; that is, the instance is read-only after it is created, and it is not possible to predict when the instance will be deleted from computer memory. Because System.String instances are immutable, operations that appear to modify an existing instance actually create a copy of it to manipulate. Consequently, if a String object contains sensitive information such as a password, credit card number, or personal data, there is a risk the information could be revealed after it is used because your application cannot delete the data from computer memory.

SecureString对象类似于String对象,因为它具有一个 文字值.但是,SecureString对象的值固定在 内存,可以使用提供的保护机制,例如加密 可以通过基础操作系统进行修改,直到您的 应用程序将其标记为只读,并且可以从计算机中删除 通过调用Dispose方法的应用程序或通过 .NET Framework垃圾收集器.

A SecureString object is similar to a String object in that it has a text value. However, the value of a SecureString object is pinned in memory, may use a protection mechanism, such as encryption, provided by the underlying operating system, can be modified until your application marks it as read-only, and can be deleted from computer memory either by your application calling the Dispose method or by the .NET Framework garbage collector.

这篇关于C#从内存中安全删除变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆