Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字节Vigenere密码,解密错误_C#_Byte_Vigenere - Fatal编程技术网

C# 字节Vigenere密码,解密错误

C# 字节Vigenere密码,解密错误,c#,byte,vigenere,C#,Byte,Vigenere,我必须编写一个对完整字节进行操作的Vigenere加密/解密函数(通过tcp加密和发送文件,然后在另一端解密)。 我的加密功能似乎正在工作(或多或少,没有解密功能就无法真正测试它) 这是加密功能的代码: public static Byte[] encryptByteVigenere(Byte[] plaintext, string key) { Byte[] result= new Byte[plaintext.Length]; key = key.Trim().ToUp

我必须编写一个对完整字节进行操作的Vigenere加密/解密函数(通过tcp加密和发送文件,然后在另一端解密)。 我的加密功能似乎正在工作(或多或少,没有解密功能就无法真正测试它)

这是加密功能的代码:

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

好的,问题确实是发送/接收方法,而不是函数本身。我仍然不知道是什么导致了这个问题,但是重写函数有帮助。谢谢你的意见

我把它留在这里,以防将来有人需要这样的功能。。。尽管这是一件微不足道的事情


干杯。

解密ByteVigeneRefix中的
klucz
szyfr
的定义在哪里。我在翻译变量,一定是错过了,对不起!这似乎不太可能引发div by zero异常,除非
key.Length==0
。请发布显示问题的完整代码。不,不是0。我使用相同的密钥进行加密和解密(这是同一个窗口应用程序)。但它仍然停留在keyIndex=keyIndex%keynlength行;整个代码几乎就是包括表单在内的整个windows应用程序。在此行设置一个断点,然后观察
keylength
key
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);               
    }
}