title: SSL handshake with Apple Push Notification Server via Java
body: <p>Hello I am trying to send a push message to my device using Java. But I'am allready getting problems when establishing the ssl connection. 
  Here is the code so far:</p>
  
  <pre><code>        KeyStore keyStore = KeyStore.getInstance("PKCS12");
  
          InputStream key = getClass().getResourceAsStream("apns-dev-key.p12");
          char[] c = key.toString().toCharArray();
  
          keyStore.load(getClass().getResourceAsStream("apns-dev-cert.p12"), c);
          KeyManagerFactory keyMgrFactory = KeyManagerFactory.getInstance("SunX509");
          keyMgrFactory.init(keyStore, c);
  
          SSLContext sslContext = SSLContext.getInstance("TLS");
          sslContext.init(keyMgrFactory.getKeyManagers(), null, null);
          SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
  
          SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port);
          String[] cipherSuites = sslSocket.getSupportedCipherSuites();
          sslSocket.setEnabledCipherSuites(cipherSuites);
          sslSocket.startHandshake();
  </code></pre>
  
  <p>The error I am getting is:</p>
  
  <pre><code>java.io.IOException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded
  </code></pre>
  
  <p>I guess there is some problem with the apns-dev-key.p12 file. Any hints?</p>
  
  <p>The code above is taken from: <a href="http://undermypalapa.wordpress.com/2009/08/23/apple-push-notification-service-java/" rel="nofollow">http://undermypalapa.wordpress.com/2009/08/23/apple-push-notification-service-java/</a></p>