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
My encoded text
Code:
Plmw$mw$e$pawp09119361013611297119112
My decoded text
Code:
This is a test4=55=7254572556=355=556