如何显式地将字符串转换为securestring

时间:2012-03-27 10:42:11

标签: c# securestring

我希望在文本框中输入的文本在c#中转换为securestring。

7 个答案:

答案 0 :(得分:80)

最简单的方法是遍历源字符串并一次将一个字符附加到安全字符串,如下所示:

var secure = new SecureString();
foreach (char c in textbox1.Text)
{
    secure.AppendChar(c);
}

答案 1 :(得分:28)

发明一次并重复使用批次。创建一个简单的扩展方法来扩展字符串基类并在某处存储一些静态实用程序类

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;
    }
}

然后调用如下:

textbox1.Text.ToSecureString();

答案 2 :(得分:17)

您应该只读取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;
    }
}

答案 3 :(得分:2)

可能有点晚了但你可以用这种方式将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;
}

虽然它与Balazs Tihanyi的答案完全相同:

  

Google是你的朋友......

var secure = new SecureString(); 
foreach(char c in textbox1.Text) 
{
secure.AppendChar(c); 
}

答案 4 :(得分:0)

我没有提到任何人提到SecureString构造函数指向char数组。

private void Button_Click(object sender, RoutedEventArgs e)
{
    var sourceCollection = Persons.ItemsSource as ObservableCollection<Person>;
    if (sourceCollection != null)
    {
        sourceCollection.Add(new Man() { Name = "..." });
    }
}

请注意,此代码仅适用于public static SecureString ToSecureString(this string source) { char[] charArray = source.ToCharArray(); unsafe { fixed (char* chars = charArray) { return new SecureString(chars, charArray.Length); } } } 编译器选项。要设置此选项,请转到项目属性,构建选项卡,然后选中允许不安全代码复选框。

答案 5 :(得分:0)

几乎相同,但更短:

SecureString secureString = new SecureString();
textbox1.Text.ToCharArray().ForEach(c => secureString.AppendChar(c));

答案 6 :(得分:-1)

将其用于文本框中的事件处理程序:

private void textbox1_TextChanged(object sender, EventArgs e)
    {
    SecureString newSecureTextBox = new SecureString();
        foreach (char c in textbox1.Text.ToCharArray())
        {
        newSecureTextBox.AppendChar(c);
        }
    }

通常将您的变量称为newSecureTextBox