简体   繁体   中英

Bad Data error while des decryption

I am trying to create a client and server to send a file from the client to the server safely and encrypted. First I will use RSA to send the DES key between the two sides (client to server). I sent the RSA public used it and sent the DES key encrypted to the server and decrypted. Then I added the file name length, file name and data (encrypted) to the byte array that is going to be sent to the server. While decrypting the file by using DES I received a bad data exception, so I thought that either the file is sent wrongly or the DES key was sent wrongly from the client to server. Also the file can be decrypted at the origin.

Here is the client code:

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

here is the server code:

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

The problem is that i am trying to decrypt the wrong block. This is because i used wrong indexing in

string temp = System.Text.Encoding.UTF8.GetString(b, 3 + fileNameLen, receivedBytesLen);

and updated to :

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

in a file i used the receivedBytesLen is 2756 bytes while the decrypted data is 1496 so i needed to send the data length to extract it from the data block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM