Search in sources :

Example 16 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project jdk8u_jdk by JetBrains.

the class PBKDF2TranslateTest method getSecretKeyForPBKDF2.

/**
     * Generate a PBKDF2 secret key using given algorithm.
     */
private SecretKey getSecretKeyForPBKDF2(String algoDeriveKey, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algoDeriveKey);
    PBEKeySpec spec = new PBEKeySpec(PASS_PHRASE.toCharArray(), salt, ITERATION_COUNT, KEY_SIZE);
    return skf.generateSecret(spec);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 17 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project android_frameworks_base by AOSPA.

the class BackupManagerService method buildCharArrayKey.

private SecretKey buildCharArrayKey(String algorithm, char[] pwArray, byte[] salt, int rounds) {
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
        KeySpec ks = new PBEKeySpec(pwArray, salt, rounds, PBKDF2_KEY_SIZE);
        return keyFactory.generateSecret(ks);
    } catch (InvalidKeySpecException e) {
        Slog.e(TAG, "Invalid key spec for PBKDF2!");
    } catch (NoSuchAlgorithmException e) {
        Slog.e(TAG, "PBKDF2 unavailable!");
    }
    return null;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 18 with PBEKeySpec

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

the class VaultSession method computeMaskedPassword.

/**
     * Method to compute masked password based on class attributes.
     *
     * @return masked password prefixed with {link @PicketBoxSecurityVault.PASS_MASK_PREFIX}.
     * @throws Exception
     */
private String computeMaskedPassword() throws Exception {
    // Create the PBE secret key
    SecretKeyFactory factory = SecretKeyFactory.getInstance(VAULT_ENC_ALGORITHM);
    char[] password = "somearbitrarycrazystringthatdoesnotmatter".toCharArray();
    PBEParameterSpec cipherSpec = new PBEParameterSpec(salt.getBytes(), iterationCount);
    PBEKeySpec keySpec = new PBEKeySpec(password);
    SecretKey cipherKey = factory.generateSecret(keySpec);
    String maskedPass = PBEUtils.encode64(keystorePassword.getBytes(), VAULT_ENC_ALGORITHM, cipherKey, cipherSpec);
    return PicketBoxSecurityVault.PASS_MASK_PREFIX + maskedPass;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 19 with PBEKeySpec

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

the class SecretKeyFactoryTest method test_PBKDF2_UTF8.

private void test_PBKDF2_UTF8(char[] password, byte[] salt, int iterations, int keyLength, byte[] expected) throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    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 20 with PBEKeySpec

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

the class SecretKeyFactoryTest method test_PBKDF2_required_parameters.

public void test_PBKDF2_required_parameters() throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    // PBEKeySpecs password only constructor
    try {
        KeySpec ks = new PBEKeySpec(null);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0]);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(PASSWORD);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    // PBEKeySpecs constructor without key length
    try {
        KeySpec ks = new PBEKeySpec(null, SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0], SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(PASSWORD, SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(null, SALT, ITERATIONS, KEY_LENGTH);
        factory.generateSecret(ks);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0], SALT, ITERATIONS, KEY_LENGTH);
        factory.generateSecret(ks);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    KeySpec ks = new PBEKeySpec(PASSWORD, SALT, ITERATIONS, KEY_LENGTH);
    factory.generateSecret(ks);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

PBEKeySpec (javax.crypto.spec.PBEKeySpec)249 SecretKeyFactory (javax.crypto.SecretKeyFactory)190 SecretKey (javax.crypto.SecretKey)118 Cipher (javax.crypto.Cipher)82 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)73 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)63 KeySpec (java.security.spec.KeySpec)59 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)59 SecretKeySpec (javax.crypto.spec.SecretKeySpec)49 IOException (java.io.IOException)25 KeyStoreException (java.security.KeyStoreException)23 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)22 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)17 CertificateException (java.security.cert.CertificateException)15 GeneralSecurityException (java.security.GeneralSecurityException)14 UnrecoverableKeyException (java.security.UnrecoverableKeyException)14 AlgorithmParameters (java.security.AlgorithmParameters)13 Key (java.security.Key)13 KeyStore (java.security.KeyStore)13 InvalidKeyException (java.security.InvalidKeyException)12