The second code sample that "Vickey" proposed is not relevant for a byte array.
Since, the file upload already gives you a byte array, encrypt the array and store it in the byte array form in an Imagefield. When you need to display it on the web-forms, read the field and then decrypt it and then show it.
Here's a sample security helper class that encrypts and decrypts a byte array using the Rijndael classes. Feel free to modify if it does not suit your needs.
Add references to the System.Security.Cryptography assembly and namespaces in your class
public class SecurityHelper
{
RijndaelManaged rindaelCipherObject = new RijndaelManaged();
const string SHA1 = "SHA1";
SecurityHelper()
{
rindaelCipherObject.Mode = CipherMode.CBC;
}
public static byte[] EncryptByteArray(byte[] inputBytes, string passPhrase, string saltValue)
{
byte[] salt = Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);
ICryptoTransform Encryptor = rindaelCipherObject.CreateEncryptor(password.GetBytes(32), password.GetBytes(16));
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
cryptoStream.Write(inputBytes, 0, inputBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] outputBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
return outputBytes;
}
public static byte[] DecryptByteArray(byte[] inputBytes, string passPhrase, string saltValue)
{
byte[] salt = Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, SHA1 , 2);
ICryptoTransform cryptoDecryptor= rindaelCipherObject.CreateDecryptor(password.GetBytes(32), password.GetBytes(16));
MemoryStream memoryStream = new MemoryStream(inputBytes);
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoDecryptor, CryptoStreamMode.Read))
{
byte[] unEncryptedBytes = new byte[inputBytes.Length];
int DecryptedCount = cryptoStream.Read(unEncryptedBytes, 0, unEncryptedBytes.Length);
memoryStream.Close();
}
return unEncryptedBytes;
}
}
As an alternate and viable option, look at securing data in the databases. Note that databases are always faster than you web servers off late. SQL Server allows you to encrypt columns of data using Symmetric algorithms using the EncryptByKey function. Look at http://msdn.microsoft.com/en-us/library/ms179331(v=SQL.90).aspx , scroll down to the section titled "A. Simple symmetric encryption" for a simple example on encrypting a column of data.
This would be the syntax:
UPDATE tablename
SET newEncryptedColumn = EncryptByKey(Key_GUID('key name'), existingColumn);
And when you want to decrypt:
SELECT DecryptByKey(newEncryptedColumn , 1)
Alternatively, in my opinion, you should use secure connections to upload files, and not invest on encryption and decryption of files. This hurts performance.
Hope this helps.