title: How to encrypt and decrypt data in Android using the elliptic curve key pair of type secp256r1?
body: <p>I need to use NIST P-256 elliptic curves to encrypt and decrypt data. Now that I have generated the key pair, but how do I use them to encrypt and decrypt?</p>
  
  <p>The official website only says how to use this ec key pair to sign/verify, but I want to know how to use this ec key pair to encrypt/decrypt.</p>
  
  <p>website: <a href="https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec#example:-nist-p-256-ec-key-pair-for-signingverification-using-ecdsa" rel="noreferrer">https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec#example:-nist-p-256-ec-key-pair-for-signingverification-using-ecdsa</a></p>
  
  <p>generate NIST P-256 key pair code:</p>
  
  <pre><code>        val kpg: KeyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore")
          val parameterSpec =
              KeyGenParameterSpec.Builder("container", KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
                  .setAlgorithmParameterSpec(ECGenParameterSpec("secp256r1"))
                  .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA384, KeyProperties.DIGEST_SHA512)
                  .build()
          kpg.initialize(parameterSpec)
          val keyPair = kpg.generateKeyPair()
  
          val ecPublicKey = keyPair.public as ECPublicKey
          val ecPrivateKey = keyPair.private as ECPrivateKey
  </code></pre>