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;
}
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);
}
}
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;
}
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);
}
}
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;
}
Aggregations