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. Encryption and Decryption Problems

Encryption and Decryption Problems

Scheduled Pinned Locked Moved C#
securityhelpquestion
11 Posts 3 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.
  • T thepolishguy

    I'm using the RijndaelManaged class to encrypt data. One of the things I want to encrypt is a log file that records the usernames of the people who log on to my application. When I write a single line to a file, I'm able to decrypt the data without a problem, but when I log onto the program again, thereby encrypting a second username to the file, I'm unable to successfully decrypt both lines of data. Only part of the second line is decrypted successfully. Does anyone have any suggestions/comments about why I'm encountering this problem?

    J Offline
    J Offline
    John Fisher
    wrote on last edited by
    #2

    In general, debugging a problem for you requires us to view the code you are using. We may not always be able to help, but seeing your code greatly increases the possibility. John
    "You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.

    T 1 Reply Last reply
    0
    • J John Fisher

      In general, debugging a problem for you requires us to view the code you are using. We may not always be able to help, but seeing your code greatly increases the possibility. John
      "You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.

      T Offline
      T Offline
      thepolishguy
      wrote on last edited by
      #3

      Okay, here's the code I'm using to encrypt: public void EncryptData() { FileStream fs=null; if(File.Exists(Directory.GetCurrentDirectory() + "\\c#.ini") == false) { fs=new FileStream(Directory.GetCurrentDirectory() + "\\c#.ini", System.IO.FileMode.CreateNew); } else { fs=new FileStream(Directory.GetCurrentDirectory() + "\\c#.ini", System.IO.FileMode.Append); } byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; RijndaelManaged RMCrypto=new RijndaelManaged(); System.Security.Cryptography.CryptoStream cryption=new CryptoStream(fs, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write); StreamWriter tnWriter2 = new StreamWriter(cryption, System.Text.Encoding.Unicode); tnWriter2.WriteLine(log); tnWriter2.Close(); fs.Close(); } And here's the code for Decrypting: private void DecryptDataLog() { FileStream read=new FileStream(currPath + "\\c#.ini", System.IO.FileMode.OpenOrCreate); byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; RijndaelManaged RMCrypto=new RijndaelManaged(); System.Security.Cryptography.CryptoStream cryption=new CryptoStream(read, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Read); StreamReader tnReader = new StreamReader(cryption, System.Text.Encoding.Unicode); this.textBox1.Text=tnReader.ReadToEnd(); tnReader.Close(); read.Close(); } When the file I'm writing to has more than one line, the first few characters of the 2nd, 3rd, etc lines of encrypted text show up as gibberish, while the rest of the line is OK.

      J 1 Reply Last reply
      0
      • T thepolishguy

        Okay, here's the code I'm using to encrypt: public void EncryptData() { FileStream fs=null; if(File.Exists(Directory.GetCurrentDirectory() + "\\c#.ini") == false) { fs=new FileStream(Directory.GetCurrentDirectory() + "\\c#.ini", System.IO.FileMode.CreateNew); } else { fs=new FileStream(Directory.GetCurrentDirectory() + "\\c#.ini", System.IO.FileMode.Append); } byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; RijndaelManaged RMCrypto=new RijndaelManaged(); System.Security.Cryptography.CryptoStream cryption=new CryptoStream(fs, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write); StreamWriter tnWriter2 = new StreamWriter(cryption, System.Text.Encoding.Unicode); tnWriter2.WriteLine(log); tnWriter2.Close(); fs.Close(); } And here's the code for Decrypting: private void DecryptDataLog() { FileStream read=new FileStream(currPath + "\\c#.ini", System.IO.FileMode.OpenOrCreate); byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; RijndaelManaged RMCrypto=new RijndaelManaged(); System.Security.Cryptography.CryptoStream cryption=new CryptoStream(read, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Read); StreamReader tnReader = new StreamReader(cryption, System.Text.Encoding.Unicode); this.textBox1.Text=tnReader.ReadToEnd(); tnReader.Close(); read.Close(); } When the file I'm writing to has more than one line, the first few characters of the 2nd, 3rd, etc lines of encrypted text show up as gibberish, while the rest of the line is OK.

        J Offline
        J Offline
        John Fisher
        wrote on last edited by
        #4

        Do you write those lines in a separate step from the code listed, or does the "log" variable have multiple lines in it? John
        "You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.

        T 1 Reply Last reply
        0
        • J John Fisher

          Do you write those lines in a separate step from the code listed, or does the "log" variable have multiple lines in it? John
          "You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.

          T Offline
          T Offline
          thepolishguy
          wrote on last edited by
          #5

          The log variable is simply this: log=this.txtBxUsername.Text + ", " + DateTime.Now; I want only to record the username and the time the user logged on. administrator, 1/16/2006 10:55:37 AM တတတတတတတတ襢⭺鉳䀗霳ꇳtrator, 1/16/2006 10:55:48 AM တတတတတတတတ垵쌂멨蛫륢trator, 1/16/2006 10:56:02 AM တတတတတတတတ뙩鲂팒ᬵ밞拑䔏⎗trator, 1/16/2006 10:56:15 AM This is an example of the output I receive.

          J G 2 Replies Last reply
          0
          • T thepolishguy

            The log variable is simply this: log=this.txtBxUsername.Text + ", " + DateTime.Now; I want only to record the username and the time the user logged on. administrator, 1/16/2006 10:55:37 AM တတတတတတတတ襢⭺鉳䀗霳ꇳtrator, 1/16/2006 10:55:48 AM တတတတတတတတ垵쌂멨蛫륢trator, 1/16/2006 10:56:02 AM တတတတတတတတ뙩鲂팒ᬵ밞拑䔏⎗trator, 1/16/2006 10:56:15 AM This is an example of the output I receive.

            J Offline
            J Offline
            John Fisher
            wrote on last edited by
            #6

            How is it that you get a multi-line file from code that only writes one line? [edit]Oops, just looked back and saw the APPEND option when you open the file. That's the problem. You need to write the entire file each time if you plan on encrypting and decrypting it. It appears that you accidentally read some of it because you are using the same key and initialization vector each time.[/edit] John
            "You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek. -- modified at 14:20 Monday 16th January, 2006

            T 2 Replies Last reply
            0
            • T thepolishguy

              The log variable is simply this: log=this.txtBxUsername.Text + ", " + DateTime.Now; I want only to record the username and the time the user logged on. administrator, 1/16/2006 10:55:37 AM တတတတတတတတ襢⭺鉳䀗霳ꇳtrator, 1/16/2006 10:55:48 AM တတတတတတတတ垵쌂멨蛫륢trator, 1/16/2006 10:56:02 AM တတတတတတတတ뙩鲂팒ᬵ밞拑䔏⎗trator, 1/16/2006 10:56:15 AM This is an example of the output I receive.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #7

              What are you using for line breaks? Use Environment.NewLine or "\r\n". --- b { font-weight: normal; }

              1 Reply Last reply
              0
              • J John Fisher

                How is it that you get a multi-line file from code that only writes one line? [edit]Oops, just looked back and saw the APPEND option when you open the file. That's the problem. You need to write the entire file each time if you plan on encrypting and decrypting it. It appears that you accidentally read some of it because you are using the same key and initialization vector each time.[/edit] John
                "You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek. -- modified at 14:20 Monday 16th January, 2006

                T Offline
                T Offline
                thepolishguy
                wrote on last edited by
                #8

                The file is written to every time a user logs on to the application. I'm appending the new user information to the document each time the user logs on. So, the WriteLine is performed once each time through the application, but the file is appended the next time through.

                J 1 Reply Last reply
                0
                • T thepolishguy

                  The file is written to every time a user logs on to the application. I'm appending the new user information to the document each time the user logs on. So, the WriteLine is performed once each time through the application, but the file is appended the next time through.

                  J Offline
                  J Offline
                  John Fisher
                  wrote on last edited by
                  #9

                  I just modified the previous post to comment on this behavior. Check it out. John
                  "You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.

                  1 Reply Last reply
                  0
                  • J John Fisher

                    How is it that you get a multi-line file from code that only writes one line? [edit]Oops, just looked back and saw the APPEND option when you open the file. That's the problem. You need to write the entire file each time if you plan on encrypting and decrypting it. It appears that you accidentally read some of it because you are using the same key and initialization vector each time.[/edit] John
                    "You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek. -- modified at 14:20 Monday 16th January, 2006

                    T Offline
                    T Offline
                    thepolishguy
                    wrote on last edited by
                    #10

                    So, would you suggest I read the file first and append the content to it the result of that read, then encrypt the whole thing? How are user logon records stored typically?

                    J 1 Reply Last reply
                    0
                    • T thepolishguy

                      So, would you suggest I read the file first and append the content to it the result of that read, then encrypt the whole thing? How are user logon records stored typically?

                      J Offline
                      J Offline
                      John Fisher
                      wrote on last edited by
                      #11

                      thepolishguy wrote:

                      So, would you suggest I read the file first and append the content to it the result of that read, then encrypt the whole thing?

                      Yep.

                      thepolishguy wrote:

                      How are user logon records stored typically?

                      I'm not sure there is a "typical" way. You might consider using a custom event log in Windows. John
                      "You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.

                      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