【问题标题】:Bad Data error while des decryption解密时出现错误数据错误
【发布时间】:2012-04-28 02:00:39
【问题描述】:

我正在尝试创建一个客户端和服务器,以将文件从客户端安全并加密地发送到服务器。首先,我将使用 RSA 在两端(客户端到服务器)之间发送 DES 密钥。我发送了 RSA public 使用它并将加密的 DES 密钥发送到服务器并解密。然后我将文件名长度、文件名和数据(加密)添加到要发送到服务器的字节数组中。在使用 DES 解密文件时,我收到了错误的数据异常,所以我认为文件发送错误或 DES 密钥从客户端错误地发送到服务器。文件也可以在源头解密。

这是客户端代码:

//encrypt the file with the des key
            byte [] encryptedFile = DESEncrypt(fileBytes);
            MessageBox.Show("enc file length  " + encryptedFile.Length);
            //send the file
            byte[] k = DESdecrypt(encryptedFile);

            byte[] fileNameByte = Encoding.ASCII.GetBytes(Path.GetFileName(filePath.Text));

            byte[] clientData = new byte[4 + fileNameByte.Length + encryptedFile.Length];
            byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

            fileNameLen.CopyTo(clientData, 0);
            fileNameByte.CopyTo(clientData, 4);
            encryptedFile.CopyTo(clientData, 4 + fileNameByte.Length);

            soc.Send(clientData);

这是服务器代码:

byte[] clientData = new byte[1024 * 5000];
                int receivedBytesLen = s.Receive(clientData);
                byte[] b = clientData;
                int fileNameLen = BitConverter.ToInt32(clientData, 0);

                fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
                byte[] l = Encoding.ASCII.GetBytes(fileName);
                Console.WriteLine("Client:{0} connected & File {1} started received.", s.RemoteEndPoint, fileName);
                Console.WriteLine("buffer size  : ", b.Length);
                Console.WriteLine("index  : ", 4 + fileNameLen);

                string temp = System.Text.Encoding.UTF8.GetString(b, 3 + fileNameLen, receivedBytesLen);
                fileBytes = System.Text.Encoding.UTF8.GetBytes(temp);
                Console.WriteLine("rec file");

            }
            catch (Exception exx)
            {
                Console.WriteLine(exx.ToString());
                Console.Read();
            }
            //decrypt
            byte[] decryptedFile = null;
            try
            {

                decryptedFile = DESdecrypt(fileBytes);
            }
            catch(Exception exx)
            {
                Console.WriteLine(exx.ToString());
                Console.Read();
            }

【问题讨论】:

  • 可能相关/同一用户:stackoverflow.com/questions/10362409/…
  • 第一个不同的用户。其次,问题解决了,我从收到的文件中得到了 1 个错误的字节。例如我使用 (4-1000) 并且加密数据来自 (5-1001) 只是错误的方式将我的数据从块中取出。

标签: c# cryptography des


【解决方案1】:

问题是我试图解密错误的块。这是因为我在

中使用了错误的索引
string temp = System.Text.Encoding.UTF8.GetString(b, 3 + fileNameLen, receivedBytesLen);

并更新为:

Array.Copy(b, fileNameLen + 4, fileBytes, 0, dataLengthInt - fileNameLen-4);

在我使用的文件中,receivedBytesLen 为 2756 字节,而解密数据为 1496,因此我需要发送数据长度以从数据块中提取它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-27
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 2017-10-29
    • 1970-01-01
    • 2017-11-08
    相关资源
    最近更新 更多