title: java invalidKeyException
body: <p>Hi all i get this error when i am trying to copy and paste a ssh2 public key that i have generated.</p>
  
  <p>here is the full stack trace of the error:</p>
  
  <pre><code>java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
      at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:188)
      at java.security.KeyFactory.generatePublic(KeyFactory.java:304)
      at com.josh.crypto.CryptoUtil.convertKeytoKeyObject(CryptoUtil.java:114)
      at com.josh.crypto.CryptoUtil.encryptData(CryptoUtil.java:80)
      at com.josh.ui.EncryptionView$2.actionPerformed(EncryptionView.java:112)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
      at java.awt.Component.processMouseEvent(Component.java:6382)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
      at java.awt.Component.processEvent(Component.java:6147)
      at java.awt.Container.processEvent(Container.java:2083)
      at java.awt.Component.dispatchEventImpl(Component.java:4744)
      at java.awt.Container.dispatchEventImpl(Container.java:2141)
      at java.awt.Component.dispatchEvent(Component.java:4572)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
      at java.awt.Container.dispatchEventImpl(Container.java:2127)
      at java.awt.Window.dispatchEventImpl(Window.java:2489)
      at java.awt.Component.dispatchEvent(Component.java:4572)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:704)
      at java.awt.EventQueue.access$400(EventQueue.java:82)
      at java.awt.EventQueue$2.run(EventQueue.java:663)
      at java.awt.EventQueue$2.run(EventQueue.java:661)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
      at java.awt.EventQueue$3.run(EventQueue.java:677)
      at java.awt.EventQueue$3.run(EventQueue.java:675)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:674)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
  Caused by: java.security.InvalidKeyException: invalid key format
      at sun.security.x509.X509Key.decode(X509Key.java:370)
      at sun.security.x509.X509Key.decode(X509Key.java:386)
      at sun.security.rsa.RSAPublicKeyImpl.&lt;init&gt;(RSAPublicKeyImpl.java:66)
      at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:281)
      at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:184)
      ... 40 more
  </code></pre>
  
  <p>Here is the public key plain text i am using to convert to a byte array </p>
  
  <pre><code>---- BEGIN SSH2 PUBLIC KEY ----
  Comment: "rsa-key-20130520"
  AAAAB3NzaC1yc2EAAAABJQAAAIEAsE8aR2CIWuQgkeOsPhHDuGo+Rokr2cT+KS5L
  sJFbEkB0R3XYXnOT3DU0CFrmHUX1PpcfTOSdxCIfeSXFHCGGWEXm4qx7ptNpm4vP
  Scuzmlr/fjuQdb7lBQ0+OEP2LKuRHxt5oEVZvq/EvwENS5T2BiVUSvTwXUS6SKCh
  ERydjXE=
  ---- END SSH2 PUBLIC KEY ----
  </code></pre>
  
  <p>And here is the code that converts to byte array and uses x509 keyspec</p>
  
  <pre><code>byte[] key = Base64.encodeBase64(publicKey.getBytes());
  x509KeySpec = new X509EncodedKeySpec(key);
          keyFactory = KeyFactory.getInstance("RSA");
          return keyFactory.generatePublic(x509KeySpec);
  </code></pre>