在这个问题中, Erik需要在Node.js中生成一个安全的随机令牌.有crypto.randomBytes生成随机缓冲区的方法.但是,节点中的base64编码不是url-safe,它包含/而+不是-和_.因此,生成此类令牌的最简单方法是我发现的
require('crypto').randomBytes(48, function(ex, buf) {
token = buf.toString('base64').replace(/\//g,'_').replace(/\+/g,'-');
});
Run Code Online (Sandbox Code Playgroud)
有更优雅的方式吗?
我正在使用power shell,我有成功将用户输入的密码转换为纯文本的代码:
$SecurePassword = Read-Host -AsSecureString "Enter password" | convertfrom-securestring | out-file C:\Users\tmarsh\Documents\securePassword.txt
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几种方法将其转换回来,但它们似乎都没有正常工作.最近,我尝试过以下内容:
$PlainPassword = Get-Content C:\Users\tmarsh\Documents\securePassword.txt
#convert the SecureString object to plain text using PtrToString and SecureStringToBSTR
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($PlainPassword)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR) #this is an important step to keep things secure
Run Code Online (Sandbox Code Playgroud)
这也给我一个错误.
Cannot convert argument "s", with value: "01000000d08c9ddf0115d1118c7a00c04fc297eb0100000026a5b6067d53fd43801a9ef3f8ef9e43000000000200000000000366000
0c0000000100000008118fdea02bfb57d0dda41f9748a05f10000000004800000a000000010000000c50f5093f3b87fbf9ee57cbd17267e0a10000000833d1d712cef01497872a3457bc8
bc271400000038c731cb8c47219399e4265515e9569438d8e8ed", for "SecureStringToBSTR" to type "System.Security.SecureString": "Cannot convert the "01000000
d08c9ddf0115d1118c7a00c04fc297eb0100000026a5b6067d53fd43801a9ef3f8ef9e430000000002000000000003660000c0000000100000008118fdea02bfb57d0dda41f9748a05f10
000000004800000a000000010000000c50f5093f3b87fbf9ee57cbd17267e0a10000000833d1d712cef01497872a3457bc8bc271400000038c731cb8c47219399e4265515e9569438d8e8
ed" value of type "System.String" to type "System.Security.SecureString"."
At C:\Users\tmarsh\Documents\Scripts\Local Admin Script\PlainTextConverter1.ps1:14 char:1
+ …Run Code Online (Sandbox Code Playgroud) 我希望在文本框中输入的文本在c#中转换为securestring,感谢您提前回答.
这可以简化为一个班轮吗?只要secureString被正确初始化,就可以完全重写它.
SecureString secureString = new SecureString ();
foreach (char c in "fizzbuzz".ToCharArray())
{
secureString.AppendChar (c);
}
Run Code Online (Sandbox Code Playgroud) 如果我理解正确,这是为了保持纯文本内存不足,以便应用程序可以安全地防止对内存,垃圾堆或分页到磁盘的内存的深奥攻击.SecureString被提供非托管字节,并在时间消耗一个非托管字节 - 然后字符串从内存中删除.(如果我离开,请纠正我!)
在ASP.NET中,秘密收集在webform中,后者以HTTPS格式发回.但随后Request对象变成从表单中的所有请求的值到名称值对,并将它们集合中,如请求["TxtPassword" - 所以我之前也可以得到字符串,它已经被不安全写入存储器.更糟糕的是,如果我使用的是控件,那么不安全的表示将在TextBox的属性中拥有更多的托管字符串.
要对此SecureString执行任何操作,我需要一个采用非托管字符串的API - 因此我似乎无法使用安全字符串来存储proc参数或其他内容.
我这样做是错误的还是尝试使用SecureString并且不将不安全字符串的副本泄漏到托管内存中这是一个愚蠢的错误?
切换到OAuth或Windows身份验证不是一种选择.
根据MSDN SecureString内容被加密以增加安全性,因此如果程序被交换到磁盘,则不能嗅探字符串内容.
我想知道这样的加密怎么样?算法是固定的,因此要么是熟知的,要么是免赔额的(比如工业算法中广泛使用的七种算法之一),并且程序中必须有一个密钥.因此,攻击者可以获取加密的字符串,获取密钥并解密数据.
这种加密如何有用?
我有一个带有两个PasswordBox的WPF应用程序,一个用于密码,另一个用于第二次输入密码以进行确认.我想用来PasswordBox.SecurePassword获取SecureString密码,但我需要能够比较两个PasswordBoxes的内容以确保在我接受密码之前的相等性.但是,两个相同的SecureStrings不相同:
var secString1 = new SecureString();
var secString2 = new SecureString();
foreach (char c in "testing")
{
secString1.AppendChar(c);
secString2.AppendChar(c);
}
Assert.AreEqual(secString1, secString2); // This fails
Run Code Online (Sandbox Code Playgroud)
我在想比较PasswordPasswordBoxes 的属性会破坏访问点,SecurePassword因为我正在阅读纯文本密码.如何在不牺牲安全性的情况下比较两个密码?
编辑:根据这个问题,我正在查看这篇关于"使用Marshal类将SecureString转换为ANSI或Unicode或BSTR"的博文,然后我可以比较一下.
我需要使用SecureString微软的课程,我在互联网上找到以下代码:
public static class SecureStringExt
{
public static SecureString ConvertToSecureString(this string password)
{
if (password == null)
throw new ArgumentNullException("password");
unsafe //Red highlighted line
{
fixed (char* passwordChars = password)
{
var securePassword = new SecureString(passwordChars, password.Length);
securePassword.MakeReadOnly();
return securePassword;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是unsafe关键字一直让我错误说Cannot use unsafe construct in safe context.不幸的是我找不到为什么会这样......
注意: 上面的代码在LINQPad中运行,但在VS2013中没有(使用resharper).
有没有办法在不包含安全性的情况下获取SecureString的值?例如,在下面的代码中,只要执行PtrToStringBSTR,字符串就不再安全,因为字符串是不可变的,垃圾收集对于字符串是不确定的.
IntPtr ptr = Marshal.SecureStringToBSTR(SecureString object);
string value = Marshal.PtrToStringBSTR(ptr);
Run Code Online (Sandbox Code Playgroud)
如果有一种方法可以获得非托管BSTR字符串的char []或byte [],该怎么办?这是否意味着垃圾收集更可预测(因为你将使用char []或byte []而不是字符串?这个假设是正确的,如果是这样,你将如何得到char []或byte []?
securestring ×10
c# ×8
.net ×3
security ×3
asp.net ×1
base64 ×1
encryption ×1
equals ×1
javascript ×1
node.js ×1
passwordbox ×1
passwords ×1
powershell ×1
unsafe ×1