ASP.NET - image encrypt - Asked By msakt on 28-May-12 12:46 AM

i am using this fun for image encryption then save in sql....here how can pass image from this even to encryptbytes fun and how to save in sql
protected void bUpload_Click(object sender, EventArgs e)
    {
      fupBill.SaveAs(Server.MapPath(@"Upload\UploadBill\" + fupBill.FileName));
}



public static byte[] EncryptBytes(byte[] inputBytes, string passPhrase, string saltValue)
   
{
       
RijndaelManaged RijndaelCipher = new RijndaelManaged();

       
RijndaelCipher.Mode = CipherMode.CBC;
       
byte[] salt = Encoding.ASCII.GetBytes(saltValue);
       
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);

       
ICryptoTransform Encryptor = RijndaelCipher.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[] CipherBytes = memoryStream.ToArray();

        memoryStream
.Close();
        cryptoStream
.Close();

       
return CipherBytes;
   
}
Jitendra Faye replied to msakt on 28-May-12 01:00 AM
Now, To store image use this code


byte[] imageSize =  return value(bytes) from EncryptBytes() method

HttpPostedFile uploadedImage = FileUpload1.PostedFile;

uploadedImage.InputStream.Read(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);

 

SqlConnection cn = new SqlConnection("YOUR CONNECTION STRING");

cn.Open();

string strQuery = "insert into EmpTable(empimage) values(@ImageData)";

SqlCommand cmd = new SqlCommand(strQuery, cn);

cmd.Parameters.Add(new SqlParameter("@ImageData", (object)imageSize));

cmd.ExecuteNonQuery();

cmd.ExecuteNonQuery();

cn.Close();


TRy this and let me know.