Search in sources :

Example 51 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 52 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 53 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)

Example 54 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project android_frameworks_base by crdroidandroid.

the class PackageParser method parsePublicKey.

public static final PublicKey parsePublicKey(final String encodedPublicKey) {
    if (encodedPublicKey == null) {
        Slog.w(TAG, "Could not parse null public key");
        return null;
    }
    EncodedKeySpec keySpec;
    try {
        final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
        keySpec = new X509EncodedKeySpec(encoded);
    } catch (IllegalArgumentException e) {
        Slog.w(TAG, "Could not parse verifier public key; invalid Base64");
        return null;
    }
    /* First try the key as an RSA key. */
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException e) {
        Slog.wtf(TAG, "Could not parse public key: RSA KeyFactory not included in build");
    } catch (InvalidKeySpecException e) {
    // Not a RSA public key.
    }
    /* Now try it as a ECDSA key. */
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance("EC");
        return keyFactory.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException e) {
        Slog.wtf(TAG, "Could not parse public key: EC KeyFactory not included in build");
    } catch (InvalidKeySpecException e) {
    // Not a ECDSA public key.
    }
    /* Now try it as a DSA key. */
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        return keyFactory.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException e) {
        Slog.wtf(TAG, "Could not parse public key: DSA KeyFactory not included in build");
    } catch (InvalidKeySpecException e) {
    // Not a DSA public key.
    }
    /* Not a supported key type */
    return null;
}
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 55 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project chuidiang-ejemplos by chuidiang.

the class RSAAsymetricCrypto method loadPublicKey.

private static PublicKey loadPublicKey(String fileName) throws Exception {
    FileInputStream fis = new FileInputStream(fileName);
    int numBtyes = fis.available();
    byte[] bytes = new byte[numBtyes];
    fis.read(bytes);
    fis.close();
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    KeySpec keySpec = new X509EncodedKeySpec(bytes);
    PublicKey keyFromBytes = keyFactory.generatePublic(keySpec);
    return keyFromBytes;
}
Also used : PublicKey(java.security.PublicKey) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) FileInputStream(java.io.FileInputStream) 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