By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,869 Members | 1,756 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,869 IT Pros & Developers. It's quick & easy.

encrypt any file type in c#

P: 38
Hi, everyone

I'm developing a c# windows application that allows the user to encrypt any file type. i would like to encrypt the file using a powerful encrypting algorithm. I've tried to use the rinjael, triple des, but it doesn't seem to encrypt certain files at all. Can you please tell me what the problem is. I don't understand why this function doesn't work when I call it and encrypt the file. when I search for the file it still pulls up when I search the harddrive and I'm still able to open the file. Can someone please help?

here's what i tried:
Expand|Select|Wrap|Line Numbers
  1. private void button3_Click(object sender, EventArgs e)
  2.         {
  3.             // Must be 64 bits, 8 bytes.
  4.  
  5.             try
  6.             {
  7.                 string filePath = Path.Combine(currentFolderPath,
  8.                     textBox3.Text);
  9.                 TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
  10.                MessageBox.Show("Warning! By deleting a file it can cause other programs to not function properly!");
  11.  
  12.                 string query = "Are you sure you want to delete the file\n" + filePath + "?";
  13.                 if (MessageBox.Show(query,
  14.                     "Delete File?", MessageBoxButtons.YesNo) == DialogResult.Yes)
  15.                 {
  16.                     // Remove the Key from memory. 
  17.  
  18.                     File.Delete(filePath); //doesn't delete  from computer (Any help?)                             
  19.                     File.Encrypt(filePath); //doesn't encrypt properly (Any help?)
  20.                    EncryptFile(filePath, filePath, key, key); //not sure how to call this               
  21.                     DisplayFolderList(currentFolderPath);
  22.                     string query_two = "Would you like to delete another file\n";
  23.                     if (MessageBox.Show(query_two,
  24.                     "Restart?", MessageBoxButtons.YesNo) == DialogResult.Yes)
  25.                         Application.Restart();
  26.                 }
  27.             }
  28.             catch (Exception ex)
  29.             {
  30.                 MessageBox.Show("Unable to delete file. The following exception"
  31.                     + " occurred:\n" + ex.Message, "Failed");
  32.             }        
  33.         }
//encryption function
Expand|Select|Wrap|Line Numbers
  1.  void EncryptFile(string sInputFilename, string sOutputFilename, string sKey, string sIV)
  2.         {
  3.             Rijndael rijndaelAlg = Rijndael.Create();
  4.             //  rijndaelAlg.Key = some_key_val;
  5.             //  rijndaelAlg.IV = some_iv_val;
  6.             //  rijndaelAlg.KeySize = 256; // other valid key sizes: 128 or 192 (defaults to 256)
  7.  
  8.  
  9.             //Set secret key For AES algorithm.
  10.             rijndaelAlg.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  11.             //Set initialization vector.
  12.             rijndaelAlg.IV = ASCIIEncoding.ASCII.GetBytes(sIV);
  13.             FileStream fsPlaintext = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
  14.             FileStream fsCiphertext = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
  15.             //Create an AES encryptor from the AES instance.
  16.             ICryptoTransform aesencrypt = rijndaelAlg.CreateEncryptor();
  17.  
  18.             //Create crypto stream set to read and do an AES encryption transform on incoming bytes.
  19.             CryptoStream cipherstream = new CryptoStream(fsCiphertext, aesencrypt, CryptoStreamMode.Write);
  20.             byte[] bytearrayinput = new byte[fsPlaintext.Length];
  21.             fsPlaintext.Read(bytearrayinput, 0, bytearrayinput.Length);
  22.             cipherstream.Write(bytearrayinput, 0, bytearrayinput.Length);
  23.             cipherstream.Close();
  24.             fsPlaintext.Close();
  25.             fsCiphertext.Close();
  26.         }
Aug 5 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.