use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PBESecretKeyFactory method engineGenerateSecret.
protected SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException {
if (keySpec instanceof PBEKeySpec) {
PBEKeySpec pbeSpec = (PBEKeySpec) keySpec;
CipherParameters param;
if (pbeSpec.getSalt() == null) {
return new BCPBEKey(this.algName, this.algOid, scheme, digest, keySize, ivSize, pbeSpec, null);
}
if (forCipher) {
param = PBE.Util.makePBEParameters(pbeSpec, scheme, digest, keySize, ivSize);
} else {
param = PBE.Util.makePBEMacParameters(pbeSpec, scheme, digest, keySize);
}
return new BCPBEKey(this.algName, this.algOid, scheme, digest, keySize, ivSize, pbeSpec, param);
}
throw new InvalidKeySpecException("Invalid KeySpec");
}
use of javax.crypto.spec.PBEKeySpec in project BIMserver by opensourceBIM.
the class Authenticator 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) throws NoSuchAlgorithmException, InvalidKeySpecException {
PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bytes * 8);
SecretKeyFactory skf = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);
return skf.generateSecret(spec).getEncoded();
}
use of javax.crypto.spec.PBEKeySpec in project aion by aionnetwork.
the class KeystoreFormat method hash.
private static byte[] hash(String encryptedData, byte[] salt, int iterations) throws Exception {
char[] chars = encryptedData.toCharArray();
PBEKeySpec spec = new PBEKeySpec(chars, salt, iterations, 256);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
return skf.generateSecret(spec).getEncoded();
}
use of javax.crypto.spec.PBEKeySpec in project felix by apache.
the class CryptoServiceSingleton method generateAESKey.
/**
* Generate the AES key from the salt and the private key.
*
* @param salt the salt (hexadecimal)
* @param privateKey the private key
* @return the generated key.
*/
private SecretKey generateAESKey(String privateKey, String salt) {
try {
byte[] raw = Hex.decodeHex(salt.toCharArray());
KeySpec spec = new PBEKeySpec(privateKey.toCharArray(), raw, iterationCount, keySize);
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBKDF_2_WITH_HMAC_SHA_1);
return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), AES_ECB_ALGORITHM);
} catch (DecoderException e) {
throw new IllegalStateException(e);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
} catch (InvalidKeySpecException e) {
throw new IllegalStateException(e);
}
}
use of javax.crypto.spec.PBEKeySpec in project nifi by apache.
the class CipherUtility method initPBECipher.
/**
* Initializes a {@link Cipher} object with the given PBE parameters.
*
* @param algorithm the algorithm
* @param provider the JCA provider
* @param password the password
* @param salt the salt
* @param iterationCount the KDF iteration count
* @param encryptMode true to encrypt; false to decrypt
* @return the initialized Cipher
* @throws IllegalArgumentException if any parameter is invalid
*/
public static Cipher initPBECipher(String algorithm, String provider, String password, byte[] salt, int iterationCount, boolean encryptMode) throws IllegalArgumentException {
try {
// Initialize secret key from password
final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
final SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm, provider);
SecretKey tempKey = factory.generateSecret(pbeKeySpec);
final PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterationCount);
Cipher cipher = Cipher.getInstance(algorithm, provider);
cipher.init(encryptMode ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, tempKey, parameterSpec);
return cipher;
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new IllegalArgumentException("One or more parameters to initialize the PBE cipher were invalid", e);
}
}
Aggregations