Search in sources :

Example 26 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project spring-security-oauth by spring-projects.

the class RSAKeySecret method createPublicKey.

/**
 * Creates a public key from the X509-encoded value of the given bytes.
 *
 * @param publicKey The X509-encoded public key bytes.
 * @return The public key.
 */
public static PublicKey createPublicKey(byte[] publicKey) {
    if (publicKey == null) {
        return null;
    }
    try {
        KeyFactory fac = KeyFactory.getInstance("RSA");
        EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
        return fac.generatePublic(spec);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException(e);
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 27 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project spring-security-oauth by spring-projects.

the class RSAKeySecret method createPrivateKey.

/**
 * Creates a private key from the PKCS#8-encoded value of the given bytes.
 *
 * @param privateKey The PKCS#8-encoded private key bytes.
 * @return The private key.
 */
public static PrivateKey createPrivateKey(byte[] privateKey) {
    if (privateKey == null) {
        return null;
    }
    try {
        KeyFactory fac = KeyFactory.getInstance("RSA");
        EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
        return fac.generatePrivate(spec);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException(e);
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 28 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project data-transfer-project by google.

the class PublicPrivateKeyPairGenerator method parsePublicKey.

/**
 * Parses the given {@code encoded} public key.
 */
public static PublicKey parsePublicKey(String encoded) {
    byte[] decoded = BaseEncoding.base64Url().decode(encoded);
    EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
    KeyFactory keyFactory;
    try {
        keyFactory = KeyFactory.getInstance(ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        logger.error("NoSuchAlgorithmException for: {}", ALGORITHM, e);
        throw new RuntimeException("NoSuchAlgorithmException generating public keyFactory", e);
    }
    try {
        return keyFactory.generatePublic(spec);
    } catch (InvalidKeySpecException e) {
        logger.error("InvalidKeySpecException for: {}", spec.getEncoded().length, e);
        throw new RuntimeException("InvalidKeySpecException generating public key", e);
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 29 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project spring-security by spring-projects.

the class NimbusJwtDecoderTests method key.

private RSAPublicKey key() throws InvalidKeySpecException {
    byte[] decoded = Base64.getDecoder().decode(VERIFY_KEY.getBytes());
    EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
    return (RSAPublicKey) kf.generatePublic(spec);
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) EncodedKeySpec(java.security.spec.EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 30 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project spring-security by spring-projects.

the class NimbusReactiveJwtDecoderTests method key.

private RSAPublicKey key() throws InvalidKeySpecException {
    byte[] decoded = Base64.getDecoder().decode(this.publicKey.getBytes());
    EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
    return (RSAPublicKey) kf.generatePublic(spec);
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) EncodedKeySpec(java.security.spec.EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Aggregations

EncodedKeySpec (java.security.spec.EncodedKeySpec)34 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)21 KeyFactory (java.security.KeyFactory)19 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)15 MyEncodedKeySpec (org.apache.harmony.security.tests.support.spec.MyEncodedKeySpec)8 PublicKey (java.security.PublicKey)3 RSAPublicKey (java.security.interfaces.RSAPublicKey)3 SecretKeyFactory (javax.crypto.SecretKeyFactory)3 TypedArray (android.content.res.TypedArray)2 IOException (java.io.IOException)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 PKCS1EncodedKeySpec (net.oauth.signature.pem.PKCS1EncodedKeySpec)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyPair (java.security.KeyPair)1