ASP.NET - image encryption and stor to database

Asked By msakt on 25-May-12 09:47 AM
fileupload option.. here image upload and encrypt then store to ont use folder for image..

how to encrypt image and save to sql
Neha Garg replied to msakt on 25-May-12 04:13 PM
Hiii   msakt...

See the code to Encrypt and decrypt the image file and then store the file into SQL DB..

Download the codes from...
LIJO PHILIP replied to msakt on 25-May-12 10:57 PM

Just read in the image into the byte array, encrypt the array, and save it to the database.

Jitendra Faye replied to msakt on 26-May-12 12:29 AM
There is no special work for this because Images are stored in  sql server in the format of Byte array, so you can not directly see those images.

Even then if you want to perform encryption then after getting image to byte array encrypt that byte array and save into database

for this use this function-

// Encrypt a byte array into a byte array using a key and an IV
    public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
      MemoryStream ms = new MemoryStream();

      Rijndael alg = Rijndael.Create();

      alg.Key = Key;
      alg.IV = IV;

      CryptoStream cs = new CryptoStream(ms,
       alg.CreateEncryptor(), CryptoStreamMode.Write);

      cs.Write(clearData, 0, clearData.Length);


      byte[] encryptedData = ms.ToArray();

      return encryptedData;

 // Decrypt a string into a string using a password

    public static string Decrypt(string cipherText, string Password)
      byte[] cipherBytes = Convert.FromBase64String(cipherText);
      PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
        new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65,
        0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

      byte[] decryptedData = Decrypt(cipherBytes,
        pdb.GetBytes(32), pdb.GetBytes(16));

      return System.Text.Encoding.Unicode.GetString(decryptedData);

TRy this and let em know.
Somesh Yadav replied to msakt on 26-May-12 03:26 AM

.net comes with cryptography in the System.Security.Cryptography namespace. The examples on this page are helpful:

[)ia6l0 iii replied to msakt on 30-May-12 10:16 AM
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";
        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);
        byte[] outputBytes = memoryStream.ToArray();
        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);
        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 , 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.
RAJASEKHAR RAJENDRAN replied to msakt on 04-Jun-12 07:10 AM
Hi msakt,

Try the code below.

public byte[] EncryptStream(byte[] input)
  RijndaelManaged rijn = new RijndaelManaged();
  byte[] encrypted = null;
  byte[] key = new byte[] {
  byte[] IV = new byte[] {
  //Get an encryptor.
  ICryptoTransform encryptor = rijn.CreateEncryptor(key, IV);
  //Encrypt the data.
  MemoryStream msEncrypt = new MemoryStream();
  CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
  //Write all data to the crypto stream and flush it.
  csEncrypt.Write(input, 0, input.Length);
  //Get encrypted array of bytes.
  encrypted = msEncrypt.ToArray();
  return encrypted;

Try and let me know.

Thanks & Regards,