Search in sources :

Example 6 with PBEKeySpec

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

Example 7 with PBEKeySpec

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());
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SecretKeyFactory(javax.crypto.SecretKeyFactory) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with PBEKeySpec

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);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 9 with PBEKeySpec

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);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) SecretKey(javax.crypto.SecretKey) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 10 with PBEKeySpec

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);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) SecretKey(javax.crypto.SecretKey) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

PBEKeySpec (javax.crypto.spec.PBEKeySpec)249 SecretKeyFactory (javax.crypto.SecretKeyFactory)190 SecretKey (javax.crypto.SecretKey)118 Cipher (javax.crypto.Cipher)82 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)73 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)63 KeySpec (java.security.spec.KeySpec)59 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)59 SecretKeySpec (javax.crypto.spec.SecretKeySpec)49 IOException (java.io.IOException)25 KeyStoreException (java.security.KeyStoreException)23 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)22 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)17 CertificateException (java.security.cert.CertificateException)15 GeneralSecurityException (java.security.GeneralSecurityException)14 UnrecoverableKeyException (java.security.UnrecoverableKeyException)14 AlgorithmParameters (java.security.AlgorithmParameters)13 Key (java.security.Key)13 KeyStore (java.security.KeyStore)13 InvalidKeyException (java.security.InvalidKeyException)12