Example usage for java.security.spec X509EncodedKeySpec X509EncodedKeySpec

List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec

Introduction

In this page you can find the example usage for java.security.spec X509EncodedKeySpec X509EncodedKeySpec.

Prototype

public X509EncodedKeySpec(byte[] encodedKey) 

Source Link

Document

Creates a new X509EncodedKeySpec with the given encoded key.

Usage

From source file:net.sourceforge.msscodefactory.cflib.v2_1.CFLib.Tip.CFTipClientHandler.java

public void setEncodedServerPublicKey(byte encoded[])
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException {
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(encoded);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    serverPublicKey = kf.generatePublic(x509KeySpec);
}

From source file:com.adeptj.modules.security.jwt.internal.JwtKeys.java

static PublicKey createVerificationKey(SignatureAlgorithm signatureAlgo, String publicKey) {
    LOGGER.info("Creating RSA verification key!!");
    Assert.isTrue(StringUtils.startsWith(publicKey, PUB_KEY_HEADER), INVALID_PUBLIC_KEY_MSG);
    try {//from  w w w  . ja  v a  2  s  . c  o m
        KeyFactory keyFactory = KeyFactory.getInstance(signatureAlgo.getFamilyName());
        byte[] publicKeyData = Base64.getDecoder().decode(publicKey.replace(PUB_KEY_HEADER, EMPTY)
                .replace(PUB_KEY_FOOTER, EMPTY).replaceAll(REGEX_SPACE, EMPTY).trim().getBytes(UTF_8));
        LOGGER.info("Creating X509EncodedKeySpec from public key !!");
        return keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyData));
    } catch (GeneralSecurityException ex) {
        LOGGER.error(ex.getMessage(), ex);
        throw new KeyInitializationException(ex.getMessage(), ex);
    }
}

From source file:net.jmhertlein.core.crypto.Keys.java

private static X509EncodedKeySpec getX509KeySpec(String file) {
    byte[] decoded;
    try (Scanner scan = new Scanner(new File(file))) {
        String output = "";
        while (scan.hasNextLine()) {
            output += scan.nextLine();//from w  w w.  j  av  a2  s .  c o m
        }

        decoded = Base64.decodeBase64(output);

    } catch (IOException ioe) {
        return null;
    }

    return new X509EncodedKeySpec(decoded);
}

From source file:org.ebayopensource.fido.uaf.crypto.KeyCodec.java

public static PublicKey getPubKey(byte[] bytes)
        throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {
    KeyFactory kf = KeyFactory.getInstance("ECDSA", "SC");
    return kf.generatePublic(new X509EncodedKeySpec(bytes));
}

From source file:com.launchkey.sdk.crypto.JCECrypto.java

/**
 * Get an RSA public key utilizing the provided provider and PEM formatted string
 * @param provider Provider to generate the key
 * @param pem PEM formatted key string//from   w  ww. j a  va 2  s.  c  om
 * @return
 */
public static RSAPublicKey getRSAPublicKeyFromPEM(Provider provider, String pem) {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA", provider);
        return (RSAPublicKey) keyFactory.generatePublic(new X509EncodedKeySpec(getKeyBytesFromPEM(pem)));
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("Algorithm SHA256withRSA is not available", e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalArgumentException("Invalid PEM provided", e);
    }
}

From source file:net.unicon.cas.support.wsfederation.WsFederationUtils.java

/**
 * getSigningCredential loads up an X509Credential from a file.
 *
 * @param resource the signing certificate file
 * @return an X509 credential// ww  w .j av a  2s .c  om
 */
public static X509Credential getSigningCredential(final Resource resource) {
    try (final InputStream inputStream = resource.getInputStream()) {
        //grab the certificate file
        final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        final X509Certificate certificate = (X509Certificate) certificateFactory
                .generateCertificate(inputStream);

        //get the public key from the certificate
        final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
                certificate.getPublicKey().getEncoded());

        //generate public key to validate signatures
        final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        final PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

        //add the public key
        final BasicX509Credential publicCredential = new BasicX509Credential();
        publicCredential.setPublicKey(publicKey);
        LOGGER.debug("getSigningCredential: key retrieved.");
        return publicCredential;
    } catch (final Exception ex) {
        LOGGER.error("I/O error retrieving the signing cert: {}", ex);
        return null;
    }
}

From source file:com.forsrc.utils.MyRsa2Utils.java

/**
 * Gets public key./* www. j  ava 2s  . co m*/
 *
 * @param key the key
 * @return the public key
 * @throws RsaException the rsa exception
 */
public static PublicKey getPublicKey(String key) throws RsaException {
    byte[] keyBytes;
    try {
        keyBytes = (new Base64()).decode(key);
    } catch (Exception e) {
        throw new RsaException(e);
    }
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = null;
    try {
        keyFactory = KeyFactory.getInstance(RsaKey.ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        throw new RsaException(e);
    }
    PublicKey publicKey = null;
    try {
        publicKey = keyFactory.generatePublic(keySpec);
    } catch (InvalidKeySpecException e) {
        throw new RsaException(e);
    }
    return publicKey;
}

From source file:com.sammyun.util.RSAUtils.java

/**
 * RSA??//from  w w  w  .  ja  va 2 s.co  m
 * 
 * @param content ???
 * @return
 */
public static String encryptContent(String content, String ali_public_key) {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] encodedKey = Base64Util.decode(ali_public_key);
        PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
        java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);
        signature.initVerify(pubKey);
        signature.update(content.getBytes("utf-8"));
        byte[] signed = signature.sign();
        return Base64Util.encode(signed);
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return "";
    }
}

From source file:com.streamsets.datacollector.publicrestapi.CredentialsDeploymentResource.java

private boolean validateSignature(CredentialsBeanJson credentialsBeanJson)
        throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {
    // getProperty so we can test it
    String publicKey = Preconditions.checkNotNull(System.getProperty(DPM_AGENT_PUBLIC_KEY));

    X509EncodedKeySpec kspec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey));
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PublicKey key = kf.generatePublic(kspec);
    Signature sig = Signature.getInstance("SHA256withRSA");
    sig.initVerify(key);//from   w ww. jav a 2 s. c  o m
    sig.update(credentialsBeanJson.getToken().getBytes(Charsets.UTF_8));
    LOG.info("Token : {}, Signature {}", credentialsBeanJson.getToken(),
            credentialsBeanJson.getTokenSignature());
    return sig.verify(Base64.getDecoder().decode(credentialsBeanJson.getTokenSignature()));
}

From source file:org.psl.fidouaf.core.crypto.KeyCodec.java

public static PublicKey getPubKey(byte[] bytes)
        throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {
    KeyFactory kf = KeyFactory.getInstance("ECDSA", "BC");
    return kf.generatePublic(new X509EncodedKeySpec(bytes));
}