File I/O Encode & Decode with XOR

This is a discussion on File I/O Encode & Decode with XOR within the C# Programming forums, part of the General Programming Boards category; Hi, I wrote 2 programs which encodes and decodes a text file. Upon decoding, it decoded my original text but ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    File I/O Encode & Decode with XOR

    Hi, I wrote 2 programs which encodes and decodes a text file. Upon decoding, it decoded my original text but also added something extra. Here is my piece of code, could anyone kindly explain this occurance?

    Encode ( It receives 2 arguments, first argument is the original text, the second argument receives a seed in order to generate a random number for XOR)
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    
        class Task2a
        {
            static void Main(string[] args)
            {
                FileStream fileOutput, seedFile;
                int i;
                int seed;
                int inputNumber;
                char[] line = new char[1024];
                String strLine;
    
                try
                {
                    StreamReader sr = new StreamReader(args[0]);
    
                    inputNumber = Convert.ToInt32(args[1]);
                    seed = randomNumber(inputNumber);
    
                    fileOutput = new FileStream("cipher.txt", FileMode.OpenOrCreate, FileAccess.Write);
                    seedFile = new FileStream("seed.txt", FileMode.OpenOrCreate, FileAccess.Write);
    
                    StreamWriter input = new StreamWriter(fileOutput);
                    StreamWriter keyInput = new StreamWriter(seedFile);
    
                    strLine = sr.ReadLine();
    
                    line = strLine.ToCharArray();
    
                    for (i = 0; i < line.Length; i++)
                    {
                        line[i] = (char)(line[i] ^ seed);
                        input.Write(line[i]);
                    }
    
                    keyInput.Write(seed.ToString());
    
                    keyInput.Close();
                    input.Close();
                    sr.Close();
                    seedFile.Close();
                    fileOutput.Close();
                }
                catch (FileNotFoundException ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    System.Environment.Exit(1);
                }
                catch (IndexOutOfRangeException ex)
                {
                    Console.WriteLine(ex.Message + "\nNo files detected!");
                    Console.ReadLine();
                    System.Environment.Exit(1);
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message + "\nError Opening Output File");
                    Console.ReadLine();
                    System.Environment.Exit(1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }
    
            static int randomNumber(int seed)
            {
                Random random = new Random();
                return random.Next(seed);
            }
        }
    Decode (It receives 2 arguments, first argument is the encoded text, the second argument is the key from the random generated number in the encode program)

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    
        class Task2b
        {
            static void Main(string[] args)
            {
                FileStream fileOutput;
                int i;
                int key;
                char[] line = new char[1024];
                String strLine, str;
    
                try
                {
                    StreamReader sr = new StreamReader(args[0]);
                    StreamReader srKey = new StreamReader(args[1]);
    
                    fileOutput = new FileStream("decoded.txt", FileMode.OpenOrCreate, FileAccess.Write);
    
                    StreamWriter input = new StreamWriter(fileOutput);
    
                    strLine = srKey.ReadLine();
                    str = sr.ReadLine();
    
                    key = Convert.ToInt32(strLine);
    
                    line = str.ToCharArray();
    
                    for (i = 0; i < line.Length; i++)
                    {
                        line[i] = (char)(line[i] ^ key);
                        input.Write(line[i]);
                    }
    
                    input.Close();
                    sr.Close();
                    srKey.Close();
                    fileOutput.Close();
                }
                catch (FileNotFoundException ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    System.Environment.Exit(1);
                }
                catch (IndexOutOfRangeException ex)
                {
                    Console.WriteLine(ex.Message + "\nNo files detected!");
                    Console.ReadLine();
                    System.Environment.Exit(1);
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message + "\nError Opening Output File");
                    Console.ReadLine();
                    System.Environment.Exit(1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }
        }
    My input text
    Code:
    This is a test
    My encoded text
    Code:
    Plmw$mw$e$pawp09119361013611297119112
    My decoded text
    Code:
    This is a test4=55=7254572556=355=556

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    There is a problem with the encoding, the encoded text should have the exact same length than the plaintext, thus there seems to be a problem with how much you write to the file.

    I'm not an expert in C++ but I suppose the problem is somewhere here:

    Code:
                    for (i = 0; i < line.Length; i++)
                    {
                        line[i] = (char)(line[i] ^ seed);
                        input.Write(line[i]);
                    }

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    Its in C#. I'm relatively new in C#, perhaps its because of the size I've declared? What should I do to overcome this? I thought that
    Code:
    line.Length
    on the for loop would just traverse until the last existing character in my char array?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. XOR and File I/O Problems...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2005, 03:21 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21