Search in sources :

Example 11 with KeySpec

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

the class KeyFactory2Test method test_getKeySpecLjava_security_KeyLjava_lang_Class.

public void test_getKeySpecLjava_security_KeyLjava_lang_Class() throws Exception {
    // java.lang.Class)
    for (int i = 0; i < keyfactAlgs.length; i++) {
        KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i], providerName);
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyfactAlgs[i]);
        // We don't use getInstance
        SecureRandom random = new SecureRandom();
        keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random);
        KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
        KeyPair keys = keyGen.generateKeyPair();
        if (keepalive != null) {
            keepalive.interrupt();
        }
        KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(), StandardNames.getPrivateKeySpecClass(keyfactAlgs[i]));
        KeySpec publicKeySpec = fact.getKeySpec(keys.getPublic(), StandardNames.getPublicKeySpecClass(keyfactAlgs[i]));
        PrivateKey privateKey = fact.generatePrivate(privateKeySpec);
        PublicKey publicKey = fact.generatePublic(publicKeySpec);
        assertEquals("generatePrivate generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPrivate().getEncoded()), Arrays.toString(privateKey.getEncoded()));
        assertEquals("generatePublic generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPublic().getEncoded()), Arrays.toString(publicKey.getEncoded()));
        KeySpec encodedSpec = fact.getKeySpec(keys.getPublic(), X509EncodedKeySpec.class);
        assertTrue("improper key spec for encoded public key", encodedSpec.getClass().equals(X509EncodedKeySpec.class));
        encodedSpec = fact.getKeySpec(keys.getPrivate(), PKCS8EncodedKeySpec.class);
        assertTrue("improper key spec for encoded private key", encodedSpec.getClass().equals(PKCS8EncodedKeySpec.class));
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) SecureRandom(java.security.SecureRandom) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyPairGenerator(java.security.KeyPairGenerator) KeyFactory(java.security.KeyFactory)

Example 12 with KeySpec

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

the class KeyFactorySpiTest method testKeyFactorySpi.

/**
     * Test for <code>KeyFactorySpi</code> constructor
     * Assertion: constructs KeyFactorySpi
     */
public void testKeyFactorySpi() {
    MyKeyFactorySpi keyFSpi = new MyKeyFactorySpi();
    assertTrue(keyFSpi instanceof KeyFactorySpi);
    KeySpec ks = new MyKeySpec();
    KeySpec kss = new MyKeySpec();
    try {
        keyFSpi.engineGeneratePrivate(ks);
        keyFSpi.engineGeneratePublic(ks);
        keyFSpi.engineGetKeySpec(null, java.lang.Class.class);
        keyFSpi.engineTranslateKey(null);
    } catch (Exception e) {
        fail("Unexpected exception");
    }
}
Also used : KeyFactorySpi(java.security.KeyFactorySpi) KeySpec(java.security.spec.KeySpec)

Example 13 with KeySpec

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

the class KeyFactoryTest method testGeneratePublic.

@SuppressWarnings("unchecked")
public void testGeneratePublic() {
    KeyFactory factory = null;
    try {
        factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    }
    assertNotNull(factory);
    try {
        TestPublicKey key = new TestPublicKey();
        TestPublicKeySpec keySpec = new TestPublicKeySpec(key);
        PublicKey publicKey = factory.generatePublic(keySpec);
        assertNotNull(publicKey);
        assertTrue(Arrays.equals(key.encoded, publicKey.getEncoded()));
    } catch (InvalidKeySpecException e) {
        fail("unexpected exception: " + e);
    }
    KeySpec[] keySpecs = { new TestPrivateKeySpec(new TestPrivateKey()), null, new DSAPublicKeySpec(null, null, null, null) };
    Class[] exceptions = { InvalidKeySpecException.class, NullPointerException.class, InvalidKeySpecException.class };
    for (int i = 0; i < keySpecs.length; i++) {
        KeySpec keySpec = keySpecs[i];
        String message = "generatePublic(" + (keySpec == null ? "null" : keySpec.toString()) + ")";
        try {
            PublicKey generatePublic = factory.generatePublic(keySpec);
            assertNotNull(generatePublic);
        } catch (Exception e) {
            checkException(message, e, exceptions[i]);
        } finally {
            checkException(message, null, exceptions[i]);
        }
    }
}
Also used : PublicKey(java.security.PublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) KeySpec(java.security.spec.KeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 14 with KeySpec

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

the class KeyFactory2Test method test_generatePrivateLjava_security_spec_KeySpec.

public void test_generatePrivateLjava_security_spec_KeySpec() throws Exception {
    // java.security.KeyFactory.generatePrivate(java.security.spec.KeySpec)
    for (int i = 0; i < keyfactAlgs.length; i++) {
        KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i], providerName);
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyfactAlgs[i]);
        // We don't use
        SecureRandom random = new SecureRandom();
        // getInstance
        keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random);
        KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
        KeyPair keys = keyGen.generateKeyPair();
        if (keepalive != null) {
            keepalive.interrupt();
        }
        KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(), StandardNames.getPrivateKeySpecClass(keyfactAlgs[i]));
        PrivateKey privateKey = fact.generatePrivate(privateKeySpec);
        assertEquals("generatePrivate generated different key for algorithm " + keyfactAlgs[i], Arrays.toString(keys.getPrivate().getEncoded()), Arrays.toString(privateKey.getEncoded()));
        privateKey = fact.generatePrivate(new PKCS8EncodedKeySpec(keys.getPrivate().getEncoded()));
        assertEquals("generatePrivate generated different key for algorithm " + keyfactAlgs[i], Arrays.toString(keys.getPrivate().getEncoded()), Arrays.toString(privateKey.getEncoded()));
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) KeyFactory(java.security.KeyFactory)

Example 15 with KeySpec

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

the class RSAPublicKeySpecTest method testRSAPublicKeySpec02.

/**
     * Test #2 for <code>RSAPublicKeySpec</code> constructor
     * Assertion: Constructs <code>RSAPublicKeySpec</code>
     * object using valid parameters
     */
public final void testRSAPublicKeySpec02() {
    KeySpec ks = new RSAPublicKeySpec(null, null);
    assertTrue(ks instanceof RSAPublicKeySpec);
}
Also used : KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Aggregations

KeySpec (java.security.spec.KeySpec)149 PBEKeySpec (javax.crypto.spec.PBEKeySpec)66 SecretKeyFactory (javax.crypto.SecretKeyFactory)62 KeyFactory (java.security.KeyFactory)46 SecretKeySpec (javax.crypto.spec.SecretKeySpec)46 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)39 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 SecretKey (javax.crypto.SecretKey)35 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)34 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)28 BigInteger (java.math.BigInteger)25 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)25 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)23 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)21 PrivateKey (java.security.PrivateKey)19 Cipher (javax.crypto.Cipher)16 IOException (java.io.IOException)15 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)14 PublicKey (java.security.PublicKey)13 InvalidKeyException (java.security.InvalidKeyException)12