title: how to convert ssh-rsa public key to PEM PKCS#1 public key format using java 7
body: <p>My Public in ssh-rsa is:</p>
  
  <p>ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQClAxT5S/WuX04OXBt9R59WcL45OmaU3M5U063lfyja7ovqaVR7/2kHtLF/LoCQCXSZMny8RTCGDjoXD7G/tGsyHFDHCI//Y1VDLE06AlDzrlu69DQY91+6gkhGjH3SF6us5hXlihrbSFLfAlSdkEs8gwSrspVQyuaOf+39dnMddhEDYYg+z0ce82ta/n8xPBWCp60nDEDayNjOsRgzDJKSujNfngjQTL1x6qKJj8BW/P5lLJE1nbMm9BQD9G7glJk86qh1I/tJCnij6On0m6KcdzVz8cU3sBgNeB433kGjJtpxXXmJB6Vuu5IverhyfpiB4hP9WlKa/LSzW+ZIdvl/</p>
  
  <p>I wanted to convert it to PKCS#1 below format</p>
  
  <p>-----BEGIN RSA PUBLIC KEY-----
  MIIBCgKCAQEAoL7K8ijUPfkkpdiIMIoHxzTn+npLzJGHr7QO09Mnz8Q1xFfv9Arr
  3Eg2R7LdTMJ+v2Rk3EDRGJEwK8cWv5Hvh7KXIdRqtw2ouxY6pK9RXfvaQ0CF2nI9
  Y7VxASdOvnI8byzlw9SvR1eDjpzIh8/wtz3KcJK5aCRsAyH6ddvM4tPisQaKVBGN
  nCtPq94PspbpEYbNSQXTmnpO099P99oDXT+kpCaNrPvM5xWmUj0R7UwbmYYdNnVB
  UPFTKj+qVpp8TUTFUgk8noke7ABK6tncf8wmB8mPhgoKznZP5MaLZpB1f4gBeXst
  GNiLN4LwgRSM56eCEVqQHkVRuRrM9nODWQIDAQAB</p>
  
  <p>-----END RSA PUBLIC KEY-----</p>
  
  <p>Summary : I want to verify file by using above public key in java 7 as i am facing the invalid key format error for ssh-rsa public key kindly let me know what need to be done.</p>
  
  <p>please find below snippet:</p>
  
  <pre><code>byte[] decoded;
          try {
          decoded = Base64.getDecoder().decode(publickeyStr);
          org.bouncycastle.asn1.pkcs.RSAPublicKey pkcs1PublicKey = org.bouncycastle.asn1.pkcs.RSAPublicKey.getInstance(decoded);
          BigInteger modulus = pkcs1PublicKey.getModulus();
          BigInteger publicExponent = pkcs1PublicKey.getPublicExponent();
          RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, publicExponent);
          KeyFactory kf;
          kf = KeyFactory.getInstance("RSA");
          PublicKey generatedPublic = kf.generatePublic(keySpec);
          System.out.printf("Modulus: %X%n", modulus);
          System.out.printf("Public exponent: %d ... 17? Why?%n", publicExponent);
          System.out.printf("See, Java class result: %s, is RSAPublicKey: %b%n", generatedPublic.getClass().getName(), generatedPublic instanceof RSAPublicKey);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>it is giving below exception:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Illegal base64 character 2d
      at java.util.Base64$Decoder.decode0(Base64.java:714)
      at java.util.Base64$Decoder.decode(Base64.java:526)
      at java.util.Base64$Decoder.decode(Base64.java:549)
      at SignVerify.convertPublicPKCS1StrToPKCS8PublicKeyObj(SignVerify.java:136)
      at SignVerify.main(SignVerify.java:184)
  </code></pre>