Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
       }
}
Posted
Updated 18-May-13 23:47pm
v2
Comments
OriginalGriff 19-May-13 3:49am    
If you are going to post code blocks for us to look at, then at least be polite enough to post the formatted code, even if you don't use code blocks or pre tags to preserve the formatting - we can add those if we need to, but I'm not sitting here reformatting your code to be readable!
Use the "Improve question" widget to edit your question and provide better information.

you can read all text from word file and then encrypt/decrypt it.
You can use Microsoft.Office.Interop.Word to read form word
for Excel Mircrosoft.office.Interop.Excel
I will post code to read word file.
C#
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
object path = @"myDocument.docx";
object readOnly = true;
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
string totaltext = "";
for (int i = 0; i < docs.Paragraphs.Count; i++)
{
totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
}
Console.WriteLine(totaltext);
 
Share this answer
 
Comments
Member 10017812 19-May-13 18:31pm    
where i have to put this set of lines?
can u plz tell me...
C#
using System;
using System.IO;
using System.Security.Cryptography;
public static byte[] Encypt(byte[] buffer, byte[] rgbSalt, string password, int iterations, PaddingMode paddingMode, CipherMode cipherMode)
        {
            using (Rijndael encryption = Rijndael.Create())
            {
                encryption.Padding = paddingMode;
                encryption.Mode = cipherMode;
                using (PasswordDeriveBytes passwordbuffer = new PasswordDeriveBytes(password, rgbSalt))
                {
                    encryption.Key = passwordbuffer.GetBytes(32);
                    encryption.IV = passwordbuffer.GetBytes(16);
                    passwordbuffer.IterationCount = iterations;
                    using (MemoryStream memory = new MemoryStream())
                    {
                        using (CryptoStream crypto = new CryptoStream(memory, encryption.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            crypto.Write(buffer, 0, buffer.Length);
                        }
                        return memory.ToArray();
                    }
                }
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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