Search in sources :

Example 1 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project che by eclipse.

the class PBKDF2PasswordEncryptor method computeHash.

private HashCode computeHash(char[] password, byte[] salt, int iterations) {
    try {
        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_NAME);
        final KeySpec keySpec = new PBEKeySpec(password, salt, iterations, 512);
        return HashCode.fromBytes(keyFactory.generateSecret(keySpec).getEncoded());
    } catch (NoSuchAlgorithmException | InvalidKeySpecException x) {
        throw new RuntimeException(x.getMessage(), x);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 2 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project android_frameworks_base by ParanoidAndroid.

the class BackupManagerService method buildCharArrayKey.

private SecretKey buildCharArrayKey(char[] pwArray, byte[] salt, int rounds) {
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        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 3 with SecretKeyFactory

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

the class MasterSecretUtil method generateIterationCount.

private static int generateIterationCount(String passphrase, byte[] salt) {
    //ms
    int TARGET_ITERATION_TIME = 50;
    //default for low-end devices
    int MINIMUM_ITERATION_COUNT = 100;
    //baseline starting iteration count
    int BENCHMARK_ITERATION_COUNT = 10000;
    try {
        PBEKeySpec keyspec = new PBEKeySpec(passphrase.toCharArray(), salt, BENCHMARK_ITERATION_COUNT);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA1AND128BITAES-CBC-BC");
        long startTime = System.currentTimeMillis();
        skf.generateSecret(keyspec);
        long finishTime = System.currentTimeMillis();
        int scaledIterationTarget = (int) (((double) BENCHMARK_ITERATION_COUNT / (double) (finishTime - startTime)) * TARGET_ITERATION_TIME);
        if (scaledIterationTarget < MINIMUM_ITERATION_COUNT)
            return MINIMUM_ITERATION_COUNT;
        else
            return scaledIterationTarget;
    } catch (NoSuchAlgorithmException e) {
        Log.w("MasterSecretUtil", e);
        return MINIMUM_ITERATION_COUNT;
    } catch (InvalidKeySpecException e) {
        Log.w("MasterSecretUtil", e);
        return MINIMUM_ITERATION_COUNT;
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 4 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project DataX by alibaba.

the class DESCipher method decrypt.

/**
	 *   * 解密
	 *
	 *   *
	 *
	 *   * @param src
	 *
	 *   * 密文(字节)
	 *
	 *   * @param key
	 *
	 *   * 密钥,长度必须是8的倍数
	 *
	 *   * @return 明文(字节)
	 *
	 *   * @throws Exception
	 *
	 *   
	 */
public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
    // DES算法要求有一个可信任的随机数源
    SecureRandom sr = new SecureRandom();
    // 从原始密匙数据创建一个DESKeySpec对象
    DESKeySpec dks = new DESKeySpec(key);
    // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
    // 一个SecretKey对象
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher对象实际完成解密操作
    Cipher cipher = Cipher.getInstance(DES);
    // 用密匙初始化Cipher对象
    cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
    return cipher.doFinal(src);
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 5 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project DataX by alibaba.

the class DESCipher method encrypt.

/**
	 *   * 加密
	 *
	 *   *
	 *
	 *   * @param src
	 *
	 *   * 明文(字节)
	 *
	 *   * @param key
	 *
	 *   * 密钥,长度必须是8的倍数
	 *
	 *   * @return 密文(字节)
	 *
	 *   * @throws Exception
	 *
	 *   
	 */
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
    // DES算法要求有一个可信任的随机数源
    SecureRandom sr = new SecureRandom();
    // 从原始密匙数据创建DESKeySpec对象
    DESKeySpec dks = new DESKeySpec(key);
    // 创建一个密匙工厂,然后用它把DESKeySpec转换成
    // 一个SecretKey对象
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher对象实际完成加密操作
    Cipher cipher = Cipher.getInstance(DES);
    // 用密匙初始化Cipher对象
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
    return cipher.doFinal(src);
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

SecretKeyFactory (javax.crypto.SecretKeyFactory)129 SecretKey (javax.crypto.SecretKey)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)75 Cipher (javax.crypto.Cipher)58 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 DESKeySpec (javax.crypto.spec.DESKeySpec)28 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)26 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)26 KeySpec (java.security.spec.KeySpec)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)23 SecureRandom (java.security.SecureRandom)18 KeyStoreException (java.security.KeyStoreException)16 IOException (java.io.IOException)15 InvalidKeyException (java.security.InvalidKeyException)14 PrivateKey (java.security.PrivateKey)12 CertificateException (java.security.cert.CertificateException)12 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)12 UnrecoverableKeyException (java.security.UnrecoverableKeyException)11 Key (java.security.Key)10 KeyFactory (java.security.KeyFactory)10