473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

encrypt any file type in c#

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
0 2123

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

Similar topics

1
by: wqhdebian | last post by:
As far as I know,when encrypt or decrypt ,a key must first be got,and the key is first generate by a tool or from SecurityRandom,that means I can not generate the same key with the same input.Does...
7
by: Jean Christophe Avard | last post by:
Hi! I am designing an application wich comes with image file. These images are copyrighted and they have to be accessible only from within the application. At first, I tought I was going to store...
1
by: DazedAndConfused | last post by:
Can you encrpt a serialized object? Or am I trying to do something that just doesn't work that way? I am trying to encrypt a serialized object. I can read and write the object to a file...
2
by: Miles Keaton | last post by:
still doing my switch from MySQL to PgSQL, and can't figure out what the comparable function would be for this: In MySQL, to store a big secret (like a credit card number) in the database that I...
4
by: tomtown.net | last post by:
Hello I'm using the File.Encrypt method (.net 2.0) to encrypt files and folders (mark for encryption) on a local disk (XP SP2, 3DES, .net 2.0). Unfortunatelly I get an exception when trying to...
0
by: mnowosad | last post by:
This problem has become a "show-stopper" for us. I have defined a custom section for my web application that contains sensitive data. Because of that, I want to have it encrypted in the same way I...
0
by: Alex Sadomov | last post by:
Hi all I have a custom configuration section in the App.config file of the console application: --------------------------------------------------------------------------- <configuration>...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
4
by: Max Vit | last post by:
Here is my problem: I have an application built in Access that outputs sensitive data to a text file. I would like to encrypt this data *whilst* the file is being outputted. The encryption I was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.