ASP.NET - image encryption and stor to database

Asked By msakt on 28-May-12 12:18 AM
i am using file i browse image and encrypt this image then save in sql server..


Jitendra Faye replied to msakt on 28-May-12 12:24 AM
Images are stored in  sql server in the format of Byte array, so you need  to encrypt byte of array.

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 28-May-12 12:29 AM

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