Search in sources :

Example 46 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project lucene-solr by apache.

the class CryptoKeys method getX509PublicKey.

/**
   * Create PublicKey from a .DER file
   */
public static PublicKey getX509PublicKey(byte[] buf) throws Exception {
    X509EncodedKeySpec spec = new X509EncodedKeySpec(buf);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePublic(spec);
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 47 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project karaf by apache.

the class KnownHostsManager method getKnownKeyInternal.

private PublicKey getKnownKeyInternal(SocketAddress remoteAddress, String checkAlgorithm, BufferedReader reader) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    String checkServerAddress = getAddressString(remoteAddress);
    String line = reader.readLine();
    while (line != null) {
        String[] lineParts = line.split(" ");
        String serverAddress = lineParts[0];
        String algorithm = lineParts[1];
        if (checkServerAddress.equals(serverAddress) && checkAlgorithm.equals(algorithm)) {
            byte[] key = Base64.getDecoder().decode(lineParts[2].getBytes());
            KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key);
            return keyFactory.generatePublic(keySpec);
        }
        line = reader.readLine();
    }
    return null;
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 48 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project cloudstack by apache.

the class SAMLUtils method savePublicKey.

public static String savePublicKey(PublicKey key) {
    try {
        KeyFactory keyFactory = SAMLUtils.getKeyFactory();
        if (keyFactory == null)
            return null;
        X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class);
        return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()), Charset.forName("UTF-8"));
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create KeyFactory:" + e.getMessage());
    }
    return null;
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 49 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project cloudstack by apache.

the class SAMLUtils method loadPublicKey.

public static PublicKey loadPublicKey(String publicKey) {
    byte[] sigBytes = org.bouncycastle.util.encoders.Base64.decode(publicKey);
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(sigBytes);
    KeyFactory keyFact = SAMLUtils.getKeyFactory();
    if (keyFact == null)
        return null;
    try {
        return keyFact.generatePublic(x509KeySpec);
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create PrivateKey from privateKey string:" + e.getMessage());
    }
    return null;
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 50 with X509EncodedKeySpec

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

the class X509CertificateTest method getRsaCertificatePublicKey.

private PublicKey getRsaCertificatePublicKey() throws Exception {
    final InputStream ris = Support_Resources.getStream("x509/cert-rsa-pubkey.der");
    try {
        final int size = ris.available();
        final DataInputStream is = new DataInputStream(ris);
        final byte[] keyBytes = new byte[size];
        is.readFully(keyBytes);
        final KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePublic(new X509EncodedKeySpec(keyBytes));
    } finally {
        try {
            ris.close();
        } catch (IOException ignored) {
        }
    }
}
Also used : DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) KeyFactory(java.security.KeyFactory)

Aggregations

X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)162 KeyFactory (java.security.KeyFactory)112 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)93 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)80 PublicKey (java.security.PublicKey)65 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)45 InvalidKeyException (java.security.InvalidKeyException)30 PrivateKey (java.security.PrivateKey)27 IOException (java.io.IOException)26 RSAPublicKey (java.security.interfaces.RSAPublicKey)20 Signature (java.security.Signature)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 BigInteger (java.math.BigInteger)11 CertificateException (java.security.cert.CertificateException)10 X509Certificate (java.security.cert.X509Certificate)10 EncodedKeySpec (java.security.spec.EncodedKeySpec)10 SecretKey (javax.crypto.SecretKey)9 KeyPair (java.security.KeyPair)8 ECPublicKey (java.security.interfaces.ECPublicKey)8 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)7