use of javax.crypto.spec.PBEKeySpec in project XobotOS by xamarin.
the class JDKPKCS12KeyStore 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;
}
use of javax.crypto.spec.PBEKeySpec in project OpenAM by OpenRock.
the class DataEncryptor method decryptWithSymmetricKey.
/**
* Decrypts the given data with a symmetric key generated using shared
* secret.
* @param data the data to be decrypted with symmetric key.
* @param encAlgorithm the encryption algorithm was used for
* encrypting the data.
* @param secret the shared secret to be used for decrypting the data.
* @return the decrypted data.
*/
public static String decryptWithSymmetricKey(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.DECRYPT_MODE, sKey, pbeParameterSpec);
byte[] tmp = Base64.decode(data);
byte[] encData = removePrefix(tmp);
byte[] decData = cipher.doFinal(encData);
return Base64.encode(decData);
} catch (NoSuchAlgorithmException nse) {
throw new Exception(nse.getMessage());
}
}
use of javax.crypto.spec.PBEKeySpec in project yyl_example by Relucent.
the class PBEWithMD5AndDES_Encrypt method encrypt.
/**
* 将传进来的明文以PBEWithMD5AndDES算法进行加密
*
* @param text String
* @return String
*/
public String encrypt(String text) throws Exception {
if (text == null || text.length() == 0) {
return "";
}
PBEKeySpec pbks = new PBEKeySpec(password.toCharArray());
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey k = skf.generateSecret(pbks);
byte[] salt = new byte[8];
Random r = new Random();
r.nextBytes(salt);
Cipher cp = Cipher.getInstance("PBEWithMD5AndDES");
PBEParameterSpec ps = new PBEParameterSpec(salt, 1000);
cp.init(Cipher.ENCRYPT_MODE, k, ps);
byte[] ptext = text.getBytes(encoding);
byte[] ctext = cp.doFinal(ptext);
String result = "";
for (int i = 0; i < salt.length; i++) {
result += salt[i] + " ";
}
for (int i = 0; i < ctext.length; i++) {
result += ctext[i] + " ";
}
return string2hex(result);
}
use of javax.crypto.spec.PBEKeySpec in project opennms by OpenNMS.
the class JCEKSSecureCredentialsVault method setCredentials.
@Override
public void setCredentials(String alias, Credentials credentials) {
try {
byte[] credentialBytes = toBase64EncodedByteArray(credentials);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
SecretKey generatedSecret = factory.generateSecret(new PBEKeySpec(new String(credentialBytes).toCharArray(), m_salt, m_iterationCount, m_keyLength));
KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(m_password);
m_keystore.setEntry(alias, new KeyStore.SecretKeyEntry(generatedSecret), keyStorePP);
writeKeystoreToDisk();
} catch (KeyStoreException | InvalidKeySpecException | NoSuchAlgorithmException | IOException e) {
throw Throwables.propagate(e);
}
}
use of javax.crypto.spec.PBEKeySpec in project opennms by OpenNMS.
the class JCEKSSecureCredentialsVault method setCredentials.
@Override
public void setCredentials(String alias, Credentials credentials) {
try {
byte[] credentialBytes = toBase64EncodedByteArray(credentials);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
SecretKey generatedSecret = factory.generateSecret(new PBEKeySpec(new String(credentialBytes).toCharArray(), m_salt, m_iterationCount, m_keyLength));
KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(m_password);
m_keystore.setEntry(alias, new KeyStore.SecretKeyEntry(generatedSecret), keyStorePP);
writeKeystoreToDisk();
} catch (KeyStoreException | InvalidKeySpecException | NoSuchAlgorithmException | IOException e) {
throw Throwables.propagate(e);
}
}
Aggregations