有关创建以System.String出它不保护你的SecureString的所有预订一边,怎么能做到呢?
如何将普通的System.Security.SecureString转换为System.String?
我相信很多熟悉SecureString的人会回应说,永远不应该将SecureString转换为普通的.NET字符串,因为它会删除所有安全保护. 我知道.但是现在我的程序用普通字符串完成所有操作,我正在尝试增强其安全性,虽然我将使用返回SecureString的API给我,但我并不是想用它来增加我的安全性.
我知道Marshal.SecureStringToBSTR,但我不知道如何取出BSTR并从中制作System.String.
对于那些可能要求知道我为什么要这样做的人,好吧,我正在从用户那里获取密码并将其作为html表单POST提交以将用户登录到网站.所以......这真的必须使用托管的,未加密的缓冲区.如果我甚至可以访问非托管的,未加密的缓冲区,我想我可以在网络流上进行逐字节流写入,并希望这样可以保证密码的安全性.我希望能够回答至少其中一种情况.
我有一些string,我想用使用C#的SHA-256哈希函数来哈希它.我想要这样的东西:
string hashString = sha256_hash("samplestring");
Run Code Online (Sandbox Code Playgroud)
框架中是否有内置功能可以执行此操作?
在.NET中,我们有SecureString类,在你尝试使用它之前一切都很好,因为(例如)散列字符串,你需要明文.在编写一个将散列SecureString的函数时,我已经开始了,给定一个带有字节数组并输出字节数组的散列函数.
private static byte[] HashSecureString(SecureString ss, Func<byte[], byte[]> hash)
{
// Convert the SecureString to a BSTR
IntPtr bstr = Marshal.SecureStringToBSTR(ss);
// BSTR contains the length of the string in bytes in an
// Int32 stored in the 4 bytes prior to the BSTR pointer
int length = Marshal.ReadInt32(bstr, -4);
// Allocate a byte array to copy the string into
byte[] bytes = new byte[length];
// Copy the BSTR to the byte array
Marshal.Copy(bstr, bytes, 0, length);
// Immediately destroy …Run Code Online (Sandbox Code Playgroud) 我怎样才能获得byte[]相当于a SecureString(我从中得到的PasswordBox)?
我的目标是使用a将这些字节写入CryptoStream文件,并且Write该类的方法接受byte[]输入,因此我想将其转换SecureString为byte[]所以我可以使用a CryptoStream.
编辑:我不想使用,string因为它打败了一个点SecureString