Idh
Idh

Reputation: 887

How to convert a string to securestring explicitly

I want the text entered in the textbox to be converted to securestring in c#.

Upvotes: 51

Views: 82157

Answers (7)

Szybki
Szybki

Reputation: 1111

I'm surprised nobody mentioned about SecureString constructor taking pointer to char array.

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 /unsafe compiler option. To set this option go to project properties, Build tab and check Allow unsafe code checkbox.

Upvotes: 2

jr_gen
jr_gen

Reputation: 63

Nearly the same, but shorter:

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

Upvotes: 1

Colin Gardner
Colin Gardner

Reputation: 531

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();

Upvotes: 34

Balazs Tihanyi
Balazs Tihanyi

Reputation: 6719

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

Upvotes: 89

ParsaG72
ParsaG72

Reputation: 29

It may be a little late but you can convert SecureString to String this way either

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:

Google is your friend...

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

Upvotes: 1

MovGP0
MovGP0

Reputation: 7765

You should make the SecureString readonly. So the code should look like this:

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

Upvotes: 18

superKing
superKing

Reputation: 89

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

Upvotes: -1

Related Questions