Search in sources :

Example 86 with Cipher

use of javax.crypto.Cipher 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 87 with Cipher

use of javax.crypto.Cipher in project robovm by robovm.

the class CipherTest method testRSA_ECB_NoPadding_GetOutputSize_Success.

private void testRSA_ECB_NoPadding_GetOutputSize_Success(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    final PublicKey pubKey = kf.generatePublic(pubKeySpec);
    Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
    c.init(Cipher.ENCRYPT_MODE, pubKey);
    final int modulusInBytes = RSA_2048_modulus.bitLength() / 8;
    assertEquals(modulusInBytes, c.getOutputSize(RSA_2048_Vector1.length));
    assertEquals(modulusInBytes, c.getOutputSize(RSA_2048_Vector1.length * 2));
    assertEquals(modulusInBytes, c.getOutputSize(0));
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 88 with Cipher

use of javax.crypto.Cipher in project robovm by robovm.

the class CipherTest method testRSA_ECB_NoPadding_Public_TooSmall_Success.

private void testRSA_ECB_NoPadding_Public_TooSmall_Success(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    final PublicKey privKey = kf.generatePublic(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[] encrypted = c.doFinal(TooShort_Vector);
    assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_ZeroPadded_Encrypted, encrypted));
    c.init(Cipher.DECRYPT_MODE, privKey);
    encrypted = c.doFinal(TooShort_Vector);
    assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_ZeroPadded_Encrypted, encrypted));
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 89 with Cipher

use of javax.crypto.Cipher in project robovm by robovm.

the class CipherTest method testRSA_ECB_NoPadding_Public_SingleByteUpdateThenEmptyDoFinal_Success.

private void testRSA_ECB_NoPadding_Public_SingleByteUpdateThenEmptyDoFinal_Success(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    final PublicKey privKey = kf.generatePublic(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);
    int i;
    for (i = 0; i < RSA_Vector1_Encrypt_Private.length / 2; i++) {
        c.update(RSA_Vector1_Encrypt_Private, i, 1);
    }
    byte[] encrypted = c.doFinal(RSA_Vector1_Encrypt_Private, i, RSA_2048_Vector1.length - i);
    assertEncryptedEqualsNoPadding(provider, Cipher.ENCRYPT_MODE, RSA_2048_Vector1, encrypted);
    c.init(Cipher.DECRYPT_MODE, privKey);
    for (i = 0; i < RSA_Vector1_Encrypt_Private.length / 2; i++) {
        c.update(RSA_Vector1_Encrypt_Private, i, 1);
    }
    encrypted = c.doFinal(RSA_Vector1_Encrypt_Private, i, RSA_2048_Vector1.length - i);
    assertEncryptedEqualsNoPadding(provider, Cipher.DECRYPT_MODE, RSA_2048_Vector1, encrypted);
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 90 with Cipher

use of javax.crypto.Cipher in project robovm by robovm.

the class CipherTest method testRC4_MultipleKeySizes.

public void testRC4_MultipleKeySizes() throws Exception {
    final int SMALLEST_KEY_SIZE = 40;
    final int LARGEST_KEY_SIZE = 1024;
    /* Make an array of keys for our tests */
    SecretKey[] keys = new SecretKey[LARGEST_KEY_SIZE - SMALLEST_KEY_SIZE];
    {
        KeyGenerator kg = KeyGenerator.getInstance("ARC4");
        for (int keysize = SMALLEST_KEY_SIZE; keysize < LARGEST_KEY_SIZE; keysize++) {
            final int index = keysize - SMALLEST_KEY_SIZE;
            kg.init(keysize);
            keys[index] = kg.generateKey();
        }
    }
    /*
         * Use this to compare the output of the first provider against
         * subsequent providers.
         */
    String[] expected = new String[LARGEST_KEY_SIZE - SMALLEST_KEY_SIZE];
    /* Find all providers that provide ARC4. We must have at least one! */
    Map<String, String> filter = new HashMap<String, String>();
    filter.put("Cipher.ARC4", "");
    Provider[] providers = Security.getProviders(filter);
    assertTrue("There must be security providers of Cipher.ARC4", providers.length > 0);
    /* Keep track of this for later error messages */
    String firstProvider = providers[0].getName();
    for (Provider p : providers) {
        Cipher c = Cipher.getInstance("ARC4", p);
        for (int keysize = SMALLEST_KEY_SIZE; keysize < LARGEST_KEY_SIZE; keysize++) {
            final int index = keysize - SMALLEST_KEY_SIZE;
            final SecretKey sk = keys[index];
            /*
                 * Test that encryption works. Donig this in a loop also has the
                 * benefit of testing that re-initialization works for this
                 * cipher.
                 */
            c.init(Cipher.ENCRYPT_MODE, sk);
            byte[] cipherText = c.doFinal(ORIGINAL_PLAIN_TEXT);
            assertNotNull(cipherText);
            /*
                 * Compare providers against eachother to make sure they're all
                 * in agreement. This helps when you add a brand new provider.
                 */
            if (expected[index] == null) {
                expected[index] = Arrays.toString(cipherText);
            } else {
                assertEquals(firstProvider + " should output the same as " + p.getName() + " for key size " + keysize, expected[index], Arrays.toString(cipherText));
            }
            c.init(Cipher.DECRYPT_MODE, sk);
            byte[] actualPlaintext = c.doFinal(cipherText);
            assertEquals("Key size: " + keysize, Arrays.toString(ORIGINAL_PLAIN_TEXT), Arrays.toString(actualPlaintext));
        }
    }
}
Also used : SecretKey(javax.crypto.SecretKey) HashMap(java.util.HashMap) Cipher(javax.crypto.Cipher) KeyGenerator(javax.crypto.KeyGenerator) Provider(java.security.Provider)

Aggregations

Cipher (javax.crypto.Cipher)1531 SecretKeySpec (javax.crypto.spec.SecretKeySpec)516 SecretKey (javax.crypto.SecretKey)377 IvParameterSpec (javax.crypto.spec.IvParameterSpec)368 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)349 InvalidKeyException (java.security.InvalidKeyException)257 IOException (java.io.IOException)202 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)202 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)196 BadPaddingException (javax.crypto.BadPaddingException)183 GeneralSecurityException (java.security.GeneralSecurityException)161 SecretKeyFactory (javax.crypto.SecretKeyFactory)149 Key (java.security.Key)148 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)134 SecureRandom (java.security.SecureRandom)105 PrivateKey (java.security.PrivateKey)93 PublicKey (java.security.PublicKey)86 UnsupportedEncodingException (java.io.UnsupportedEncodingException)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)82 KeyGenerator (javax.crypto.KeyGenerator)78