Click here to Skip to main content
15,886,847 members

encrypting and decrypting file in c#

Member 10017812 asked:

Open original thread
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............

C#
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;
       }
}
Tags: C#, ASP.NET, Encryption

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900