prasad M

prasad M

  • NA
  • 1
  • 1.9k

encryption and decryption in c#

May 19 2013 3:44 AM
it's work only for text file can any help me how to do it for all kind of files ex: pdf, word etc
the below code encoding all files but while in decrypting it's decoded to some char
 
plz help me............
 

protected void FileUpload(int size,string CntType)

conn.Open();
SqlCommand cmd = new SqlCommand("insert into FileDetails values('" + File1.FileName + "','" + CntType + "','" + size + "','" + DateTime.Now + "')", conn);
cmd.ExecuteNonQuery();
cmd = new SqlCommand("select max(FileID) from FileDetails", conn);
using (SqlDataReader dr = cmd.ExecuteReader())
if (dr.Read()) 
{
lstFile.Items.Clear();
lstFile.Items.Add(" \t Keyword Encryption ----> ");
lstFile.Items.Add("----------------------------------------------------");
string[] str=txtKeyword.Text.Split(',');
foreach(string word in str)
{
string strEncrypt = Homomorphic.DesEncrypt(word.ToLower(), true);
lstFile.Items.Add(" \t " + strEncrypt);
SqlCommand cmdindex = new SqlCommand("insert into Indexing values(" + dr.GetValue(0) + ",'" + strEncrypt + "')", conn);
cmdindex.ExecuteNonQuery();
}
dr.Close();
lstFile.Items.Add("------------------------------------------------------------");
}
}
protected void btnUpload_Click(object sender, System.EventArgs e)
{
try
{
for(int i=0;i<request.files.count;i++)>
{
if(Request.Files[i].FileName.Trim().Length >0) 
{
HttpPostedFile file=Request.Files[i];
if(file!=null && file.FileName.Length>0)
{
string strmsg = string.Empty;
byte[] fileData = new byte[file.ContentLength];
file.InputStream.Read(fileData, 0, file.ContentLength);
strmsg = System.Text.Encoding.UTF8.GetString(fileData); 
string ps = Homomorphic.DesEncrypt(strmsg.ToLower(),true); 
using (StreamWriter writer = new StreamWriter(Path.Combine(GetCurDir(), Path.GetFileName(file.FileName)), true))
{
writer.WriteLine(ps);
writer.Flush();
}
FileUpload(file.ContentLength,file.ContentType);
Response.Write("<script language='javascript'>alert('Encrypted & Uploaded !')</script>");
lstFile.Items.Add(" \t File Encryption ----> ");
lstFile.Items.Add("------------------------------------------------------");
lstFile.Items.Add(" \t " + ps);
}
}
}
BindData();
}
catch(Exception ex)
{
lblError.Text=ex.Message;
}
}