0
votes

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?

1
You were trying something with if(binaryStr[x]), didn't it work? - Nikhil Vartak
I was going to attempt to terminate the loop early... Just didn't know where to go about it. - Corey Iles
Yes so what problem did you face in that? - Nikhil Vartak
No problem, because I didn't finish the attempt. As I said, I didn't know what to do. Which is when I came here. - Corey Iles
You could continue (skip Xth short value) or break the loop altogether. - Nikhil Vartak

1 Answers

0
votes

I decided to just store the original string's length. I'll use it for the writing loop, store it in the file, and read it from the file later.

    private void dialogImportMsg_FileOk(object sender, CancelEventArgs e)
    {
        using (BinaryReader reader = new BinaryReader(new FileStream(dialogImportMsg.FileName, FileMode.Open)))
        {
            binaryStrLen = reader.ReadInt16();
            for (short x = 0; x < binaryStrLen; 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)))
        {
            writer.Write(BitConverter.GetBytes(binaryStrLen));
            for (int x = 0; x < binaryStrLen; x++)
            {
                writer.Write(BitConverter.GetBytes(binaryStr[x]));
            }
        }
    }