Search in sources :

Example 61 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class CipherTest method testRSA_ECB_NoPadding_Private_UpdateInAndOutPlusDoFinal_TooBig_Failure.

private void testRSA_ECB_NoPadding_Private_UpdateInAndOutPlusDoFinal_TooBig_Failure(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    final PrivateKey privKey = kf.generatePrivate(keySpec);
    Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
    /*
         * You're actually encrypting with public keys, but there is no
         * distinction made here. It's all keyed off of what kind of key you're
         * using. ENCRYPT_MODE and DECRYPT_MODE are the same.
         */
    c.init(Cipher.ENCRYPT_MODE, privKey);
    byte[] output = new byte[RSA_2048_Vector1.length];
    c.update(RSA_Vector1_ZeroPadded_Encrypted, 0, RSA_Vector1_ZeroPadded_Encrypted.length, output);
    try {
        c.doFinal(RSA_Vector1_ZeroPadded_Encrypted);
        fail("Should have error when block size is too big.");
    } catch (IllegalBlockSizeException success) {
        assertFalse(provider, "BC".equals(provider));
    } catch (ArrayIndexOutOfBoundsException success) {
        assertEquals("BC", provider);
    }
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 62 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class CipherTest method testRSA_ECB_NoPadding_Private_OnlyDoFinal_TooBig_Failure.

private void testRSA_ECB_NoPadding_Private_OnlyDoFinal_TooBig_Failure(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    final PrivateKey privKey = kf.generatePrivate(keySpec);
    Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
    /*
         * You're actually encrypting with public keys, but there is no
         * distinction made here. It's all keyed off of what kind of key you're
         * using. ENCRYPT_MODE and DECRYPT_MODE are the same.
         */
    c.init(Cipher.ENCRYPT_MODE, privKey);
    byte[] tooBig_Vector = new byte[RSA_Vector1_ZeroPadded_Encrypted.length * 2];
    System.arraycopy(RSA_Vector1_ZeroPadded_Encrypted, 0, tooBig_Vector, 0, RSA_Vector1_ZeroPadded_Encrypted.length);
    System.arraycopy(RSA_Vector1_ZeroPadded_Encrypted, 0, tooBig_Vector, RSA_Vector1_ZeroPadded_Encrypted.length, RSA_Vector1_ZeroPadded_Encrypted.length);
    try {
        c.doFinal(tooBig_Vector);
        fail("Should have error when block size is too big.");
    } catch (IllegalBlockSizeException success) {
        assertFalse(provider, "BC".equals(provider));
    } catch (ArrayIndexOutOfBoundsException success) {
        assertEquals("BC", provider);
    }
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 63 with PrivateKey

use of java.security.PrivateKey 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 64 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class KeyPairTest method testGetPrivate02.

/**
     * Test #2 for <code>getPrivate()</code> method<br>
     * Assertion: returns private key (valid private key in this case)
     * @throws InvalidKeySpecException
     */
public final void testGetPrivate02() throws InvalidKeySpecException {
    PrivateKey pk = TestKeyPair.getPrivate();
    KeyPair kp = new KeyPair(null, pk);
    assertSame(pk, kp.getPrivate());
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey)

Example 65 with PrivateKey

use of java.security.PrivateKey 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)

Aggregations

PrivateKey (java.security.PrivateKey)517 X509Certificate (java.security.cert.X509Certificate)217 KeyFactory (java.security.KeyFactory)169 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)144 Certificate (java.security.cert.Certificate)127 PublicKey (java.security.PublicKey)120 ByteArrayInputStream (java.io.ByteArrayInputStream)118 KeyStore (java.security.KeyStore)93 CertificateFactory (java.security.cert.CertificateFactory)92 IOException (java.io.IOException)81 Key (java.security.Key)74 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)73 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)70 Entry (java.security.KeyStore.Entry)60 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)60 KeyPair (java.security.KeyPair)59 SecretKey (javax.crypto.SecretKey)48 InvalidKeyException (java.security.InvalidKeyException)47 KeyStoreException (java.security.KeyStoreException)46 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)46