I've made a tool that I made to encrypt and decrypt messages. Not that I'm working for the military or anything, I just got interested in a concept.
It takes each character and converts it to my personal binary format. Each character is extended to 16 bits, or a short
. Each of these short integers are stored in a single array.
My goal is to write this array to a file in binary, and be able to read it back into the array.
Here is what I've started:
//This is the array the encrypted characters are stored in.
short[] binaryStr = new short[32767];
//...
private void butSelInput_Click(object sender, EventArgs e)
{
dialogImportMsg.ShowDialog();
}
private void dialogImportMsg_FileOk(object sender, CancelEventArgs e)
{
using (BinaryReader reader = new BinaryReader(new FileStream(dialogImportMsg.FileName, FileMode.Open)))
{
for (short x = 0; x < (short)reader.BaseStream.Length; x++)
{
binaryStr[x] = reader.ReadInt16();
}
}
}
private void butExport_Click(object sender, EventArgs e)
{
dialogExportMsg.ShowDialog();
}
private void dialogExportMsg_FileOk(object sender, CancelEventArgs e)
{
using (BinaryWriter writer = new BinaryWriter(new FileStream(dialogExportMsg.FileName, FileMode.OpenOrCreate)))
{
for (int x = 0; x < binaryStr.Length; x++)
{
//if(binaryStr[x]
writer.Write(BitConverter.GetBytes(binaryStr[x]));
}
}
}
Obviously I'm going about it wrong since it's not working like I want it to. The writer might be working, but it writes the entire array which is 65534 bytes. I want it to only write the characters that are stored (i.e. everything up to the last non-zero character). Then the reader should correspond to that, reading the characters from the file and putting them into the array exactly as they were when they were exported.
So the question is, how do I do that?