Search in sources :

Example 66 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project Signal-Android by WhisperSystems.

the class MasterSecretUtil method getKeyFromPassphrase.

private static SecretKey getKeyFromPassphrase(String passphrase, byte[] salt, int iterations) throws GeneralSecurityException {
    PBEKeySpec keyspec = new PBEKeySpec(passphrase.toCharArray(), salt, iterations);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA1AND128BITAES-CBC-BC");
    return skf.generateSecret(keyspec);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 67 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project graylog2-server by Graylog2.

the class PemKeyStore method generateKeySpec.

/**
     * Generates a key specification for an (encrypted) private key.
     *
     * @param password characters, if {@code null} or empty an unencrypted key is assumed
     * @param key      bytes of the DER encoded private key
     * @return a key specification
     * @throws IOException                        if parsing {@code key} fails
     * @throws NoSuchAlgorithmException           if the algorithm used to encrypt {@code key} is unkown
     * @throws NoSuchPaddingException             if the padding scheme specified in the decryption algorithm is unkown
     * @throws InvalidKeySpecException            if the decryption key based on {@code password} cannot be generated
     * @throws InvalidKeyException                if the decryption key based on {@code password} cannot be used to decrypt
     *                                            {@code key}
     * @throws InvalidAlgorithmParameterException if decryption algorithm parameters are somehow faulty
     */
protected static PKCS8EncodedKeySpec generateKeySpec(char[] password, byte[] key) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, InvalidKeyException, InvalidAlgorithmParameterException {
    if (password == null || password.length == 0) {
        return new PKCS8EncodedKeySpec(key);
    }
    final EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key);
    final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName());
    final PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    final SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
    @SuppressWarnings("InsecureCryptoUsage") final Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters());
    return encryptedPrivateKeyInfo.getKeySpec(cipher);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 68 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project jodd by oblac.

the class PBKDF2Hash method pbkdf2.

/**
	 * Computes the PBKDF2 hash of a password.
	 *
	 * @param password the password to hash.
	 * @param salt the salt
	 * @param iterations the iteration count (slowness factor)
	 * @param bytes the length of the hash to compute in bytes
	 * @return the PBDKF2 hash of the password
	 */
private static byte[] pbkdf2(char[] password, byte[] salt, int iterations, int bytes) {
    PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bytes * 8);
    SecretKeyFactory skf = null;
    try {
        skf = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);
        return skf.generateSecret(spec).getEncoded();
    } catch (NoSuchAlgorithmException ignore) {
        return null;
    } catch (InvalidKeySpecException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 69 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project orientdb by orientechnologies.

the class OSymmetricKey method create.

protected void create() {
    try {
        SecureRandom secureRandom = new SecureRandom();
        byte[] salt = secureRandom.generateSeed(saltLength);
        KeySpec keySpec = new PBEKeySpec(seedPhrase.toCharArray(), salt, iteration, keySize);
        SecretKeyFactory factory = SecretKeyFactory.getInstance(seedAlgorithm);
        SecretKey tempKey = factory.generateSecret(keySpec);
        secretKey = new SecretKeySpec(tempKey.getEncoded(), secretKeyAlgorithm);
    } catch (Exception ex) {
        throw new OSecurityException("OSymmetricKey.create() Exception: " + ex);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecureRandom(java.security.SecureRandom) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) SecretKeyFactory(javax.crypto.SecretKeyFactory) OException(com.orientechnologies.common.exception.OException) KeyStoreException(java.security.KeyStoreException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 70 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project platform_frameworks_base by android.

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)

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