Search in sources :

Example 26 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 27 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)

Example 28 with X509EncodedKeySpec

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

the class SignatureTest method test18566.

// http://code.google.com/p/android/issues/detail?id=18566
// http://b/5038554
public void test18566() throws Exception {
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(PK_BYTES);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey pk = keyFactory.generatePublic(keySpec);
    Signature sig = Signature.getInstance("SHA256withRSA");
    sig.initVerify(pk);
    sig.update(CONTENT);
    assertTrue(sig.verify(SIGNATURE));
}
Also used : PublicKey(java.security.PublicKey) Signature(java.security.Signature) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 29 with X509EncodedKeySpec

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

the class X509EncodedKeySpecTest method testX509EncodedKeySpec.

//
// Test cases
//
/**
     * Test for <code>X509EncodedKeySpec</code> constructor<br>
     * Assertion: constructs new <code>X509EncodedKeySpec</code>
     * object using valid parameter
     */
public final void testX509EncodedKeySpec() {
    byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    EncodedKeySpec eks = new X509EncodedKeySpec(encodedKey);
    assertTrue(eks instanceof X509EncodedKeySpec);
    try {
        eks = new X509EncodedKeySpec(null);
        fail("expected NullPointerException");
    } catch (NullPointerException e) {
    // ok
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 30 with X509EncodedKeySpec

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

the class X509EncodedKeySpecTest method testGetFormat.

/**
     * Test for <code>getFormat()</code> method
     * Assertion: returns format name (always "X.509")
     */
public final void testGetFormat() {
    byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    X509EncodedKeySpec meks = new X509EncodedKeySpec(encodedKey);
    assertEquals("X.509", meks.getFormat());
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Aggregations

X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)343 KeyFactory (java.security.KeyFactory)228 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)154 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)142 PublicKey (java.security.PublicKey)129 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)82 PrivateKey (java.security.PrivateKey)50 RSAPublicKey (java.security.interfaces.RSAPublicKey)48 IOException (java.io.IOException)39 InvalidKeyException (java.security.InvalidKeyException)37 KeyPair (java.security.KeyPair)30 Cipher (javax.crypto.Cipher)26 Signature (java.security.Signature)25 EncodedKeySpec (java.security.spec.EncodedKeySpec)21 NoSuchProviderException (java.security.NoSuchProviderException)14 ECPublicKey (java.security.interfaces.ECPublicKey)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 SecretKey (javax.crypto.SecretKey)13 BigInteger (java.math.BigInteger)12 Key (java.security.Key)12