Search in sources :

Example 56 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project hbase by apache.

the class Encryption method pbkdf128.

/**
   * Return a 128 bit key derived from the concatenation of the supplied
   * arguments using PBKDF2WithHmacSHA1 at 10,000 iterations.
   * 
   */
public static byte[] pbkdf128(byte[]... args) {
    byte[] salt = new byte[128];
    Bytes.random(salt);
    StringBuilder sb = new StringBuilder();
    for (byte[] b : args) {
        sb.append(Arrays.toString(b));
    }
    PBEKeySpec spec = new PBEKeySpec(sb.toString().toCharArray(), salt, 10000, 128);
    try {
        return SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(spec).getEncoded();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 57 with PBEKeySpec

use of javax.crypto.spec.PBEKeySpec in project elasticsearch by elastic.

the class KeyStoreWrapper method setString.

/**
     * Set a string setting.
     *
     * @throws IllegalArgumentException if the value is not ASCII
     */
void setString(String setting, char[] value) throws GeneralSecurityException {
    if (ASCII_ENCODER.canEncode(CharBuffer.wrap(value)) == false) {
        throw new IllegalArgumentException("Value must be ascii");
    }
    SecretKey secretKey = secretFactory.generateSecret(new PBEKeySpec(value));
    keystore.get().setEntry(setting, new KeyStore.SecretKeyEntry(secretKey), keystorePassword.get());
    settingNames.add(setting);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) KeyStore(java.security.KeyStore)

Example 58 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 59 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 60 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)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