真正销毁c#

时间:2018-07-31 09:58:42

标签: c# string passwords

我正在编写一个API,它将接受用户名和密码的标准字符串作为兼容性的一种方式。我知道标准字符串不是理想的,我的API已经为此使用了SecureString类,并且我上面的方法摘要对此进行了警告。但是,由于该API可能无法在可能使用SecureString的环境中使用,因此我编写了一个函数,当我的SecureString Extention方法将标准字符串转换为SecureString时,便会真正销毁字符串。

    public static void CrunchString(ref string str) {
        int l = str.Length;
        unsafe {
            fixed (char* c = str) {
                for (int i = 0; i < l; ++i) {
                    c[i] = (char)0x00;
                }
            }
        }
        str = null;
    }

这是正确的方法还是有更好的解决方案?通过就地破坏这种性质的字符串,是否可以预见任何后果?

这里的目的是真正尽早销毁不安全的字符串,并将其从正常内存中彻底删除。

0 个答案:

没有答案
相关问题