Java Key Public getPublicKey(byte[] keyBytes, String algorithm)

Here you can find the source of getPublicKey(byte[] keyBytes, String algorithm)

Description

get Public Key

License

Open Source License

Declaration

private static PublicKey getPublicKey(byte[] keyBytes, String algorithm) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;

import java.security.PublicKey;

import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;

import java.security.spec.X509EncodedKeySpec;

public class Main {
    private static PublicKey getPublicKey(byte[] keyBytes, String algorithm) {
        PublicKey publicKey = null;
        try {/*  ww w  .ja  v a  2  s.  co  m*/
            KeyFactory kf = KeyFactory.getInstance(algorithm);
            EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
            publicKey = kf.generatePublic(keySpec);
        } catch (NoSuchAlgorithmException e) {
            System.out
                    .println("Could not reconstruct the public key, the given algorithm could not be found.");
        } catch (InvalidKeySpecException e) {
            System.out.println("Could not reconstruct the public key");
        }

        return publicKey;
    }
}

Related

  1. getPublicEncoded(KeyPair kp)
  2. getPublicExponent(PublicKey pubk)
  3. getPublicKey()
  4. getPublicKey(BigInteger modulus, BigInteger exponent)
  5. getPublicKey(byte[] der)
  6. getPublicKey(final byte[] keyData)
  7. getPublicKey(final byte[] modulus, final byte[] exponent)
  8. getPublicKey(final String algorithm, final File publicKeyFile)
  9. getPublicKey(KeyPair keyPair)