有关创建以System.String出它不保护你的SecureString的所有预订一边,怎么能做到呢?
如何将普通的System.Security.SecureString转换为System.String?
我相信很多熟悉SecureString的人会回应说,永远不应该将SecureString转换为普通的.NET字符串,因为它会删除所有安全保护. 我知道.但是现在我的程序用普通字符串完成所有操作,我正在尝试增强其安全性,虽然我将使用返回SecureString的API给我,但我并不是想用它来增加我的安全性.
我知道Marshal.SecureStringToBSTR,但我不知道如何取出BSTR并从中制作System.String.
对于那些可能要求知道我为什么要这样做的人,好吧,我正在从用户那里获取密码并将其作为html表单POST提交以将用户登录到网站.所以......这真的必须使用托管的,未加密的缓冲区.如果我甚至可以访问非托管的,未加密的缓冲区,我想我可以在网络流上进行逐字节流写入,并希望这样可以保证密码的安全性.我希望能够回答至少其中一种情况.
我似乎无法找到使用AES 128位加密的一个很好的清洁示例.
有没有人有一些示例代码?
是否可以解密通过PowerShell加密的C#中的字符串以及如何?
该字符串通过PowerShell加密,如下所示:
$pw = read-host "Enter Password" –AsSecureString
ConvertFrom-SecureString $pw | out-file "C:\file.txt"
Run Code Online (Sandbox Code Playgroud)
要使用PowerShell将其转换回来,我可以使用这些调用C#类的命令System.Runtime.InteropServices.Marshal
.
$pwdSec = Get-Content "C:\file.txt" | ConvertTo-SecureString
$bPswd = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwdSec)
$pswd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bPswd)
Run Code Online (Sandbox Code Playgroud)
文件包含已转换为加密标准的字符串string("hello")
.
因此,如果打开file.txt
文件,它看起来类似于:
01000000d08c9ddf0115d1118c7a00c04fc297eb0100000052ded6c2db80e748933432e19b9de8b10000
000002000000000003660000c00000001000000016dc35885d76d07bab289eb9927cfc1e000000000480
0000a0000000100000003106cde553f45b08d13d89d11336170b280000005cc865c1ee1b57e84ed3d1a2
d3f2d0ec0f189b532e61c18d1f31444d6f119a1e8368477fd2d81f54140000000cb0262e58b08ae14f37
22c14c69684841b6b21c
Run Code Online (Sandbox Code Playgroud) c# ×5
.net ×3
encryption ×2
securestring ×2
security ×2
aes ×1
arrays ×1
cryptography ×1
hex ×1
powershell ×1