Search in sources :

Example 81 with Cipher

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

the class CipherTest method testRSA_ECB_NoPadding_Private_OnlyDoFinalWithOffset_Success.

private void testRSA_ECB_NoPadding_Private_OnlyDoFinalWithOffset_Success(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 decrypting with private 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 = new byte[RSA_Vector1_Encrypt_Private.length];
    final int encryptLen = c.doFinal(RSA_2048_Vector1, 0, RSA_2048_Vector1.length, encrypted, 0);
    assertEquals("Encrypted size should match expected", RSA_Vector1_Encrypt_Private.length, encryptLen);
    assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, encrypted));
    c.init(Cipher.DECRYPT_MODE, privKey);
    final int decryptLen = c.doFinal(RSA_2048_Vector1, 0, RSA_2048_Vector1.length, encrypted, 0);
    assertEquals("Encrypted size should match expected", RSA_Vector1_Encrypt_Private.length, decryptLen);
    assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, encrypted));
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 82 with Cipher

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

the class CipherTest method test_Cipher_Algorithm.

private void test_Cipher_Algorithm(Provider provider, String algorithm) throws Exception {
    if (algorithm.equals("RSA") && provider.getName().equals("BC")) {
        // http://b/9097343 BC's Cipher.RSA defaults to NoPadding
        // which makes it fail the key wrapping test if the
        // generated AES key to wrap starts with a leading
        // zero. For the purposes of the test, use the same
        // default behavior as the RI. Real code really should
        // specify the exact mode and padding they need and not
        // rely on defaults. http://b/9097343
        algorithm = "RSA/ECB/PKCS1Padding";
    }
    // Cipher.getInstance(String)
    Cipher c1 = Cipher.getInstance(algorithm);
    if (provider.equals(c1.getProvider())) {
        assertEquals(algorithm, c1.getAlgorithm());
        test_Cipher(c1);
    }
    // Cipher.getInstance(String, Provider)
    Cipher c2 = Cipher.getInstance(algorithm, provider);
    assertEquals(algorithm, c2.getAlgorithm());
    assertEquals(provider, c2.getProvider());
    test_Cipher(c2);
    // Cipher.getInstance(String, String)
    Cipher c3 = Cipher.getInstance(algorithm, provider.getName());
    assertEquals(algorithm, c3.getAlgorithm());
    assertEquals(provider, c3.getProvider());
    test_Cipher(c3);
}
Also used : Cipher(javax.crypto.Cipher)

Example 83 with Cipher

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

the class CipherTest method testRSA_ECB_NoPadding_Public_UpdateThenEmptyDoFinal_Success.

private void testRSA_ECB_NoPadding_Public_UpdateThenEmptyDoFinal_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);
    c.update(RSA_Vector1_Encrypt_Private);
    byte[] encrypted = c.doFinal();
    assertEncryptedEqualsNoPadding(provider, Cipher.ENCRYPT_MODE, RSA_2048_Vector1, encrypted);
    c.init(Cipher.DECRYPT_MODE, privKey);
    c.update(RSA_Vector1_Encrypt_Private);
    encrypted = c.doFinal();
    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 84 with Cipher

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

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

the class CipherTest method testAES_ECB_NoPadding_IncrementalUpdate_Success.

private void testAES_ECB_NoPadding_IncrementalUpdate_Success(String provider) throws Exception {
    SecretKey key = new SecretKeySpec(AES_128_KEY, "AES");
    Cipher c = Cipher.getInstance("AES/ECB/NoPadding", provider);
    c.init(Cipher.ENCRYPT_MODE, key);
    for (int i = 0; i < AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext_Padded.length - 1; i++) {
        final byte[] outputFragment = c.update(AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext_Padded, i, 1);
        if (outputFragment != null) {
            assertEquals(0, outputFragment.length);
        }
    }
    final byte[] output = c.doFinal(AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext_Padded, AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext_Padded.length - 1, 1);
    assertNotNull(output);
    assertEquals(AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext_Padded.length, output.length);
    assertTrue(Arrays.equals(AES_128_ECB_PKCS5Padding_TestVector_1_Encrypted, output));
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

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