title: Android - Jave Server AES encryption Not working - Different keys
body: <p>I am trying to encrypt images on android, send them to the server so that it process them. The server has to decrypt  the received message. I already posted the code in this <a href="https://stackoverflow.com/questions/10562908/encryption-of-strings-works-encryption-of-byte-array-type-does-not-work/">question</a></p>
  
  <p>I have called the encrypt function on the Android side and the decrypt function the java server side (image is sent via TCP).</p>
  
  <p>However, I am receiving the error: </p>
  
  <pre><code>javax.crypto.BadPaddingException: Given final block not properly padded 
  </code></pre>
  
  <p>I output the key on android and got: </p>
  
  <pre><code> javax.crypto.spec.SecretKeySpec@652
  </code></pre>
  
  <p>whereas on the Java server (developped using Netbeans) I got:    </p>
  
  <pre><code>javax.crypto.spec.SecretKeySpec@148dd
  </code></pre>
  
  <p>I thought that the padding is different so I used</p>
  
  <pre><code>AES/ECB/PKCS5Padding 
  </code></pre>
  
  <p>instead of 
      AES </p>
  
  <p>but the Java server output an error: </p>
  
  <pre><code>should use AES.
  </code></pre>
  
  <p>How can i solve this issue?</p>
  
  <p>For send on android side:</p>
  
  <pre><code> public void send(Bitmap mRgbImage1_array, int port_number)
          throws IOException {
  
  socket = new Socket("192.168.0.107", port_number);
  boolean encrypt = HomeScreen.checkbox2.isChecked();
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  
  mRgbImage1_array.compress(CompressFormat.JPEG, 100, stream);
  InputStream photoStream = new ByteArrayInputStream(stream.toByteArray());
  BufferedInputStream bis = new BufferedInputStream(photoStream);
  byte[] mybytearray = new byte[photoStream.available()];
  bis.read(mybytearray, 0, mybytearray.length);
  photoStream.close();
  
  if(encrypt)
     {
   try {
  byte[] dst = Security.encrypt(mybytearray);
  mybytearray = new byte[10000];
  for(int i=0; i&lt;dst.length;i++)
  {
       mybytearray[i] = dst[i];   
  }
  
   } catch (Exception e1) {
      /// TODO Auto-generated catch block
      e1.printStackTrace();
  }
  
  }
      os = socket.getOutputStream();
      os.write(mybytearray);
              os.flush();
      os.close();
  if (os != null)
  {
  try {
  os.close();
      } 
  
      catch (IOException e) 
         {
              // TODO Auto-generated catch block
              e.printStackTrace();
      }
   }
  
  }
  </code></pre>
  
  <p>For receive on Java server side:</p>
  
  <pre><code>    public static void receive(int port_number) {
      boolean received = false;
  
      Socket socket = null;
      InputStream is = null;
      int bytesRead;
      int current = 0;
  
      BufferedOutputStream bos = null;
      try {
          if (serverSocket == null) {
              serverSocket = new ServerSocket(port_number);
          }
          System.out.println("Listening :" + port_number);
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      while (!received) {
          try {
              socket = serverSocket.accept();
              InetAddress ip = socket.getInetAddress();
              String[] ip1 = ip.toString().split("/");
              ip2 = ip1[1];
              System.out.println("ip is " + ip2);
              byte[] mybytearray = new byte[10000000];
              is = socket.getInputStream();
              FileOutputStream fos = new    FileOutputStream("source-image.jpeg");
              bos = new BufferedOutputStream(fos);
              bytesRead = is.read(mybytearray, 0, mybytearray.length);
             current = bytesRead;
              do {
                  bytesRead =
                          is.read(mybytearray, current, (mybytearray.length - current));
                  if (bytesRead &gt;= 0) {
                      current += bytesRead;
                  }
  
              } while (bytesRead &gt; -1);
  if(Networker.should_encrypt)
              {
           try {
       mybytearray = Security1.decrypt(mybytearray);
          } catch (Exception e1) {
              /// TODO Auto-generated catch block
              e1.printStackTrace();
          }
              }
              bos.write(mybytearray, 0, current);
              bos.flush();
              bos.close();
  
              received = true;
          } catch (IOException ex) {
              Logger.getLogger(MyServer1.class.getName()).log(Level.SEVERE, null, ex);
          } finally {
              if (socket != null) {
                  try {
                      socket.close();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
              if (is != null) {
                  try {
                      is.close();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          }
      }
     }
  </code></pre>