Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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;
       }
}
Posted 18-May-13 22:39pm
Edited 19-May-13 0:47am
v2
Comments
OriginalGriff at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
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);
  Permalink  
Comments
Member 10017812 at 19-May-13 18:31pm
   
where i have to put this set of lines?
can u plz tell me...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
                    }
                }
            }
        }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 210
3 Afzaal Ahmad Zeeshan 204
4 CPallini 185
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 19 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100