Search in sources :

Example 76 with KeySpec

use of java.security.spec.KeySpec in project ice by JBEI.

the class TokenHash method encrypt.

public String encrypt(String value, String salt) {
    if (value == null || value.trim().isEmpty() || salt == null || salt.trim().isEmpty())
        throw new NullPointerException("Cannot encrypt null value or salt");
    KeySpec spec = new PBEKeySpec(value.toCharArray(), salt.getBytes(), PBKDF2_ITERATIONS, HASH_BYTE_SIZE);
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        byte[] hash = keyFactory.generateSecret(spec).getEncoded();
        return DatatypeConverter.printBase64Binary(hash);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        return null;
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 77 with KeySpec

use of java.security.spec.KeySpec in project ice by JBEI.

the class AccountUtils method encryptNewUserPassword.

public static String encryptNewUserPassword(String password, String salt) {
    if (StringUtils.isEmpty(password) || StringUtils.isEmpty(salt))
        throw new IllegalArgumentException("Password and salt cannot be empty");
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 20000, 160);
    try {
        SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        byte[] hash = f.generateSecret(spec).getEncoded();
        return bytesToHex(hash);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        Logger.error(e);
        return null;
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 78 with KeySpec

use of java.security.spec.KeySpec in project oxCore by GluuFederation.

the class StringEncrypter method encrypt.

/**
 * Encrypt a string
 *
 * @param unencryptedString
 *            String to encrypt
 * @return Encrypted string (using scheme and key specified at construction)
 * @throws EncryptionException
 */
public String encrypt(final String unencryptedString, String encryptionKey) throws EncryptionException {
    lock.lock();
    try {
        final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
        String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
        KeySpec keySpec;
        if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
            keySpec = new DESedeKeySpec(keyAsBytes);
        } else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
            keySpec = new DESKeySpec(keyAsBytes);
        } else {
            throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
        }
        return encrypt(unencryptedString, keySpec);
    } catch (final Exception e) {
        throw new EncryptionException(e);
    } finally {
        lock.unlock();
    }
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) KeySpec(java.security.spec.KeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 79 with KeySpec

use of java.security.spec.KeySpec in project inbot-utils by Inbot.

the class AESUtils method getKey.

private static SecretKey getKey(String salt, String password) {
    try {
        // https://tools.ietf.org/html/rfc2898
        // sha1 with 1000 iterations and 256 bits is good enough here http://stackoverflow.com/questions/6126061/pbekeyspec-what-do-the-iterationcount-and-keylength-parameters-influence
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 1000, 256);
        SecretKey tmp = factory.generateSecret(spec);
        return new SecretKeySpec(tmp.getEncoded(), "AES");
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        throw new IllegalStateException("cannot create key: " + e.getMessage(), e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

KeySpec (java.security.spec.KeySpec)79 PBEKeySpec (javax.crypto.spec.PBEKeySpec)29 SecretKeyFactory (javax.crypto.SecretKeyFactory)27 KeyFactory (java.security.KeyFactory)21 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)21 SecretKeySpec (javax.crypto.spec.SecretKeySpec)21 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)20 SecretKey (javax.crypto.SecretKey)19 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)15 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)12 PrivateKey (java.security.PrivateKey)11 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)11 InvalidKeyException (java.security.InvalidKeyException)9 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)9 DESKeySpec (javax.crypto.spec.DESKeySpec)9 IOException (java.io.IOException)8 BigInteger (java.math.BigInteger)7 NoSuchProviderException (java.security.NoSuchProviderException)7 PublicKey (java.security.PublicKey)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6