title: How to generate 33-byte compressed NIST P-256 public key?
body: <p>I need to generate such public key and do the additional signing of the bytes (which will include this generated previously key)</p>
  
  <p>I need to construct bytes of:
  ASN.1 prefix + signature of (33-byte compressed NIST P-256 public key)</p>
  
  <p>The signature should be delivered from other defined private key</p>
  
  <p>The ECDSA specifications:</p>
  
  <p>● Curve: </p>
  
  <p>NIST P-256 Otherwise known as secp256r1 and prime256v1 (openssl)</p>
  
  <p>● Signature format
  ASN.1. The r and s values of the ECDSA signature must be positive
  integers, and DER-encoded as such.</p>
  
  <p>Is there API in Android to do such process? How can I use it then?</p>
  
  <p>WHAT I've tried: </p>
  
  <pre><code> try {
          val generator = KeyPairGenerator.getInstance("ECDSA")
          val ecSpec = ECNamedCurveTable
                  .getParameterSpec("prime256v1")
          generator.initialize(ecSpec)
          val keyPair = generator.generateKeyPair()
  
          val privKey = keyPair.private
          val encodedPrivKey = privKey.encoded
          System.out.println(toHex(encodedPrivKey))
  
          val pubKey = keyPair.public
          val encodedPubKey = pubKey.encoded
          System.out.println(toHex(encodedPubKey))
  
          val keyFactory = KeyFactory.getInstance("ECDSA")
          val pubKey2 = keyFactory.generatePublic(X509EncodedKeySpec(encodedPubKey))
          if (Arrays.equals(pubKey2.getEncoded(), encodedPubKey)) {
              println("That worked for the public key")
          }
  
          val privKey2 = keyFactory.generatePrivate(PKCS8EncodedKeySpec(encodedPrivKey))
          if (Arrays.equals(privKey2.getEncoded(), encodedPrivKey)) {
              println("That worked for the private key")
          }
  
      } catch (e: GeneralSecurityException) {
          throw IllegalStateException(e)
      }
  </code></pre>
  
  <p>Here - the encoded public key has the lenght of 90 bytes which i guess i want it to be 33 bytes</p>