Search in sources :

Example 76 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project opennms by OpenNMS.

the class JCEKSSecureCredentialsVault method getCredentials.

@Override
public Credentials getCredentials(String alias) {
    try {
        KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(m_password);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
        KeyStore.SecretKeyEntry ske = (KeyStore.SecretKeyEntry) m_keystore.getEntry(alias, keyStorePP);
        if (ske == null) {
            return null;
        }
        PBEKeySpec keySpec = (PBEKeySpec) factory.getKeySpec(ske.getSecretKey(), PBEKeySpec.class);
        return fromBase64EncodedByteArray(new String(keySpec.getPassword()).getBytes());
    } catch (KeyStoreException | InvalidKeySpecException | NoSuchAlgorithmException | IOException | ClassNotFoundException | UnrecoverableEntryException e) {
        throw Throwables.propagate(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) UnrecoverableEntryException(java.security.UnrecoverableEntryException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 77 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project opennms by OpenNMS.

the class JCEKSSecureCredentialsVault method getCredentials.

@Override
public Credentials getCredentials(String alias) {
    try {
        KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(m_password);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
        KeyStore.SecretKeyEntry ske = (KeyStore.SecretKeyEntry) m_keystore.getEntry(alias, keyStorePP);
        if (ske == null) {
            return null;
        }
        PBEKeySpec keySpec = (PBEKeySpec) factory.getKeySpec(ske.getSecretKey(), PBEKeySpec.class);
        return fromBase64EncodedByteArray(new String(keySpec.getPassword()).getBytes());
    } catch (KeyStoreException | InvalidKeySpecException | NoSuchAlgorithmException | IOException | ClassNotFoundException | UnrecoverableEntryException e) {
        throw Throwables.propagate(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) UnrecoverableEntryException(java.security.UnrecoverableEntryException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 78 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 79 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 80 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)110 SecretKeyFactory (javax.crypto.SecretKeyFactory)85 SecretKey (javax.crypto.SecretKey)60 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 Cipher (javax.crypto.Cipher)39 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)33 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)27 KeySpec (java.security.spec.KeySpec)26 SecretKeySpec (javax.crypto.spec.SecretKeySpec)17 KeyStoreException (java.security.KeyStoreException)16 IOException (java.io.IOException)15 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)8 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)8 Key (java.security.Key)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 InvalidKeyException (java.security.InvalidKeyException)6