Hi Nick,
Sorry for the misunderstanding.
In order to use a password to encrypt a block of text, you can use the way
you mentioned in your original post. And in order to prevent dictionary
attack, you may want to add salt to the password hashing to make such
attacks harder.
Since you didn't mention the version of .NET Framework you're targeting,
I'm using the RijndaelManaged class instead of AesManaged class (new in
3.5) to make a demo:
private string EncryptText(string password, string salt, string plainText)
{
int blockSize = 128; // Block size in bits.
int keySize = 256;
SHA256Managed sha = new SHA256Managed(); // key size is 256
byte[] key = sha.ComputeHash(Encoding.UTF8.GetBytes(salt + password));
byte[] iv = new byte[blockSize / 8]; // The IV size should be the
same as blockSize.
Array.Copy(sha.ComputeHash(Encoding.UTF8.GetBytes(password + salt)),
iv, iv.Length);
RijndaelManaged rijndael = null;
MemoryStream msEncrypt = null;
CryptoStream csEncrypt = null;
StreamWriter swEncrypt = null;
try
{
rijndael = new RijndaelManaged();
rijndael.BlockSize = blockSize;
rijndael.KeySize = keySize;
rijndael.Key = key;
rijndael.IV = iv;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = rijndael.CreateEncryptor();
// Create the streams used for encryption.
msEncrypt = new MemoryStream();
csEncrypt = new CryptoStream(msEncrypt, encryptor,
CryptoStreamMode.Write);
swEncrypt = new StreamWriter(csEncrypt);
//Write all data to the stream.
swEncrypt.Write(plainText);
}
finally
{
if (swEncrypt != null)
swEncrypt.Close();
if (csEncrypt != null)
csEncrypt.Close();
if (msEncrypt != null)
msEncrypt.Close();
// Clear the RijndaelManaged object.
if (rijndael != null)
rijndael.Clear();
}
// Get the encrypted bytes from the memory stream and convert to base64
text.
return Convert.ToBase64String(msEncrypt.ToArray());
}
private string DecryptText(string password, string salt, string
encryptedText)
{
int blockSize = 128; // Block size in bits.
int keySize = 256;
SHA256Managed sha = new SHA256Managed(); // key size is 256 bits
byte[] key = sha.ComputeHash(Encoding.UTF8.GetBytes(salt + password));
byte[] iv = new byte[blockSize / 8]; // The IV size should be the
same as blockSize.
Array.Copy(sha.ComputeHash(Encoding.UTF8.GetBytes(password + salt)),
iv, iv.Length);
RijndaelManaged rijndael = null;
MemoryStream msEncrypt = null;
CryptoStream csEncrypt = null;
StreamReader srDecrypt = null;
try
{
rijndael = new RijndaelManaged();
rijndael.BlockSize = blockSize;
rijndael.KeySize = keySize;
rijndael.Key = key;
rijndael.IV = iv;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = rijndael.CreateDecryptor();
// Create the streams used for decryption.
msEncrypt = new
MemoryStream(Convert.FromBase64String(encryptedText));
csEncrypt = new CryptoStream(msEncrypt, encryptor,
CryptoStreamMode.Read);
srDecrypt = new StreamReader(csEncrypt);
// Read the decrypted bytes from the decrypting stream
return srDecrypt.ReadToEnd();
}
finally
{
if (srDecrypt != null)
srDecrypt.Close();
if (csEncrypt != null)
csEncrypt.Close();
if (msEncrypt != null)
msEncrypt.Close();
// Clear the RijndaelManaged object.
if (rijndael != null)
rijndael.Clear();
}
}
Please kindly let me know if the code above helps.
Thanks,
Jie Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.