Hi RichardSa,
Note:For this sample i have refer below article for encrypt. For more details refer below article.
You need to pass VARCHAR(MAX) in your database for image.
SQL
CREATE TABLE [Encrypt_Image]
(
[Id] INT IDENTITY (1,1)
,[Name] VARCHAR(50)
,[Image] VARCHAR(MAX) Null
)
SELECT * FROM [Encrypt_Image]
DROP TABLE [Encrypt_Image]
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<hr />
<asp:TextBox runat="server" ID="txtName"></asp:TextBox><br /><br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="OnSubmit"></asp:Button>
Namespaces
C#
using System.IO;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Security.Cryptography;
VB.Net
Imports System.IO
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Security.Cryptography
Code
C#
protected void OnSubmit(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
byte[] image;
using (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream))
{
image = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
}
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "INSERT INTO Encrypt_Image (Name,Image) VALUES (@Name,@Image)";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@image",this.Encrypt(Convert.ToBase64String(image)));
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
private string Encrypt(string clearText)
{
string encryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
VB.Net
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
Dim name As String = txtName.Text.Trim()
Dim image As Byte()
Using br As BinaryReader = New BinaryReader(FileUpload1.PostedFile.InputStream)
image = br.ReadBytes(FileUpload1.PostedFile.ContentLength)
End Using
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "INSERT INTO Encrypt_Image (Name,Image) VALUES (@Name,@Image)"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@image", Me.Encrypt(Convert.ToBase64String(image)))
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Sub
Private Function Encrypt(ByVal clearText As String) As String
Dim encryptionKey As String = "MAKV2SPBNI99212"
Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText)
Using encryptor As Aes = Aes.Create()
Dim pdb As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(encryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using ms As MemoryStream = New MemoryStream()
Using cs As CryptoStream = New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
cs.Write(clearBytes, 0, clearBytes.Length)
cs.Close()
End Using
clearText = Convert.ToBase64String(ms.ToArray())
End Using
End Using
Return clearText
End Function
Screenshot
Output