use of javax.crypto.SecretKeyFactory in project opennms by OpenNMS.
the class JCEKSSecureCredentialsVault method getCredentials.
@Override
public Credentials getCredentials(String alias) {
try {
KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(m_password);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
KeyStore.SecretKeyEntry ske = (KeyStore.SecretKeyEntry) m_keystore.getEntry(alias, keyStorePP);
if (ske == null) {
return null;
}
PBEKeySpec keySpec = (PBEKeySpec) factory.getKeySpec(ske.getSecretKey(), PBEKeySpec.class);
return fromBase64EncodedByteArray(new String(keySpec.getPassword()).getBytes());
} catch (KeyStoreException | InvalidKeySpecException | NoSuchAlgorithmException | IOException | ClassNotFoundException | UnrecoverableEntryException e) {
throw Throwables.propagate(e);
}
}
use of javax.crypto.SecretKeyFactory in project OpenAM by OpenRock.
the class DataEncryptor method encryptWithSymmetricKey.
/**
* Encrypts the given data with a symmetric key that was generated
* using given shared secret.
* @param data the data to be encrypted.
* @param encAlgorithm the encryption algorithm to be used.
* The encryption algorithm must be one of the supported
* algorithm by the underlying JCE encryption provider.
* For password based encryptions, the encryption algorithm
* PBEWithMD5AndDES is commonly used.
* @param secret the shared secret to be used for symmetric encryption.
* @return the encrypted data in Base64 encoded format.
*/
public static String encryptWithSymmetricKey(String data, String encAlgorithm, String secret) throws Exception {
try {
String algorithm = encAlgorithm;
if (!algorithm.startsWith("PBEWith")) {
algorithm = "PBEWithMD5And" + encAlgorithm;
}
SecretKeyFactory skFactory = SecretKeyFactory.getInstance(algorithm);
PBEKeySpec pbeKeySpec = new PBEKeySpec(secret.toCharArray());
SecretKey sKey = skFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, sKey, pbeParameterSpec);
byte[] encData = cipher.doFinal(data.getBytes("UTF-8"));
encData = addPrefix(encData);
return Base64.encode(encData);
} catch (NoSuchAlgorithmException nse) {
throw new Exception(nse.getMessage());
}
}
use of javax.crypto.SecretKeyFactory in project WordPress-Android by wordpress-mobile.
the class WPLegacyMigrationUtils method encryptPassword.
private static String encryptPassword(String clearText) {
try {
DESKeySpec keySpec = new DESKeySpec(DEPRECATED_DB_PASSWORD_SECRET.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return Base64.encodeToString(cipher.doFinal(clearText.getBytes("UTF-8")), Base64.DEFAULT);
} catch (Exception e) {
}
return clearText;
}
use of javax.crypto.SecretKeyFactory in project XobotOS by xamarin.
the class JDKPKCS12KeyStore method cryptData.
protected byte[] cryptData(boolean forEncryption, AlgorithmIdentifier algId, char[] password, boolean wrongPKCS12Zero, byte[] data) throws IOException {
String algorithm = algId.getObjectId().getId();
PKCS12PBEParams pbeParams = new PKCS12PBEParams((ASN1Sequence) algId.getParameters());
PBEKeySpec pbeSpec = new PBEKeySpec(password);
try {
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
JCEPBEKey key = (JCEPBEKey) keyFact.generateSecret(pbeSpec);
key.setTryWrongPKCS12Zero(wrongPKCS12Zero);
Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
int mode = forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
cipher.init(mode, key, defParams);
return cipher.doFinal(data);
} catch (Exception e) {
throw new IOException("exception decrypting data - " + e.toString());
}
}
use of javax.crypto.SecretKeyFactory in project XobotOS by xamarin.
the class JDKKeyStore 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);
}
}
Aggregations