/** * @deprecated * Generates a public key from the given key spec. * * @param keySpec {@link KeySpec} specification for the key * @param keyAlgorithm key generation algorithm, only DSA and RSA supported * * @return the generated {@link PublicKey} * * @throws KeyException thrown if the key algorithm is not supported by the JCE or the key spec does not * contain valid information */ public static PublicKey buildKey(KeySpec keySpec, String keyAlgorithm) throws KeyException { return SecurityHelper.buildKey(keySpec, keyAlgorithm); }
/** * @deprecated * Generates a public key from the given key spec. * * @param keySpec {@link KeySpec} specification for the key * @param keyAlgorithm key generation algorithm, only DSA and RSA supported * * @return the generated {@link PublicKey} * * @throws KeyException thrown if the key algorithm is not supported by the JCE or the key spec does not * contain valid information */ public static PublicKey buildKey(KeySpec keySpec, String keyAlgorithm) throws KeyException { return SecurityHelper.buildKey(keySpec, keyAlgorithm); }
/** * Decodes RSA/DSA public keys in DER-encoded "SubjectPublicKeyInfo" format. * * @param key encoded key * @param password password if the key is encrypted or null if not * * @return decoded key * * @throws KeyException thrown if the key can not be decoded */ public static PublicKey decodePublicKey(byte[] key, char[] password) throws KeyException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key); try { return buildKey(keySpec, "RSA"); } catch (KeyException ex) { } try { return buildKey(keySpec, "DSA"); } catch (KeyException ex) { } try { return buildKey(keySpec, "EC"); } catch (KeyException ex) { } throw new KeyException("Unsupported key type."); }
/** * Decodes RSA/DSA public keys in DER-encoded "SubjectPublicKeyInfo" format. * * @param key encoded key * @param password password if the key is encrypted or null if not * * @return decoded key * * @throws KeyException thrown if the key can not be decoded */ public static PublicKey decodePublicKey(byte[] key, char[] password) throws KeyException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key); try { return buildKey(keySpec, "RSA"); } catch (KeyException ex) { } try { return buildKey(keySpec, "DSA"); } catch (KeyException ex) { } try { return buildKey(keySpec, "EC"); } catch (KeyException ex) { } throw new KeyException("Unsupported key type."); }
/** * Build Java RSA public key from base64 encoding. * * @param base64EncodedKey base64-encoded RSA public key * @return a native Java RSAPublicKey * @throws KeyException thrown if there is an error constructing key */ public static RSAPublicKey buildJavaRSAPublicKey(String base64EncodedKey) throws KeyException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decode(base64EncodedKey)); return (RSAPublicKey) buildKey(keySpec, "RSA"); }
/** * Build Java RSA public key from base64 encoding. * * @param base64EncodedKey base64-encoded RSA public key * @return a native Java RSAPublicKey * @throws KeyException thrown if there is an error constructing key */ public static RSAPublicKey buildJavaRSAPublicKey(String base64EncodedKey) throws KeyException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decode(base64EncodedKey)); return (RSAPublicKey) buildKey(keySpec, "RSA"); }
/** * Build Java DSA public key from base64 encoding. * * @param base64EncodedKey base64-encoded DSA public key * @return a native Java DSAPublicKey * @throws KeyException thrown if there is an error constructing key */ public static DSAPublicKey buildJavaDSAPublicKey(String base64EncodedKey) throws KeyException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decode(base64EncodedKey)); return (DSAPublicKey) buildKey(keySpec, "DSA"); }
/** * Build Java EC public key from base64 encoding. * * @param base64EncodedKey base64-encoded EC public key * @return a native Java ECPublicKey * @throws KeyException thrown if there is an error constructing key */ public static ECPublicKey buildJavaECPublicKey(String base64EncodedKey) throws KeyException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decode(base64EncodedKey)); return (ECPublicKey) buildKey(keySpec, "EC"); }
/** * Build Java DSA public key from base64 encoding. * * @param base64EncodedKey base64-encoded DSA public key * @return a native Java DSAPublicKey * @throws KeyException thrown if there is an error constructing key */ public static DSAPublicKey buildJavaDSAPublicKey(String base64EncodedKey) throws KeyException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decode(base64EncodedKey)); return (DSAPublicKey) buildKey(keySpec, "DSA"); }
/** * Build Java EC public key from base64 encoding. * * @param base64EncodedKey base64-encoded EC public key * @return a native Java ECPublicKey * @throws KeyException thrown if there is an error constructing key */ public static ECPublicKey buildJavaECPublicKey(String base64EncodedKey) throws KeyException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decode(base64EncodedKey)); return (ECPublicKey) buildKey(keySpec, "EC"); }