KeyCodec { public static KeyPair getKeyPair() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException { ECGenParameterSpec ecGenSpec = new ECGenParameterSpec("secp256r1"); KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC"); g.initialize(ecGenSpec, new SecureRandom()); return g.generateKeyPair(); }  static KeyPair getKeyPair(); static KeyPair getRSAKeyPair(); static RSAKeyParameters generatePrivateKeyParameter(RSAPrivateKey key); static byte[] getKeyAsRawBytes(String base64EncodedPubKey); static byte[] getKeyAsRawBytes(BCECPublicKey pub); @SuppressWarnings("deprecation") static byte[] getKeyAsRawBytes(
			org.bouncycastle.jce.interfaces.ECPublicKey pub); static byte[] asUnsignedByteArray(BigInteger value); static PublicKey getPubKey(byte[] bytes); static PrivateKey getPrivKey(byte[] bytes); static KeyPair generate(); static PublicKey getPubKeyFromCurve(byte[] pubKey, String curveName); static PublicKey getRSAPublicKey(byte[] encodedPubKey); static PrivateKey getPrivKeyFromCurve(byte[] privKey,
			String curveName);  }
@Test public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException, InvalidKeySpecException { KeyPair keyPair = KeyCodec.getKeyPair(); PrivateKey privKey = keyPair.getPrivate(); byte[] encodedPrivKey = privKey.getEncoded(); logger.info("priv=" + Base64.encodeBase64URLSafeString(encodedPrivKey)); PublicKey pubKey = keyPair.getPublic(); byte[] encodedPubKey = pubKey.getEncoded(); logger.info("pub=" + Base64.encodeBase64URLSafeString(encodedPubKey)); KeyFactory kf = KeyFactory.getInstance("ECDSA", "BC"); PublicKey pubKey2 = kf.generatePublic(new X509EncodedKeySpec( encodedPubKey)); assertTrue(Arrays.equals(pubKey2.getEncoded(), encodedPubKey)); PrivateKey privKey2 = kf.generatePrivate(new PKCS8EncodedKeySpec( encodedPrivKey)); assertTrue(Arrays.equals(privKey2.getEncoded(), encodedPrivKey)); }