0
Reply

Reading ASCII Characters from .dat file and Byte Manipulation

Jaikishan Jalan

Jaikishan Jalan

Feb 9 2008 3:20 AM
5.9k

Hi,

I have two .dat files which contains ASCII character. I am trying to read the characters from the file, XOR them and then try to output the result in a .dat file.

Code:

//i will be user as counter index, _n will be used as the minimum length of the files - cipher1.dat,cipher2.dat
//_size will be used to temporarily used to store the length of the stream
//_cipher1 and _cipher 2 are the byte arrays used to store the cipher text
int i = 0; int _n = 0; int _size = 0;
byte[] _cipher1; byte[] _cipher2;

//Reading cipher1.dat file in terms of bytes
FileStream _fsopen = new FileStream("cipher1.dat", FileMode.Open);
_size = (int)_fsopen.Length;
_cipher1 = new byte[_size];
for (i = 0; i < _size; i++)
{
_cipher1[i] = (byte)_fsopen.ReadByte();
}


//Reading cipher2.dat file
_fsopen = new FileStream("cipher2.dat", FileMode.Open);
_size = (int)_fsopen.Length;
_cipher2 = new byte[_size];
for (i = 0; i < _size; i++)
{
_cipher2[i] = (byte)_fsopen.ReadByte();

}


//Calculating the XOR of the two cipher text
_n = Math.Min(_cipher1.Length,_cipher2.Length);
FileStream _fswrite = new FileStream("plainxor.dat", FileMode.Create);
for (i = 0; i < _n; i++)
{
int _xor = (int)_cipher1[i] ^ (int)_cipher2[i];
_fswrite.WriteByte((byte)_xor);
}

_fsopen.Close();
_fswrite.Close();

However, I am not sure if my code is correct as I am suspecting the garbage characters in the output file. Can anyone help me as I this is my first time playing with ASCII characters and byte manipulation? Please ignore the identation as it got bad due to copy - paste of the code