Search in sources :

Example 76 with SecretKey

use of javax.crypto.SecretKey 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 77 with SecretKey

use of javax.crypto.SecretKey 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 78 with SecretKey

use of javax.crypto.SecretKey in project newsrob by marianokamp.

the class EntryManager method getSecretKey.

private static SecretKey getSecretKey() throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException {
    DESKeySpec keySpec = new DESKeySpec("EntryManager.class".getBytes("UTF8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(keySpec);
    return secretKey;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 79 with SecretKey

use of javax.crypto.SecretKey in project UltimateAndroid by cymcsg.

the class DefaultAppLock method encryptPassword.

private String encryptPassword(String clearText) {
    try {
        DESKeySpec keySpec = new DESKeySpec(PASSWORD_ENC_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);
        String encrypedPwd = Base64.encodeToString(cipher.doFinal(clearText.getBytes("UTF-8")), Base64.DEFAULT);
        return encrypedPwd;
    } catch (Exception e) {
    }
    return clearText;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 80 with SecretKey

use of javax.crypto.SecretKey in project UltimateAndroid by cymcsg.

the class DefaultAppLock method decryptPassword.

private String decryptPassword(String encryptedPwd) {
    try {
        DESKeySpec keySpec = new DESKeySpec(PASSWORD_ENC_SECRET.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);
        byte[] encryptedWithoutB64 = Base64.decode(encryptedPwd, Base64.DEFAULT);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainTextPwdBytes = cipher.doFinal(encryptedWithoutB64);
        return new String(plainTextPwdBytes);
    } catch (Exception e) {
    }
    return encryptedPwd;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

SecretKey (javax.crypto.SecretKey)491 Cipher (javax.crypto.Cipher)176 SecretKeySpec (javax.crypto.spec.SecretKeySpec)141 KeyGenerator (javax.crypto.KeyGenerator)121 SecretKeyFactory (javax.crypto.SecretKeyFactory)89 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)87 SecureRandom (java.security.SecureRandom)61 InvalidKeyException (java.security.InvalidKeyException)58 PBEKeySpec (javax.crypto.spec.PBEKeySpec)58 IvParameterSpec (javax.crypto.spec.IvParameterSpec)46 IOException (java.io.IOException)44 Test (org.junit.Test)40 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)35 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)34 KeyStore (java.security.KeyStore)32 PrivateKey (java.security.PrivateKey)30 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)30 KeyStoreException (java.security.KeyStoreException)29 BadPaddingException (javax.crypto.BadPaddingException)29 Mac (javax.crypto.Mac)29