How to convert a SecureString into an encrypted String in a secure manner?



I'm designing a system for Windows initiated Single Sign-On against
RACF.

I keep my RACF-passwords in fields of the new .net 2.0 type
System.Security.SecureString. I need to store these passwords in a SQL
server 2005 database between user sessions. Hence, I need to convert
the SecureString into an encrypted string.

I could of course convert the SecureString into a string before
encryption, but this would compromise the security of the system.

My suggestion is to read the bytes of the SecureString byte by byte,
writing the each byte directly into a CryptoStream like this:

private static string SecurePassword2EncryptedPassword(SecureString
password)
{

SymmetricAlgorithm cryptoAlg = GetCryptoAlg();
ICryptoTransform encryptor = cryptoAlg.CreateEncryptor();

MemoryStream outStream = new MemoryStream();
using (CryptoStream encryptStream = new CryptoStream(outStream,
encryptor, CryptoStreamMode.Write))
{

IntPtr bstr = Marshal.SecureStringToBSTR(password);

try
{
byte b;
for (int ofset = 0; ofset < password.Length * 2;
ofset = ofset + 2)
{
b = Marshal.ReadByte(bstr, ofset);
encryptStream.WriteByte(b);

}
b = 0;

encryptStream.FlushFinalBlock();

}
finally
{
Marshal.ZeroFreeBSTR(bstr);
}


return Convert.ToBase64String(outStream.ToArray());

}
}

Is my way, the secure way to converte a SecureString into an encrypted
string? Or should I do something else?

Best regards

Michael Brandt Lassen
3F, Denmark

.



Relevant Pages

  • Convert a SecureString into an encrypted String in a secure manner
    ... SecureString into an encrypted string. ... the each byte directly into a CryptoStream like this: ... ICryptoTransform encryptor = cryptoAlg.CreateEncryptor; ...
    (microsoft.public.platformsdk.security)
  • Re: Length of the data to decrypt is invalid
    ... I may be misunderstanding your code, but it looks like your decrypt function ... converting that back to binary use ASCII! ... You would then take the decrypted data and convert that back to a string ... ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Best practice SecureString and pswd collection
    ... because ConsoleKeyInfo just keeps one char in it's structure. ... There is never a string exposed. ... SecureString password = new SecureString; ...
    (microsoft.public.dotnet.security)
  • Re: Destroy a string
    ... public static unsafe void OverwriteString{ ... The string is still on the heap, ... That's why the SecureString was invented, it get's allocated in a non swappable fixed ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Length of the data to decrypt is invalid
    ... In my case it is different, I just store the encrypted string in a Session ... /// Summary description for MyEncryption ... ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, ... ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, ...
    (microsoft.public.dotnet.framework.aspnet.security)