Tony and Elise's Blog

All things Family, and some tech/dev stuff here and there.
posts - 29, comments - 7, trackbacks - 263

Sunday, December 21, 2008

String/SecureString Extension

Wanting to make my applications more secure, and wanting to make it as simple as possible, I decided to use a couple extension methods to helps me manage working with the SecureString type.

public static class StringExtensions

{

    public static SecureString ToSecureString(this string source)

    {

        SecureString secureString = new SecureString();

        Array.ForEach(source.ToCharArray(), s => secureString.AppendChar(s));

        return secureString;

    }

 

    public static string ToUnSecureString(this SecureString source)

    {

        IntPtr pwd = Marshal.SecureStringToBSTR(source);

        return Marshal.PtrToStringBSTR(pwd);

    }

}

Hope this helps.

posted @ Sunday, December 21, 2008 11:21 PM | Feedback (20) |

Powered by: