gpt4 book ai didi

c# - 内存中烧录System.String

转载 作者:太空宇宙 更新时间:2023-11-03 19:09:22 26 4
gpt4 key购买 nike

我试图从内存中删除普通字符串的任何痕迹,为此,我从普通字符串的引用创建了一个 SecureString 实例。像这样:

public static unsafe void Burn(this string input)
{
fixed (char* c = input)
{
var secure = new SecureString(c, input.Length);
secure.Dispose();
}
}

问题是,即使在调用 dispose 方法之后,input 的内容也没有改变。根据我的理解,SecureString 实例应该引用 input 地址,因此如果在 Dispose() 调用时从内存中清除。我错过了什么?

最佳答案

出现the two parameter constructor不打算由您的代码使用。文档不清楚,但它使用短语 Initializes a new instance of the SecureString class from a subarray of System.Char objects 告诉我它可能正在复制数据,而不是加密现有的字符串.这是有道理的,因为 SecureString 的文档特别提到 String 不能以确定的方式销毁。

检验这个理论的一个好方法是比较inputsecure 的地址,看看它们在初始化后是否仍然指向内存中的相同位置。

关于c# - 内存中烧录System.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22107804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com