-1

I'm working on an encryption program in C# (Windows Forms) and one of the options I'd like to add is that the user will be able to choose an existing text (.txt) file, and the program would make a new file which is the file chosen, but encrypted (without making any changes in the original file). I though about making a copy of the original file and then encrypting the new file, but I have no clue how to do it. Please tell me how to do it. Thanks a lot in advance!

Danny
  • 45
  • 6
  • 1
    What have you tried..? there are so many options / approaches one could take to do this.. Have you researched what type of encryption you want to use..? please show what you have done thus far if anything at all – MethodMan Aug 12 '13 at 19:14
  • Explain clearly what you need - guidance on encryption or file handling? – Nayan Aug 12 '13 at 19:23
  • I know how to make a button which opens a browser and lets the user choose the file. I also know how to encrypt the chosen file. What I don't know is how do I make the program create a new file, which is exactly the same as the chosen file, encrypt it (No help needed in that step) and then save it in a directory chosen by the user. By the way, I haven't written my code yet, I'm just practicing using a simple substitution cipher... – Danny Aug 12 '13 at 20:41
  • So what's the problem, when using StreamWriter? I have updated my answer. It describes how to chose your save location and how to write the file. – Daniel Abou Chleih Aug 12 '13 at 22:32

3 Answers3

0

StreamReader/StreamWriter for loading and saving the file. StreamReader:

string unencryptedText;
    private void ReadTextFile()
    {

        using (StreamReader reader = new StreamReader("file.txt"))
        {
            unencryptedText= reader.ReadToEnd();
        }
    }

StreamWriter

    using (StreamWriter writer = new StreamWriter("encryptedFile.txt", true))
    {
        writer.Write(encryptedText);
    }

Encryption: Simple insecure two-way "obfuscation" for C#

Update Chose Directory where to save encrypted file(only Directory)

        FolderBrowserDialog fbd = new FolderBrowserDialog();
        if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
        {
            using (StreamWriter writer = new StreamWriter(fbd.SelectedPath+"\\encryptedFile.txt", true))
            {
                writer.Write(encryptedText);
            }
        }

Chose Directory and FileName

        SaveFileDialog sfd = new SaveFileDialog();
        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            using (StreamWriter writer = new StreamWriter(sfd.FileName, true))
            {
                writer.Write(encryptedText);
            }
        }
Community
  • 1
  • 1
Daniel Abou Chleih
  • 2,440
  • 2
  • 19
  • 31
0

Use System.IO.StreamReader and System.IO.StreamWriter to read and write from text files.

http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx

        using (StreamReader sr = new StreamReader(filePath))
        {
            fileContents = sr.ReadToEnd();
        }

        string encryptedContents = Encrypt(fileContents);

        using (StreamWriter sw = new StreamWriter(destinationPath))
        {
            sw.Write(encryptedContents);
        }
Michael
  • 1,803
  • 1
  • 17
  • 26
0
File.Copy(pathX,pathY) 

Will copy the file from path X to path Y.

The next thing is to write the encrypted text to the copied file:

File.WriteAllText(pathY,textToWrite)

I can also say you will learn more if you read msdn examples. Everything you look for is there.

FelProNet
  • 689
  • 2
  • 10
  • 25