Java Utililty Methods Key Public

List of utility methods to do Key Public

Description

The list of methods to do Key Public are organized into topic(s).

Method

PublicKeygetPublicKey(String publicKeyContents)
Returns the public key for the given key.
PublicKey publicKey = null;
try {
    byte[] decodeKeyBytes = decodeToBase64Bytes(publicKeyContents);
    KeyFactory keyFactory = KeyFactory.getInstance(ALGO_RSA);
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeKeyBytes);
    publicKey = keyFactory.generatePublic(publicKeySpec);
} catch (NoSuchAlgorithmException ex) {
    System.err.println(ex);
...
RSAPublicKeygetPublicKey(String publicKeyFile)
get Public Key
ObjectInputStream publicKeyInputStream;
try {
    publicKeyInputStream = new ObjectInputStream(new FileInputStream(publicKeyFile));
    RSAPublicKey publicKey = (RSAPublicKey) publicKeyInputStream.readObject();
    publicKeyInputStream.close();
    return publicKey;
} catch (IOException e) {
    e.printStackTrace();
...
PublicKeygetPublicKey(String publicKeyFilepath, String algorithm)
Creates a PublicKey from the specified public key file and algorithm.
InputStream pubKey = null;
try {
    URL url = new URL(publicKeyFilepath);
    pubKey = url.openStream();
} catch (MalformedURLException e) {
    pubKey = new FileInputStream(publicKeyFilepath);
byte[] bytes = new byte[pubKey.available()];
...
PublicKeygetPublicKeyFromBytes(final String algorithm, final byte[] publicKeyBytes)
get Public Key From Bytes
try {
    final KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    final X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes);
    return keyFactory.generatePublic(spec);
} catch (Throwable t) {
    t.printStackTrace();
return null;
...
PublicKeygetPublicKeyFromFile(File cert, String alias, String password)
get Public Key From File
KeyStore ks = KeyStore.getInstance("JKS");
char[] pwd = password.toCharArray();
InputStream is = new FileInputStream(cert);
ks.load(is, pwd);
Key key = ks.getKey(alias, pwd);
Certificate c = ks.getCertificate(alias);
PublicKey p = c.getPublicKey();
return p;
...
PublicKeygetPublicKeyFromPEMFile(String fileName, String jceProvider)
Reads the public key from a PEM formatted file (via the cert)
_jceProvider = jceProvider;
if (_debug) {
    System.out.println("getPublicKeyFromPEMFile: Reading public key from " + fileName);
return getCertFromPEMFile(fileName, _jceProvider).getPublicKey();
RSAPublicKeygetPublicKeyFromString(String certificateString)
Get RSA public key from X.509 certificate string (full crt file content, including header and footer)
CertificateFactory fact = CertificateFactory.getInstance("X.509");
ByteArrayInputStream is = new ByteArrayInputStream(certificateString.getBytes("UTF8"));
X509Certificate cer = (X509Certificate) fact.generateCertificate(is);
return (RSAPublicKey) cer.getPublicKey();
StringgetPublicKeyModulus(RSAPublicKey publicKey)
get Public Key Modulus
return publicKey.getModulus().toString(16);
RSAPublicKeySpecgetPublicKeySpec(KeyPair kp)
get Public Key Spec
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
return pub;
MethodgetPublicPutMethodForResultClass(final Class resultClass)
Convenience method to return the put(Object, Object method for the result class.
return (Method) AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
        try {
            return resultClass.getMethod("put", classArrayObjectObject);
        } catch (NoSuchMethodException ex) {
            return null;
});