title: DER encoded RSA public key (OCTET STRING )
body: <p>We are trying to create a RSA public key which should be octet string and then we need to make it DER encoded. </p>
  
  <p>We are trying to create a key pair using below code:</p>
  
  <pre><code>public static KeyPair getRSAKeyPair() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
      KeyPairGenerator g = KeyPairGenerator.getInstance("RSA", "SC");
      g.initialize(2048);
      return g.generateKeyPair();
  </code></pre>
  
  <p>}</p>
  
  <p>and then getting public key from it using below code</p>
  
  <pre><code>PublicKey publicKey1 =  keyPair.getPublic();
  byte[] pubkey =  publicKey1.getEncoded();
  </code></pre>
  
  <p>It seems public key getting fetched in above code are DER encoded binary string.</p>
  
  <p>Queries:</p>
  
  <ol>
  <li>How can I change the above code so that it creates an octet string.</li>
  <li>How can I make it DER encoded. </li>
  </ol>