Byte Vigenere Cipher, error with decryption

2019-07-09 01:16发布

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);               
    }
}

标签: c# byte vigenere
1条回答
对你真心纯属浪费
2楼-- · 2019-07-09 02:13

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.

查看更多
登录 后发表回答