Search in sources :

Example 81 with SecretKeyFactory

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

the class SecretKeyFactoryTest method test_PBKDF2_8BIT.

private void test_PBKDF2_8BIT(char[] password, byte[] salt, int iterations, int keyLength, byte[] expected) throws Exception {
    if (!StandardNames.IS_RI) {
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1And8bit");
        KeySpec ks = new PBEKeySpec(password, salt, iterations, keyLength);
        SecretKey key = factory.generateSecret(ks);
        assertTrue(Arrays.equals(expected, key.getEncoded()));
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 82 with SecretKeyFactory

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

the class CipherTest method test_update$BII.

/**
     * javax.crypto.Cipher#update(byte[], int, int)
     */
public void test_update$BII() throws Exception {
    for (int index = 1; index < 4; index++) {
        Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
        byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".key");
        DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE");
        Key k = skf.generateSecret(keySpec);
        byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".iv");
        IvParameterSpec iv = new IvParameterSpec(ivMaterial);
        c.init(Cipher.DECRYPT_MODE, k, iv);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] input = new byte[256];
        String resPath = "hyts_" + "des-ede3-cbc.test" + index + ".ciphertext";
        File resources = Support_Resources.createTempFolder();
        Support_Resources.copyFile(resources, null, resPath);
        InputStream is = Support_Resources.getStream(resPath);
        int bytesRead = is.read(input, 0, 256);
        while (bytesRead > 0) {
            byte[] output = c.update(input, 0, bytesRead);
            if (output != null) {
                baos.write(output);
            }
            bytesRead = is.read(input, 0, 256);
        }
        byte[] output = c.doFinal();
        if (output != null) {
            baos.write(output);
        }
        byte[] decipheredCipherText = baos.toByteArray();
        is.close();
        byte[] plaintextBytes = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".plaintext");
        assertEquals("Operation produced incorrect results for index " + index, Arrays.toString(plaintextBytes), Arrays.toString(decipheredCipherText));
    }
    Cipher cipher = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
    try {
        cipher.update(new byte[64], 0, 32);
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : InputStream(java.io.InputStream) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SecretKeyFactory(javax.crypto.SecretKeyFactory) File(java.io.File) Key(java.security.Key)

Example 83 with SecretKeyFactory

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

the class CipherTest method test_doFinal.

/**
     * javax.crypto.Cipher#doFinal()
     */
public void test_doFinal() throws Exception {
    for (int index = 1; index < 4; index++) {
        Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
        byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".key");
        DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE");
        Key k = skf.generateSecret(keySpec);
        byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".iv");
        IvParameterSpec iv = new IvParameterSpec(ivMaterial);
        c.init(Cipher.ENCRYPT_MODE, k, iv);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] input = new byte[256];
        String resPath = "hyts_" + "des-ede3-cbc.test" + index + ".plaintext";
        File resources = Support_Resources.createTempFolder();
        Support_Resources.copyFile(resources, null, resPath);
        InputStream is = Support_Resources.getStream(resPath);
        int bytesRead = is.read(input, 0, 256);
        while (bytesRead > 0) {
            byte[] output = c.update(input, 0, bytesRead);
            if (output != null) {
                baos.write(output);
            }
            bytesRead = is.read(input, 0, 256);
        }
        byte[] output = c.doFinal();
        if (output != null) {
            baos.write(output);
        }
        byte[] encryptedPlaintext = baos.toByteArray();
        is.close();
        byte[] cipherText = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".ciphertext");
        assertEquals("Operation produced incorrect results for index " + index, Arrays.toString(cipherText), Arrays.toString(encryptedPlaintext));
    }
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    byte[] b1 = new byte[30];
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    c.update(b, 0, 10, b1, 5);
    try {
        c.doFinal();
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal();
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    int len = c.doFinal(b, 0, 16, b1, 0);
    assertEquals(16, len);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    assertTrue(Arrays.equals(c.getIV(), IV));
    c.update(b1, 0, 24, b, 0);
    try {
        c.doFinal();
        fail();
    } catch (BadPaddingException expected) {
    }
}
Also used : InputStream(java.io.InputStream) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BadPaddingException(javax.crypto.BadPaddingException) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) File(java.io.File) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key)

Example 84 with SecretKeyFactory

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

the class mySecretKeyFactory method testSecretKeyFactory01.

/**
     * Test for <code>SecretKeyFactory</code> constructor
     * Assertion: returns SecretKeyFactory object
     */
public void testSecretKeyFactory01() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    SecretKeyFactorySpi spi = new MySecretKeyFactorySpi();
    SecretKeyFactory secKF = new mySecretKeyFactory(spi, defaultProvider, defaultAlgorithm);
    assertEquals("Incorrect algorithm", secKF.getAlgorithm(), defaultAlgorithm);
    assertEquals("Incorrect provider", secKF.getProvider(), defaultProvider);
    assertNull("Incorrect result", secKF.generateSecret(null));
    assertNull("Incorrect result", secKF.getKeySpec(null, null));
    assertNull("Incorrect result", secKF.translateKey(null));
    secKF = new mySecretKeyFactory(null, null, null);
    assertNull("Algorithm must be null", secKF.getAlgorithm());
    assertNull("Provider must be null", secKF.getProvider());
    try {
        secKF.translateKey(null);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    }
}
Also used : MySecretKeyFactorySpi(org.apache.harmony.crypto.tests.support.MySecretKeyFactorySpi) SecretKeyFactorySpi(javax.crypto.SecretKeyFactorySpi) MySecretKeyFactorySpi(org.apache.harmony.crypto.tests.support.MySecretKeyFactorySpi) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 85 with SecretKeyFactory

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

the class mySecretKeyFactory method test_translateKeyLjavax_crypto_SecretKey.

public void test_translateKeyLjavax_crypto_SecretKey() throws NoSuchAlgorithmException, InvalidKeyException {
    KeyGenerator kg = null;
    Key key = null;
    SecretKeyFactory secKF = null;
    for (int i = 0; i < validValues.length; i++) {
        secKF = SecretKeyFactory.getInstance(validValues[i]);
        assertNotNull(secKF.getProvider());
        kg = KeyGenerator.getInstance(secKF.getAlgorithm());
        kg.init(new SecureRandom());
        key = kg.generateKey();
        secKF.translateKey((SecretKey) key);
    }
    try {
        secKF.translateKey(null);
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
    //expected
    }
}
Also used : SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) KeyGenerator(javax.crypto.KeyGenerator) SecretKeyFactory(javax.crypto.SecretKeyFactory) Key(java.security.Key) SecretKey(javax.crypto.SecretKey)

Aggregations

SecretKeyFactory (javax.crypto.SecretKeyFactory)129 SecretKey (javax.crypto.SecretKey)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)75 Cipher (javax.crypto.Cipher)58 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 DESKeySpec (javax.crypto.spec.DESKeySpec)28 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)26 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)26 KeySpec (java.security.spec.KeySpec)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)23 SecureRandom (java.security.SecureRandom)18 KeyStoreException (java.security.KeyStoreException)16 IOException (java.io.IOException)15 InvalidKeyException (java.security.InvalidKeyException)14 PrivateKey (java.security.PrivateKey)12 CertificateException (java.security.cert.CertificateException)12 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)12 UnrecoverableKeyException (java.security.UnrecoverableKeyException)11 Key (java.security.Key)10 KeyFactory (java.security.KeyFactory)10