Search in sources :

Example 61 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.

the class BcKeyStoreSpi method makePBECipher.

protected Cipher makePBECipher(String algorithm, int mode, char[] password, byte[] salt, int iterationCount) throws IOException {
    try {
        PBEKeySpec pbeSpec = new PBEKeySpec(password);
        SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
        PBEParameterSpec defParams = new PBEParameterSpec(salt, iterationCount);
        Cipher cipher = Cipher.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
        cipher.init(mode, keyFact.generateSecret(pbeSpec), defParams);
        return cipher;
    } catch (Exception e) {
        throw new IOException("Error initialising store of key store: " + e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 62 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.

the class PKCS12KeyStoreSpi method wrapKey.

protected byte[] wrapKey(String algorithm, Key key, PKCS12PBEParams pbeParams, char[] password) throws IOException {
    PBEKeySpec pbeSpec = new PBEKeySpec(password);
    byte[] out;
    try {
        SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
        PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
        Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
        cipher.init(Cipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), defParams);
        out = cipher.wrap(key);
    } catch (Exception e) {
        throw new IOException("exception encrypting data - " + e.toString());
    }
    return out;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException)

Example 63 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec 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 64 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.

the class PBEKeySpecTest method testGetKeyLength.

/**
     * getKeyLength() method testing.
     */
public void testGetKeyLength() {
    char[] password = new char[] { '1', '2', '3', '4', '5' };
    byte[] salt = new byte[] { 1, 2, 3, 4, 5 };
    int iterationCount = 10;
    int keyLength = 10;
    PBEKeySpec pbeks = new PBEKeySpec(password, salt, iterationCount, keyLength);
    assertTrue("The returned keyLength is not equal to the value specified " + "in the constructor.", pbeks.getKeyLength() == keyLength);
    pbeks = new PBEKeySpec(password);
    assertTrue("The getKeyLength() method should return 0 " + "if the keyLength is not specified.", pbeks.getKeyLength() == 0);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec)

Example 65 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.

the class PBEKeySpecTest method testPBEKeySpec3.

/**
     * PBEKeySpec(char[] password, byte[] salt, int iterationCount) method
     * testing. Tests the behavior of the method in the case
     * of inappropriate parameters and checks that array objects specified as
     * a parameters are copied during the object initialization.
     */
public void testPBEKeySpec3() {
    char[] password = new char[] { '1', '2', '3', '4', '5' };
    byte[] salt = new byte[] { 1, 2, 3, 4, 5 };
    int iterationCount = 10;
    try {
        PBEKeySpec pbeks = new PBEKeySpec(null, salt, iterationCount);
        assertTrue("An empty char[] should be used in case of null input " + "char array.", pbeks.getPassword().length == 0);
    } catch (IllegalArgumentException e) {
        fail("Unexpected IllegalArgumentException was thrown.");
    } catch (NullPointerException e) {
        fail("Unexpected NullPointerException was thrown.");
    }
    try {
        new PBEKeySpec(password, null, iterationCount);
        fail("A NullPointerException should be was thrown " + "in the case of null salt.");
    } catch (IllegalArgumentException e) {
        fail("Unexpected IllegalArgumentException was thrown.");
    } catch (NullPointerException e) {
    }
    try {
        new PBEKeySpec(password, new byte[0], iterationCount);
        fail("An IllegalArgumentException should be thrown " + "in the case of empty salt.");
    } catch (IllegalArgumentException e) {
    }
    try {
        new PBEKeySpec(password, salt, -1);
        fail("An IllegalArgumentException should be thrown " + "in the case of negative iterationCount.");
    } catch (IllegalArgumentException e) {
    }
    try {
        new PBEKeySpec(password, salt, 0);
        fail("An IllegalArgumentException should be thrown " + "in the case of zero iterationCount.");
    } catch (IllegalArgumentException e) {
    }
    PBEKeySpec pbeks = new PBEKeySpec(password, salt, iterationCount);
    password[0]++;
    assertFalse("The change of password specified in the constructor " + "should not cause the change of internal array.", password[0] == pbeks.getPassword()[0]);
    salt[0]++;
    assertFalse("The change of salt specified in the constructor " + " should not cause the change of internal array.", salt[0] == pbeks.getSalt()[0]);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec)

Aggregations

PBEKeySpec (javax.crypto.spec.PBEKeySpec)99 SecretKeyFactory (javax.crypto.SecretKeyFactory)75 SecretKey (javax.crypto.SecretKey)55 Cipher (javax.crypto.Cipher)36 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)31 KeySpec (java.security.spec.KeySpec)24 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)23 KeyStoreException (java.security.KeyStoreException)16 IOException (java.io.IOException)15 SecretKeySpec (javax.crypto.spec.SecretKeySpec)14 CertificateException (java.security.cert.CertificateException)12 UnrecoverableKeyException (java.security.UnrecoverableKeyException)11 KeyStore (java.security.KeyStore)10 CertificateEncodingException (java.security.cert.CertificateEncodingException)8 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 InvalidKeyException (java.security.InvalidKeyException)6 Key (java.security.Key)6