I have a need to take sensitive information collected from a WPF PasswordBox
. After the user enters this data, it needs to be encrypted using a System.Security.Cryptography.Rijendael
encryptor object.
Currently, this code that is being modified takes a "plaintext" string and encrypts it and using the following code:
using (var rijAlg = Rijndael.Create())
{
var salt = ... //Generated Salt
rijAlg.KeySize = CryptographyHelper.ENCRYPTION_KEYSIZE;
rijAlg.Key = encryptionKey; //This is an encryption key safely derived elsewhere.
rijAlg.IV = salt;
var encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
using (var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText); //Plain Text value.
}
encrypted = msEncrypt.ToArray();
}
}
}
The value plainText
above is actually a value that is passed into this encryption method. At the end of the method, the encrypted
value is passed into Convert.ToBase64String(...)
and the data is converted to Base64 and return from the encryption method.
My question is, how can I modify the above code to take a SecureString
object representing the value that needs to be encrypted, securely encrypt the associated value, clean up the associated data and return the encrypted data as a string, just as I am the plainText
value? Keep in mind, I'll need to derive a follow-up Decrypt(...)
method which I hope can return a SecureString
object, but figuring that out can be another question.