title: Bad Data error while des decryption
body: <p>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.</p>
  
  <p>Here is the client code:</p>
  
  <pre><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);
  </code></pre>
  
  <p>here is the server code:</p>
  
  <pre><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 &amp; 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();
              }
  </code></pre>