字节V @ genere加密,误差解密 [英] Byte Vigenere Cipher, error with decryption

查看:203
本文介绍了字节V @ genere加密,误差解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须写上全字节运行的的V @ genere加密/解密功能(加密和通过TCP发送文件,然后在另一端解密)。
我的加密功能似乎是工作(多还是少,不能真正测试它没有解密功能)。



这是加密函数的代码

 公共静态字节] encryptByteVigenere(字节[]明文字符串键)
{

字节[]结果=新的字节[plaintext.Length]

键= key.Trim()ToUpper的()。

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

的for(int i = 0; I< plaintext.Length;我++)
{
keyIndex = keyIndex%keylength;
INT移位=(int)的关键[keyIndex] - 65;
结果[I] =(字节)(((int)的明文[I] + SHIFT)256%);
keyIndex ++;
}

返回结果;
}



不过,解密功能,即使在几乎相同的方式写的,导致错误。
。试图除以零



解密函数的代码:

 公共静态字节] decryptByteVigenere(字节[]的密文,字符串键)
{
字节[]结果=新的字节[ciphertext.Length]

键= key.Trim()ToUpper的()。

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

的for(int i = 0; I< ciphertext.Length;我++)
{
keyIndex = keyIndex%keylength;
INT移位=(int)的关键[keyIndex] - 65;
结果[I] =(字节)(((int)的密文[I] + 256 - 移)256%);
keyIndex ++;
}

返回结果;
}



在该行
keyIndex = keyIndex%keylength错误点;
但是我想知道的是,该代码是相当多的第一个功能相同,它似乎并没有引起任何麻烦。我接收到的FILD,它正确地到达未经加密测试。 ?谁能帮助我与



编辑:
的方法/线程正在使用的解密函数代码:

 公共无效fileListenThread()
{

{
fileServer.Start();

字符串receivedFileName =TEST.DAT
串键=(textKlucz.Text).ToUpper();

,而(真)
{
fileClient = fileServer.AcceptTcpClient();
的NetworkStream streamFileServer = fileClient.GetStream();
INT thisRead = 0;
INT块大小= 1024;
字节[]数据字节=新的字节[BLOCKSIZE]
字节[] = dataByteDecrypted新的字节[BLOCKSIZE]

的FileStream FILESTREAM =新的FileStream(receivedFileName,FileMode.Create);
,而(真)
{
thisRead = streamFileServer.Read(数据字节,0,块大小);
dataByteDecrypted = Program.decryptByteVigenere(数据字节,键);
fileStream.Write(dataByteDecrypted,0,thisRead);
如果(thisRead == 0)
中断;
}

fileStream.Close();
}
}
赶上(SocketException E)
{
MessageBox.Show(SocketException:+ E,Wystąpiłwyjątek,MessageBoxButtons.OK,MessageBoxIcon。错误);
}
}


解决方案

确定问题的确是发送/接收方法,而不是函数本身。我还是真的不知道是什么原因造成的问题,而是重写的功能有帮助。感谢您的输入!



我在这里把它留在万一有人在未来需要这样的功能......即使它相当琐碎的事情。



干杯。


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

解决方案

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.

这篇关于字节V @ genere加密,误差解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆