Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Encrypting & decrypta txt file

Encrypting & decrypta txt file

Scheduled Pinned Locked Moved C#
csharphelp
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 3 Offline
    3 Offline
    3bood ghzawi
    wrote on last edited by
    #1

    Hi all... I'm asking of how i can encrypt and decript a txt file using C#, i think u don't mined to help me ... Thank u............... :-D

    B H 2 Replies Last reply
    0
    • 3 3bood ghzawi

      Hi all... I'm asking of how i can encrypt and decript a txt file using C#, i think u don't mined to help me ... Thank u............... :-D

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Have you search on google?[^]


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

      1 Reply Last reply
      0
      • 3 3bood ghzawi

        Hi all... I'm asking of how i can encrypt and decript a txt file using C#, i think u don't mined to help me ... Thank u............... :-D

        H Offline
        H Offline
        hamed vojdani
        wrote on last edited by
        #3

        because you want both Encrypting and Decrypting I would offer you gunzipstream

            public string Decode()
            {
                FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Open, FileAccess.ReadWrite);
                byte\[\] array = new byte\[400\];
                GZipStream gstream = new GZipStream(fs, CompressionMode.Decompress);
                gstream.Read(array, 0, array.Length);
                gstream.Close();
                string Mytext = Encoding.UTF8.GetString(array);
                return Mytext;
            }
        
            public void Encode(string text)
            {
                byte\[\] array = Encoding.UTF8.GetBytes(text);
                FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Create, FileAccess.ReadWrite);
                GZipStream gstream = new GZipStream(fs, CompressionMode.Compress);
                gstream.Write(array, 0, array.Length);
                gstream.Flush();
                gstream.Close();
            }
        

        note: this solution is not completely secure!

        R 1 Reply Last reply
        0
        • H hamed vojdani

          because you want both Encrypting and Decrypting I would offer you gunzipstream

              public string Decode()
              {
                  FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Open, FileAccess.ReadWrite);
                  byte\[\] array = new byte\[400\];
                  GZipStream gstream = new GZipStream(fs, CompressionMode.Decompress);
                  gstream.Read(array, 0, array.Length);
                  gstream.Close();
                  string Mytext = Encoding.UTF8.GetString(array);
                  return Mytext;
              }
          
              public void Encode(string text)
              {
                  byte\[\] array = Encoding.UTF8.GetBytes(text);
                  FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Create, FileAccess.ReadWrite);
                  GZipStream gstream = new GZipStream(fs, CompressionMode.Compress);
                  gstream.Write(array, 0, array.Length);
                  gstream.Flush();
                  gstream.Close();
              }
          

          note: this solution is not completely secure!

          R Offline
          R Offline
          robbrad
          wrote on last edited by
          #4

          Hi, How could you make this happen on the click of a button? I've tried adding it to the event but the Public void Encode(string text) line obviously stops this happening. edit: Some of my terminology may not be correct, so if your unsure, just ask.

          modified on Monday, December 21, 2009 10:41 AM

          H 1 Reply Last reply
          0
          • R robbrad

            Hi, How could you make this happen on the click of a button? I've tried adding it to the event but the Public void Encode(string text) line obviously stops this happening. edit: Some of my terminology may not be correct, so if your unsure, just ask.

            modified on Monday, December 21, 2009 10:41 AM

            H Offline
            H Offline
            hamed vojdani
            wrote on last edited by
            #5

            just insert button on the form and double click it, Then write button1_Click event code like shown. In this sample I encoded text of TextBox1 to 'Myfile.dat' in My Document note: must copy Encode function to Form class

                private void button1\_Click(object sender, EventArgs e)
                {
                    Encode(TextBox1.Text);
                }
            
                public void Encode(string text)
                {
                    byte\[\] array = Encoding.UTF8.GetBytes(text);
                    FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Create, FileAccess.ReadWrite);
                    GZipStream gstream = new GZipStream(fs, CompressionMode.Compress);
                    gstream.Write(array, 0, array.Length);
                    gstream.Flush();
                    gstream.Close();
                }
            

            modified on Thursday, December 24, 2009 10:23 AM

            R 1 Reply Last reply
            0
            • H hamed vojdani

              just insert button on the form and double click it, Then write button1_Click event code like shown. In this sample I encoded text of TextBox1 to 'Myfile.dat' in My Document note: must copy Encode function to Form class

                  private void button1\_Click(object sender, EventArgs e)
                  {
                      Encode(TextBox1.Text);
                  }
              
                  public void Encode(string text)
                  {
                      byte\[\] array = Encoding.UTF8.GetBytes(text);
                      FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Create, FileAccess.ReadWrite);
                      GZipStream gstream = new GZipStream(fs, CompressionMode.Compress);
                      gstream.Write(array, 0, array.Length);
                      gstream.Flush();
                      gstream.Close();
                  }
              

              modified on Thursday, December 24, 2009 10:23 AM

              R Offline
              R Offline
              robbrad
              wrote on last edited by
              #6

              I get the following error when my code is run: "The given path's format is not supported." The error is in this line:

              FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "MYFILE.txt", FileMode.Create, FileAccess.ReadWrite);

              It cant be a problem with my txt file i dont think, because i have used them before in my programs and they have worked fine. Any help would be appreciated. Extra information: - Using VS 2008 - .NET Framework 3.5 SP1

              H 1 Reply Last reply
              0
              • R robbrad

                I get the following error when my code is run: "The given path's format is not supported." The error is in this line:

                FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "MYFILE.txt", FileMode.Create, FileAccess.ReadWrite);

                It cant be a problem with my txt file i dont think, because i have used them before in my programs and they have worked fine. Any help would be appreciated. Extra information: - Using VS 2008 - .NET Framework 3.5 SP1

                H Offline
                H Offline
                hamed vojdani
                wrote on last edited by
                #7

                thanks you forget the "\\" before "MYFILE.txt" corrct that to this:

                FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MYFILE.txt", FileMode.Create, FileAccess.ReadWrite);
                
                R 1 Reply Last reply
                0
                • H hamed vojdani

                  thanks you forget the "\\" before "MYFILE.txt" corrct that to this:

                  FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MYFILE.txt", FileMode.Create, FileAccess.ReadWrite);
                  
                  R Offline
                  R Offline
                  robbrad
                  wrote on last edited by
                  #8

                  Thanks for your help so far. Just 1 more question. Ive managed to succesfully encrypt the data that is inputted into textbox1, but decoding it fails.

                  public Form1()
                  {
                  InitializeComponent();
                  Decode();
                  }
                  private void button2_Click(object sender, EventArgs e)

                      {
                          Decode();
                  
                          StreamReader rb = new StreamReader("C:\\\\file.txt");
                          MessageBox.Show(rb.ReadToEnd());
                          rb.Close();
                          
                      }
                          public string Decode()
                      {
                          FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\file.txt", FileMode.Open, FileAccess.ReadWrite);
                          byte\[\] array = new byte\[400\];
                          GZipStream gstream = new GZipStream(fs, CompressionMode.Decompress);
                          gstream.Read(array, 0, array.Length);
                          gstream.Close();
                          string Mytext = Encoding.UTF8.GetString(array);
                          return Mytext;
                      }
                  

                  It doesnt return any errors, it just clears the file. Any help would be appreciated

                  R 1 Reply Last reply
                  0
                  • R robbrad

                    Thanks for your help so far. Just 1 more question. Ive managed to succesfully encrypt the data that is inputted into textbox1, but decoding it fails.

                    public Form1()
                    {
                    InitializeComponent();
                    Decode();
                    }
                    private void button2_Click(object sender, EventArgs e)

                        {
                            Decode();
                    
                            StreamReader rb = new StreamReader("C:\\\\file.txt");
                            MessageBox.Show(rb.ReadToEnd());
                            rb.Close();
                            
                        }
                            public string Decode()
                        {
                            FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\file.txt", FileMode.Open, FileAccess.ReadWrite);
                            byte\[\] array = new byte\[400\];
                            GZipStream gstream = new GZipStream(fs, CompressionMode.Decompress);
                            gstream.Read(array, 0, array.Length);
                            gstream.Close();
                            string Mytext = Encoding.UTF8.GetString(array);
                            return Mytext;
                        }
                    

                    It doesnt return any errors, it just clears the file. Any help would be appreciated

                    R Offline
                    R Offline
                    robbrad
                    wrote on last edited by
                    #9

                    Never mind, figured it out. Its amazing what a bit of sleep can do. Thanks

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups