C# 如何将字符串显式转换为安全字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9887996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a string to securestring explicitly
提问by Indish Cholleti
I want the text entered in the textbox to be converted to securestring in c#.
我希望在文本框中输入的文本在 c# 中转换为安全字符串。
采纳答案by Balazs Tihanyi
The simplest approach is to iterate over the source string and append one character at a time to the secure string, like so:
最简单的方法是遍历源字符串并一次向安全字符串附加一个字符,如下所示:
var secure = new SecureString();
foreach (char c in textbox1.Text)
{
secure.AppendChar(c);
}
回答by Colin Gardner
Invent once and reuse lots. Create a simple extension method to extend the string base class and store it some static utilities class somewhere
发明一次并重复使用批次。创建一个简单的扩展方法来扩展字符串基类并将它的一些静态实用程序类存储在某处
using System.Security;
/// <summary>
/// Returns a Secure string from the source string
/// </summary>
/// <param name="Source"></param>
/// <returns></returns>
public static SecureString ToSecureString(this string source)
{
if (string.IsNullOrWhiteSpace(source))
return null;
else
{
SecureString result = new SecureString();
foreach (char c in source.ToCharArray())
result.AppendChar(c);
return result;
}
}
and then call as follows:
然后调用如下:
textbox1.Text.ToSecureString();
回答by superKing
use this for the event handler on your text box:
将此用于文本框上的事件处理程序:
private void textbox1_TextChanged(object sender, EventArgs e)
{
SecureString newSecureTextBox = new SecureString();
foreach (char c in textbox1.Text.ToCharArray())
{
newSecureTextBox.AppendChar(c);
}
}
call your variable normally as newSecureTextBox
通常将您的变量称为 newSecureTextBox
回答by MovGP0
You should make the SecureString readonly. So the code should look like this:
您应该将 SecureString 设为只读。所以代码应该是这样的:
static class SecureStringExtensions
{
public static string ToUnsecureString(this SecureString secureString)
{
if (secureString == null) throw new ArgumentNullException("secureString");
var unmanagedString = IntPtr.Zero;
try
{
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString);
return Marshal.PtrToStringUni(unmanagedString);
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
}
}
public static SecureString ToSecureString(this string unsecureString)
{
if (unsecureString == null) throw new ArgumentNullException("unsecureString");
return unsecureString.Aggregate(new SecureString(), AppendChar, MakeReadOnly);
}
private static SecureString MakeReadOnly(SecureString ss)
{
ss.MakeReadOnly();
return ss;
}
private static SecureString AppendChar(SecureString ss, char c)
{
ss.AppendChar(c);
return ss;
}
}
回答by ParsaG72
It may be a little late but you can convert SecureString to String this way either
可能有点晚了,但您可以通过这种方式将 SecureString 转换为 String
using System.Security;
.
.
.
/// <summary>
/// Converts String to SecureString
/// </summary>
/// <param name="input">Input in String</param>
/// <returns>Input in SecureString</returns>
public SecureString String2SecureString(String input) {
SecureString _output = new SecureString();
input.ToCharArray().ToList().ForEach((q) => _output.AppendChar(q));
return _output;
}
although it all the same as Balazs Tihanyi's answer:
尽管这与 Balazs Tihanyi 的回答完全相同:
Google is your friend...
var secure = new SecureString(); foreach(char c in textbox1.Text) { secure.AppendChar(c); }
谷歌是你的朋友...
var secure = new SecureString(); foreach(char c in textbox1.Text) { secure.AppendChar(c); }
回答by Szybki
I'm supprised nobody metioned about SecureString constructor taking pointer to char array.
我很惊讶没有人提到 SecureString 构造函数采用指向 char 数组的指针。
public static SecureString ToSecureString(this string source)
{
char[] charArray = source.ToCharArray();
unsafe
{
fixed (char* chars = charArray)
{
return new SecureString(chars, charArray.Length);
}
}
}
Note that this code only works with /unsafecompiler option. To set this option go to project properties, Build tab and check Allow unsafe code checkbox.
请注意,此代码仅适用于/unsafe编译器选项。要设置此选项,请转到项目属性、构建选项卡并选中允许不安全代码复选框。
回答by jr_gen
Nearly the same, but shorter:
几乎相同,但更短:
SecureString secureString = new SecureString();
textbox1.Text.ToCharArray().ForEach(c => secureString.AppendChar(c));

