Search in sources :

Example 91 with KeySpec

use of java.security.spec.KeySpec in project robovm by robovm.

the class SubjectPublicKeyInfo method getPublicKey.

/**
     * Returns the PublicKey corresponding to this SubjectPublicKeyInfo
     * instance.
     */
public PublicKey getPublicKey() {
    if (publicKey == null) {
        final byte[] encoded = getEncoded();
        final KeySpec keySpec = new X509EncodedKeySpec(encoded);
        /* Try using the algorithm name first. */
        final String algName = algorithmID.getAlgorithmName();
        publicKey = generateKeyForAlgorithm(keySpec, algName);
        /*
             * Fall back to using the algorithm OID if it's not the same as the
             * algorithm name.
             */
        final String algOid = algorithmID.getAlgorithm();
        if (publicKey == null && !algOid.equals(algName)) {
            publicKey = generateKeyForAlgorithm(keySpec, algOid);
        }
        /*
             * Encode this as an X.509 public key since we didn't have any
             * KeyFactory that could handle this algorithm name or OID. Perhaps
             * the thing that's using this can decode it.
             */
        if (publicKey == null) {
            publicKey = new X509PublicKey(algOid, encoded, subjectPublicKey);
        }
    }
    return publicKey;
}
Also used : KeySpec(java.security.spec.KeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) ASN1BitString(org.apache.harmony.security.asn1.ASN1BitString) BitString(org.apache.harmony.security.asn1.BitString)

Example 92 with KeySpec

use of java.security.spec.KeySpec in project neo4j by neo4j.

the class Certificates method loadPrivateKey.

public PrivateKey loadPrivateKey(File privateKeyFile) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
    try (PemReader r = new PemReader(new FileReader(privateKeyFile))) {
        PemObject pemObject = r.readPemObject();
        if (pemObject != null) {
            byte[] encodedKey = pemObject.getContent();
            KeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
            try {
                return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
            } catch (InvalidKeySpecException ignore) {
                try {
                    return KeyFactory.getInstance("DSA").generatePrivate(keySpec);
                } catch (InvalidKeySpecException ignore2) {
                    try {
                        return KeyFactory.getInstance("EC").generatePrivate(keySpec);
                    } catch (InvalidKeySpecException e) {
                        throw new InvalidKeySpecException("Neither RSA, DSA nor EC worked", e);
                    }
                }
            }
        }
    }
    // Ok, failed to read as PEM file, try and read it as a raw binary private key
    try (DataInputStream in = new DataInputStream(new FileInputStream(privateKeyFile))) {
        byte[] keyBytes = new byte[(int) privateKeyFile.length()];
        in.readFully(keyBytes);
        KeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        return KeyFactory.getInstance(DEFAULT_ENCRYPTION).generatePrivate(keySpec);
    }
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) FileReader(java.io.FileReader) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream)

Example 93 with KeySpec

use of java.security.spec.KeySpec in project robovm by robovm.

the class RSAMultiPrimePrivateCrtKeySpecTest method testRSAMultiPrimePrivateCrtKeySpec12.

/**
     * Test #12 for
     * <code>RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
     *                                      BigInteger publicExponent,
     *                                      BigInteger privateExponent,
     *                                      BigInteger primeP,
     *                                      BigInteger primeQ,
     *                                      BigInteger primeExponentP,
     *                                      BigInteger primeExponentQ,
     *                                      BigInteger crtCoefficient,
     *                                      RSAOtherPrimeInfo[] otherPrimeInfo)
     * </code> ctor<br>
     * Assertion: constructs <code>RSAMultiPrimePrivateCrtKeySpec</code>
     * object using valid parameters. Constructed object must be
     * instance of RSAPrivateKeySpec.
     */
public final void testRSAMultiPrimePrivateCrtKeySpec12() {
    KeySpec ks = new RSAMultiPrimePrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, opi);
    assertTrue(ks instanceof RSAPrivateKeySpec);
}
Also used : RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) RSAMultiPrimePrivateCrtKeySpec(java.security.spec.RSAMultiPrimePrivateCrtKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) RSAMultiPrimePrivateCrtKeySpec(java.security.spec.RSAMultiPrimePrivateCrtKeySpec)

Example 94 with KeySpec

use of java.security.spec.KeySpec in project robovm by robovm.

the class RSAMultiPrimePrivateCrtKeySpecTest method testRSAMultiPrimePrivateCrtKeySpec01.

// Test-cases:
/**
     * Test #1 for
     * <code>RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
     *                                      BigInteger publicExponent,
     *                                      BigInteger privateExponent,
     *                                      BigInteger primeP,
     *                                      BigInteger primeQ,
     *                                      BigInteger primeExponentP,
     *                                      BigInteger primeExponentQ,
     *                                      BigInteger crtCoefficient,
     *                                      RSAOtherPrimeInfo[] otherPrimeInfo)
     * </code> ctor<br>
     * Assertion: constructs <code>RSAMultiPrimePrivateCrtKeySpec</code>
     * object using valid parameters
     */
public final void testRSAMultiPrimePrivateCrtKeySpec01() {
    KeySpec ks = new RSAMultiPrimePrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, opi);
    assertTrue(ks instanceof RSAMultiPrimePrivateCrtKeySpec);
}
Also used : RSAMultiPrimePrivateCrtKeySpec(java.security.spec.RSAMultiPrimePrivateCrtKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) RSAMultiPrimePrivateCrtKeySpec(java.security.spec.RSAMultiPrimePrivateCrtKeySpec)

Example 95 with KeySpec

use of java.security.spec.KeySpec in project robovm by robovm.

the class RSAPrivateCrtKeySpecTest method testRSAPrivateCrtKeySpec01.

/**
     * Test #1 for <code>RSAPrivateCrtKeySpec</code> constructor
     * Assertion: Constructs <code>RSAPrivateCrtKeySpec</code>
     * object using valid parameters
     */
public final void testRSAPrivateCrtKeySpec01() {
    KeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE);
    assertTrue(ks instanceof RSAPrivateCrtKeySpec);
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Aggregations

KeySpec (java.security.spec.KeySpec)171 PBEKeySpec (javax.crypto.spec.PBEKeySpec)71 SecretKeyFactory (javax.crypto.SecretKeyFactory)65 KeyFactory (java.security.KeyFactory)61 SecretKeySpec (javax.crypto.spec.SecretKeySpec)50 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)41 BigInteger (java.math.BigInteger)40 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)40 SecretKey (javax.crypto.SecretKey)39 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)38 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)34 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)29 PublicKey (java.security.PublicKey)27 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)24 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)21 PrivateKey (java.security.PrivateKey)19 IOException (java.io.IOException)18 Cipher (javax.crypto.Cipher)16 InvalidKeyException (java.security.InvalidKeyException)15