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



This may or may not be a good way to do this. It depends a lot on whether
you use a single symmetric key or multiple different keys, how you store the
keys and what you plan to do about a real random IV. It also depends a bit
on the algorithm used and the key length. It also depends on what the
threat models are.

If you are really concerned about what would happen if someone was able to
get the encrypted data and wanted to recover your passwords, you would
probably want to improve this. For example, if you use a single symmetric
key, recovery of that key will provide access to all of the passwords. If
you don't use a random IV, duplicate passwords will produce duplicate cipher
text which increases vunerability. It also makes the cipher text more
generally vulnerable to brute force attacks that could compromise the key.

In general, it is better to avoid storing encrypted passwords and store
hashes instead, but I'm guessing your system requires you to recover the
plaintext password, so you probably don't have a choice but to encrypt them.
You can probably make this significantly more secure though.

SQL Server 2005 has some built in ability to encrypt stored data, so it
might not be a bad idea to look into those features and try to leverage the
platform. I don't know much about how it works though.

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
<spam@xxxxxxxxxxxxxxxx> wrote in message
news:1188297357.314678.296800@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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

  • Re: Security package for an individual in a hostile country
    ... Note that any advice you get here is given by armchair generals. ... Used to encrypt and/or hide your files. ... I'm assuming that the hostile government ... This program saves your passwords and has a ...
    (sci.crypt)
  • Re: Create hash with AES?
    ... > files, but also email, and text such as passwords... ... the AES is the most sensible choice. ... However if you're going to encrypt government data with it, ... would be wise to use a public/private key pair on a smart card. ...
    (microsoft.public.dotnet.security)
  • Re: Security package for an individual in a hostile country
    ... Used to encrypt and/or hide your files. ... I'm assuming that the hostile government ... I believe there are Firefox extensions that ensures the Google Mail ... This program saves your passwords and has a ...
    (sci.crypt)
  • Re: Security package for an individual in a hostile country
    ... Used to encrypt and/or hide your files. ... I'm assuming that the hostile government ... I believe there are Firefox extensions that ensures the Google Mail ... This program saves your passwords and has a ...
    (sci.crypt)
  • Re: How to convert a SecureString into an encrypted String in a se
    ... secure string and into a byte array looked a little weird to me, ... You can then either convert that to a .NET string (not a good idea if the ... Note that you can encrypt your SQL network traffic on the wire if you are ... planning to encrypt the passwords using the symmetric Rijndael algorithm, ...
    (microsoft.public.dotnet.security)