首页 > 解决方案 > 使用 Base64 编码将字节数组转换为 SecureString 的安全方法

问题描述

我有一个byte[]需要在 Base64 中编码并返回为SecureString. 我目前的代码如下:

        string privateString = Convert.ToBase64String(byteArray);
        SecureString result = new SecureString();
        foreach (char c in privateString)
        {
            result.AppendChar(c);
        }
        // wipe the byte array...

问题是调用Convert.ToBase64String不安全,因为它创建了一个我无法销毁的托管字符串。有没有一种安全的方法来做到这一点?

标签: c#securestring

解决方案


关于在没有中间字符串的情况下编码 base-64 数据的方法:System.Buffers.Text.Base64. 然而!SecureString不安全,现在基本上不应该使用。曾经。它没有针对任何有意义的攻击提供任何有用的保护。


推荐阅读