Search in sources :

Example 96 with SecretKeyFactory

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

Example 97 with SecretKeyFactory

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());
    }
}
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 98 with SecretKeyFactory

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;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) SQLException(android.database.SQLException)

Example 99 with SecretKeyFactory

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());
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) PKCS12PBEParams(org.bouncycastle.asn1.pkcs.PKCS12PBEParams) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERBMPString(org.bouncycastle.asn1.DERBMPString) BERConstructedOctetString(org.bouncycastle.asn1.BERConstructedOctetString) DEROctetString(org.bouncycastle.asn1.DEROctetString) 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 100 with SecretKeyFactory

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);
    }
}
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) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Aggregations

SecretKeyFactory (javax.crypto.SecretKeyFactory)129 SecretKey (javax.crypto.SecretKey)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)75 Cipher (javax.crypto.Cipher)58 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 DESKeySpec (javax.crypto.spec.DESKeySpec)28 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)26 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)26 KeySpec (java.security.spec.KeySpec)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)23 SecureRandom (java.security.SecureRandom)18 KeyStoreException (java.security.KeyStoreException)16 IOException (java.io.IOException)15 InvalidKeyException (java.security.InvalidKeyException)14 PrivateKey (java.security.PrivateKey)12 CertificateException (java.security.cert.CertificateException)12 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)12 UnrecoverableKeyException (java.security.UnrecoverableKeyException)11 Key (java.security.Key)10 KeyFactory (java.security.KeyFactory)10