Crypto { public static PublicKey extractPublicKey(PrivateKey privateKey) throws CryptoException { PublicKey publicKey; switch (privateKey.getAlgorithm()) { case RSA: try { KeyFactory kf = KeyFactory.getInstance(getRSAAlgo(), getProvider()); RSAPrivateCrtKey rsaCrtKey = (RSAPrivateCrtKey) privateKey; RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaCrtKey.getModulus(), rsaCrtKey.getPublicExponent()); publicKey = kf.generatePublic(keySpec); } catch (NoSuchProviderException ex) { LOG.error("extractPublicKey: RSA - Caught NoSuchProviderException exception: " + ex.getMessage()); throw new CryptoException(ex); } catch (NoSuchAlgorithmException ex) { LOG.error("extractPublicKey: RSA - Caught NoSuchAlgorithmException exception: " + ex.getMessage()); throw new CryptoException(ex); } catch (InvalidKeySpecException ex) { LOG.error("extractPublicKey: RSA - Caught InvalidKeySpecException exception: " + ex.getMessage()); throw new CryptoException(ex); } break; case ECDSA: try { KeyFactory kf = KeyFactory.getInstance(getECDSAAlgo(), getProvider()); BCECPrivateKey ecPrivKey = (BCECPrivateKey) privateKey; ECMultiplier ecMultiplier = new FixedPointCombMultiplier(); ECParameterSpec ecParamSpec = ecPrivKey.getParameters(); ECPoint ecPointQ = ecMultiplier.multiply(ecParamSpec.getG(), ecPrivKey.getD()); ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPointQ, ecParamSpec); publicKey = kf.generatePublic(keySpec); } catch (NoSuchProviderException ex) { LOG.error("extractPublicKey: ECDSA - Caught NoSuchProviderException exception: " + ex.getMessage()); throw new CryptoException(ex); } catch (NoSuchAlgorithmException ex) { LOG.error("extractPublicKey: ECDSA - Caught NoSuchAlgorithmException exception: " + ex.getMessage()); throw new CryptoException(ex); } catch (InvalidKeySpecException ex) { LOG.error("extractPublicKey: ECDSA - Caught InvalidKeySpecException exception: " + ex.getMessage()); throw new CryptoException(ex); } break; default: String msg = "Unsupported Key Algorithm: " + privateKey.getAlgorithm(); LOG.error("extractPublicKey: " + msg); throw new CryptoException(msg); } return publicKey; }  static String hmac(String message, String sharedSecret); static String sign(String message, PrivateKey key, String digestAlgorithm); static byte[] sign(byte[] message, PrivateKey key, String digestAlgorithm); static String sign(String message, PrivateKey key); static boolean verify(String message, PublicKey key, String signature,
                                 String digestAlgorithm); static boolean verify(String message, PublicKey key, String signature); static boolean verify(byte[] message, PublicKey key, byte[] signature,
                                 String digestAlgorithm); static byte [] sha256(byte [] data); static byte [] sha256(String text); static String ybase64(byte [] data); static byte [] ybase64Decode(String b64); static String ybase64DecodeString(String b64); static String ybase64EncodeString(String str); static String x509CertificatesToPEM(X509Certificate[] x509Certs); static X509Certificate[] loadX509Certificates(final String certsFile); static X509Certificate loadX509Certificate(File certFile); static X509Certificate loadX509Certificate(String pemEncoded); static X509Certificate loadX509Certificate(Reader reader); static PublicKey loadPublicKey(String pemEncoded); static PublicKey loadPublicKey(Reader r); static PublicKey loadPublicKey(File f); static PublicKey extractPublicKey(PrivateKey privateKey); static PrivateKey loadPrivateKey(String pemEncoded); static PrivateKey loadPrivateKey(Reader reader); static PrivateKey loadPrivateKey(File file); static PrivateKey loadPrivateKey(File file, String pwd); static PrivateKey loadPrivateKey(String pemEncoded, String pwd); static PrivateKey loadPrivateKey(Reader reader, String pwd); static PrivateKey generateRSAPrivateKey(int bits); static String randomSalt(); static String encodedFile(File f); static String encodedFile(FileInputStream is); static PKCS10CertificationRequest getPKCS10CertRequest(String csr); static String extractX509CSRSubjectField(PKCS10CertificationRequest certReq, ASN1ObjectIdentifier id); static String extractX509CSRCommonName(PKCS10CertificationRequest certReq); static String extractX509CSRSubjectOField(PKCS10CertificationRequest certReq); static String extractX509CSRSubjectOUField(PKCS10CertificationRequest certReq); static String extractX509CSREmail(PKCS10CertificationRequest certReq); static List<String> extractX509CSREmails(PKCS10CertificationRequest certReq); static List<String> extractX509CSRDnsNames(PKCS10CertificationRequest certReq); static List<String> extractX509CSRURIs(PKCS10CertificationRequest certReq); static List<String> extractX509CSRIPAddresses(PKCS10CertificationRequest certReq); static String extractX509CSRPublicKey(PKCS10CertificationRequest certReq); static String generateX509CSR(PrivateKey privateKey, String x500Principal,
                                         GeneralName[] sanArray); static String generateX509CSR(PrivateKey privateKey, PublicKey publicKey,
                                         String x500Principal, GeneralName[] sanArray); static String extractX509CertSubjectField(X509Certificate x509Cert, ASN1ObjectIdentifier id); static long extractX509CertIssueTime(X509Certificate x509Cert); static String extractX509CertCommonName(X509Certificate x509Cert); static String extractX509CertSubjectOUField(X509Certificate x509Cert); static String extractX509CertSubjectOField(X509Certificate x509Cert); static boolean isRestrictedCertificate(X509Certificate x509Cert, GlobStringsMatcher globStringsMatcher); static List<String> extractX509CertDnsNames(X509Certificate x509Cert); static List<String> extractX509CertEmails(X509Certificate x509Cert); static List<String> extractX509CertIPAddresses(X509Certificate x509Cert); static List<String> extractX509CertURIs(X509Certificate x509Cert); static String extractX509CertPublicKey(X509Certificate x509Cert); static X509Certificate generateX509Certificate(PKCS10CertificationRequest certReq,
            PrivateKey caPrivateKey, X509Certificate caCertificate, int validityTimeout,
            boolean basicConstraints); static X509Certificate generateX509Certificate(PKCS10CertificationRequest certReq,
            PrivateKey caPrivateKey, X500Name issuer, int validityTimeout,
            boolean basicConstraints); static boolean validatePKCS7Signature(String data, String signature, PublicKey publicKey); static String convertToPEMFormat(Object obj); static void main(String[] args); static final String SHA1; static final String SHA256; }
@Test public void testExctractPublicKeyDefault() { PrivateKey privateKey = mock(PrivateKey.class); when(privateKey.getAlgorithm()).thenReturn("TestAlgo"); assertThrows(CryptoException.class, () -> { Crypto.extractPublicKey(privateKey); }); }