2

I have to write a Vigenere encryption / decryption function that operates on full bytes (to encrypt and send files over tcp and then decrypt on the other side). My encrypting function seems to be working (more or less, can't really test it without decrypting function).

This is the code of the encrypting function:

public static Byte[] encryptByteVigenere(Byte[] plaintext, string key) 
{

    Byte[] result= new Byte[plaintext.Length];

    key = key.Trim().ToUpper();

    int keyIndex = 0;
    int keylength = key.Length;

    for (int i = 0; i < plaintext.Length; i++)
    {
        keyIndex = keyIndex % keylength;
        int shift = (int)key[keyIndex] - 65;
        result[i] = (byte)(((int)plaintext[i] + shift) % 256);
        keyIndex++;
    }

    return result;
}

However, the decrypting function, even though wrote in pretty much the same way, causes an error. "Attempted to divide by zero."

The code of the decrypting function:

public static Byte[] decryptByteVigenere(Byte[] ciphertext, string key)
{
    Byte[] result = new Byte[ciphertext.Length];

    key = key.Trim().ToUpper();

    int keyIndex = 0;
    int keylength = key.Length;

    for (int i = 0; i < ciphertext.Length; i++)
    {             
        keyIndex = keyIndex % keylength;
        int shift = (int)key[keyIndex] - 65;
        result[i]= (byte)(((int)ciphertext[i] + 256 - shift) % 256);
        keyIndex++;               
    }

    return result;
}

The error points at the line keyIndex = keyIndex % keylength; But what wonders me is that the code is pretty much the same in the first function and it doesn't seem to cause any trouble. I'm testing it on the received fild, which arrives correctly without encryption. Could anyone help me with that?

EDIT: The method / thread that is using the decryption function code:

public void fileListenThread()
{         
    try
    {
        fileServer.Start();

        String receivedFileName = "test.dat";
        String key = (textKlucz.Text).ToUpper();

        while (true)
        {
            fileClient = fileServer.AcceptTcpClient();
            NetworkStream streamFileServer = fileClient.GetStream();
            int thisRead = 0;
            int blockSize = 1024;
            Byte[] dataByte = new Byte[blockSize];
            Byte[] dataByteDecrypted = new Byte[blockSize];

            FileStream fileStream = new FileStream(receivedFileName, FileMode.Create);
            while (true)
            {
                thisRead = streamFileServer.Read(dataByte, 0, blockSize);
                dataByteDecrypted = Program.decryptByteVigenere(dataByte, key);
                fileStream.Write(dataByteDecrypted, 0, thisRead);
                if (thisRead == 0)
                     break;
            }

            fileStream.Close();                 
        }
    }
    catch (SocketException e)
    {
        MessageBox.Show("SocketException: " + e, "Wystąpił wyjątek", MessageBoxButtons.OK, MessageBoxIcon.Error);               
    }
}
Matyy
  • 21
  • 3
  • Where are the definitions of `klucz` and `szyfr` in decryptByteVigenere – L.B Nov 13 '12 at 21:56
  • Fixed. I was translating the variables and must have missed these, sorry! – Matyy Nov 13 '12 at 21:56
  • Seems unlikely that this throws a div-by-zero exception, unless `key.Length==0`. Please post complete code that exhibits the problem. – CodesInChaos Nov 13 '12 at 21:59
  • No, it's not 0. I use the same key to encrypt and decrypt (it's the same window application). Yet it still stops at the line keyIndex = keyIndex % keylength; The entire code is pretty much the whole windows application including form. – Matyy Nov 13 '12 at 22:00
  • Set a breakpoint at this line and watch `keylength` and `key`. – Alex Nov 13 '12 at 22:04
  • Added the code of the thread that is used to receive files sent by the client. – Matyy Nov 13 '12 at 22:05
  • one obvious mistake is that you're decrypting the whole buffer, and not just the first `thisRead` bytes. But that doesn't explain your issue. Perhaps your problem is broken multi-threading. – CodesInChaos Nov 13 '12 at 22:15
  • But if that was really the problem, it would also be a problem in encrypting, right? I don't really have any idea how to fix it. But the problem with "division by zero" still occurs. While the keylength indeed is 1. – Matyy Nov 13 '12 at 22:19

1 Answers1

0

Ok the problem was indeed the sending / receiving method, not the function itself. I still don't really know what caused the problem, but rewriting the functions helped. Thanks for your input!

I'm leaving it here in case someone needed such function in the future... even though it's rather trivial thing.

Cheers.

Matyy
  • 21
  • 3