我一直在研究使用System.Security.SecureString类在处理信用卡号码时将其保存在内存中。是否有人使用SecureString类来保存信用卡号码,还是大多数人只使用普通的System.String类?
我一直在研究使用System.Security.SecureString类在处理信用卡号码时将其保存在内存中。是否有人使用SecureString类来保存信用卡号码,还是大多数人只使用普通的System.String类?
你可能已经完成了你的项目,但希望这能帮助其他人。在ASP.NET应用程序中使用SecureString类不太合适。不可能从包含敏感数据(例如信用卡号码)的Web页面中提取数据并将其放入SecureString中,而不经过中间System.String对象
我在其他方面(不是信用卡)使用SecureString,如果它将在内存中缓存一段时间。
我一直遇到的问题是,您仍然必须将其编组为普通字符串才能实际用于任何事情,因此其有用性非常有限。
我创建了几个扩展方法,以便稍微方便地使用它们:
public static unsafe SecureString Secure(this string source)
{
if (source == null)
return null;
if (source.Length == 0)
return new SecureString();
fixed (char* pChars = source.ToCharArray())
{
SecureString secured = new SecureString(pChars, source.Length);
return secured;
}
}
public static string Unsecure(this SecureString source)
{
if (source == null)
return null;
IntPtr bstr = Marshal.SecureStringToBSTR(source);
try
{
return Marshal.PtrToStringUni(bstr);
}
finally
{
Marshal.ZeroFreeBSTR(bstr);
}
}
不要使用securestring。PG正在弃用它。观看视频,他们概述了它所涉及的诸多安全问题。 https://github.com/dotnet/apireviews/tree/master/2015-07-14-securestring。