Search in sources :

Example 76 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project android-demos by novoda.

the class Encrypt method crypt.

public String crypt(int mode, String encryption_subject) throws CryptException {
    final PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(SALT, 20);
    final SecretKeyFactory kf = getSecretKeyFactory();
    final SecretKey k = getSecretKey(kf);
    final Cipher crypter = getCipherInstance();
    String result;
    switch(mode) {
        case Cipher.DECRYPT_MODE:
            initialise(ps, k, crypter, Cipher.DECRYPT_MODE);
            result = getString(encryption_subject, crypter);
            break;
        case Cipher.ENCRYPT_MODE:
        default:
            initialise(ps, k, crypter, Cipher.ENCRYPT_MODE);
            result = encode(encryption_subject, crypter);
    }
    return result;
}
Also used : SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 77 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory 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 78 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory 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)

Example 79 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project robovm by robovm.

the class BcKeyStoreSpi method makePBECipher.

protected Cipher makePBECipher(String algorithm, int mode, char[] password, byte[] salt, int iterationCount) throws IOException {
    try {
        PBEKeySpec pbeSpec = new PBEKeySpec(password);
        SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
        PBEParameterSpec defParams = new PBEParameterSpec(salt, iterationCount);
        Cipher cipher = Cipher.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
        cipher.init(mode, keyFact.generateSecret(pbeSpec), defParams);
        return cipher;
    } catch (Exception e) {
        throw new IOException("Error initialising store of key store: " + e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 80 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project robovm by robovm.

the class PKCS12KeyStoreSpi method wrapKey.

protected byte[] wrapKey(String algorithm, Key key, PKCS12PBEParams pbeParams, char[] password) throws IOException {
    PBEKeySpec pbeSpec = new PBEKeySpec(password);
    byte[] out;
    try {
        SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
        PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
        Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
        cipher.init(Cipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), defParams);
        out = cipher.wrap(key);
    } catch (Exception e) {
        throw new IOException("exception encrypting data - " + e.toString());
    }
    return out;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException)

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