Example usage for javax.crypto.spec PBEKeySpec PBEKeySpec

List of usage examples for javax.crypto.spec PBEKeySpec PBEKeySpec

Introduction

In this page you can find the example usage for javax.crypto.spec PBEKeySpec PBEKeySpec.

Prototype

public PBEKeySpec(char[] password) 

Source Link

Document

Constructor that takes a password.

Usage

From source file:org.apache.ranger.plugin.util.PasswordUtils.java

private String decrypt() throws IOException {
    String ret = null;// ww  w  .  j  a  v a 2s.c  o m
    try {
        byte[] decodedPassword = Base64.decode(password);
        Cipher engine = Cipher.getInstance(CRYPT_ALGO);
        PBEKeySpec keySpec = new PBEKeySpec(encryptKey);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(CRYPT_ALGO);
        SecretKey key = skf.generateSecret(keySpec);
        engine.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, ITERATION_COUNT));
        String decrypted = new String(engine.doFinal(decodedPassword));
        int foundAt = decrypted.indexOf(LEN_SEPARATOR_STR);
        if (foundAt > -1) {
            if (decrypted.length() > foundAt) {
                ret = decrypted.substring(foundAt + 1);
            } else {
                ret = "";
            }
        } else {
            ret = null;
        }
    } catch (Throwable t) {
        LOG.error("Unable to decrypt password due to error", t);
        throw new IOException("Unable to decrypt password due to error", t);
    }
    return ret;
}

From source file:de.alpharogroup.crypto.simple.SimpleEncryptor.java

/**
 * Initializes the {@link SimpleEncryptor} object.
 * /*from ww  w.jav a 2s. c om*/
 * @throws InvalidAlgorithmParameterException
 *             is thrown if initialization of the cypher object fails.
 * @throws NoSuchPaddingException
 *             is thrown if instantiation of the cypher object fails.
 * @throws InvalidKeySpecException
 *             is thrown if generation of the SecretKey object fails.
 * @throws NoSuchAlgorithmException
 *             is thrown if instantiation of the SecretKeyFactory object fails.
 * @throws InvalidKeyException
 *             is thrown if initialization of the cypher object fails.
 */
private void initialize() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        InvalidKeyException, InvalidAlgorithmParameterException {
    if (!isInitialized()) {
        final KeySpec keySpec;
        if (this.getPrivateKey() != null) {
            keySpec = new PBEKeySpec(this.getPrivateKey().toCharArray());
        } else {
            keySpec = new PBEKeySpec(CryptConst.PRIVATE_KEY.toCharArray());
        }
        final SecretKeyFactory factory = SecretKeyFactory.getInstance(CryptConst.PBEWITH_MD5AND_DES);
        final SecretKey key = factory.generateSecret(keySpec);
        this.cipher = Cipher.getInstance(key.getAlgorithm());
        final AlgorithmParameterSpec paramSpec = new PBEParameterSpec(CryptConst.SALT,
                CryptConst.ITERATIONCOUNT);
        this.cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        initialized = true;
    }
}

From source file:JavaTron.JTP.java

/**
 * Decrypts an encrypted string/*from   w ww.  j av a  2 s  . com*/
 * @param property
 * @return A plaintext string
 */
public static String decrypt(String property) {
    String p = new String();
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        p = new String(pbeCipher.doFinal(base64Decode(property)));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return p;
}

From source file:org.hawk.core.security.FileBasedCredentialsStore.java

private String encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException {

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(encryptionKey));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, 20));
    return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
}

From source file:org.artifactory.security.CryptoHelper.java

public static SecretKey generatePbeKey(String password) {
    try {//  ww  w.j  av  a2s. com
        PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SYM_ALGORITHM);
        SecretKey secretKey = keyFactory.generateSecret(pbeKeySpec);
        return secretKey;
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("No such algorithm: " + e.getMessage());
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException("Unexpected exception: ", e);
    }
}

From source file:org.artifactory.security.crypto.CryptoHelper.java

private static SecretKey generatePbeKey(String password) {
    try {/*w  ww  .  ja  v  a  2  s . c o  m*/
        PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SYM_ALGORITHM);
        SecretKey secretKey = keyFactory.generateSecret(pbeKeySpec);
        return secretKey;
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("No such algorithm: " + e.getMessage());
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException("Unexpected exception: ", e);
    }
}

From source file:org.hawk.core.security.FileBasedCredentialsStore.java

private String decrypt(String property) throws GeneralSecurityException, IOException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(encryptionKey));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, 20));
    return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8");
}

From source file:org.apache.nifi.processors.standard.util.crypto.OpenSSLPKCS5CipherProvider.java

protected Cipher getInitializedCipher(EncryptionMethod encryptionMethod, String password, byte[] salt,
        boolean encryptMode) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
    if (encryptionMethod == null) {
        throw new IllegalArgumentException("The encryption method must be specified");
    }/* w w  w .j  a v  a2s  .com*/

    if (StringUtils.isEmpty(password)) {
        throw new IllegalArgumentException("Encryption with an empty password is not supported");
    }

    validateSalt(encryptionMethod, salt);

    String algorithm = encryptionMethod.getAlgorithm();
    String provider = encryptionMethod.getProvider();

    // Initialize secret key from password
    final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
    final SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm, provider);
    SecretKey tempKey = factory.generateSecret(pbeKeySpec);

    final PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, getIterationCount());
    Cipher cipher = Cipher.getInstance(algorithm, provider);
    cipher.init(encryptMode ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, tempKey, parameterSpec);
    return cipher;
}

From source file:org.linagora.linshare.core.utils.SymmetricEnciphermentPBEwithAES.java

private static SecretKey getSecretKey(String pw) throws NoSuchAlgorithmException, InvalidKeySpecException {

    PBEKeySpec key_spec = new PBEKeySpec(pw.toCharArray());
    SecretKeyFactory key_factory = SecretKeyFactory.getInstance(SECRETKEYFACTORY_ALGO);
    SecretKey secret_key = key_factory.generateSecret(key_spec);

    return secret_key;
}

From source file:it.scoppelletti.programmerpower.security.spi.PBEKeyFactory.java

@Override
protected KeySpec getKeySpec(String alg, KeyRep.Type keyType, Properties props, String prefix) {
    int count, keyLen;
    boolean varKeySize;
    String countName, saltName, name, value;
    byte[] salt;/*from ww w  . j  av  a2s  . co m*/
    KeySpec keySpec;
    SecureString pwd = null;

    saltName = Strings.concat(prefix, PBEKeyFactory.PROP_SALT);
    value = props.getProperty(saltName);
    if (!Strings.isNullOrEmpty(value)) {
        try {
            salt = Hex.decodeHex(value.toCharArray());
        } catch (DecoderException ex) {
            throw SecurityUtils.toSecurityException(ex);
        }
    } else {
        salt = null;
    }

    countName = Strings.concat(prefix, PBEKeyFactory.PROP_ITERATIONCOUNT);
    value = props.getProperty(countName);
    if (!Strings.isNullOrEmpty(value)) {
        if (salt == null) {
            throw new ArgumentNullException(saltName);
        }

        count = Integer.parseInt(value);
    } else if (salt != null) {
        throw new ArgumentNullException(countName);
    } else {
        count = 0;
    }

    name = Strings.concat(prefix, PBEKeyFactory.PROP_KEYLEN);
    value = props.getProperty(name);
    varKeySize = !Strings.isNullOrEmpty(value);
    if (varKeySize) {
        if (salt == null) {
            throw new ArgumentNullException(saltName);
        }

        keyLen = Integer.parseInt(value);
    } else {
        keyLen = 0;
    }

    name = Strings.concat(prefix, PBEKeyFactory.PROP_PASSWORD);
    pwd = new SecureString(props.getProperty(name));
    if (pwd.isEmpty()) {
        throw new ArgumentNullException(name);
    }

    try {
        if (salt == null) {
            keySpec = new PBEKeySpec(pwd.getValue());
        } else if (varKeySize) {
            keySpec = new PBEKeySpec(pwd.getValue(), salt, count, keyLen);
        } else {
            keySpec = new PBEKeySpec(pwd.getValue(), salt, count);
        }
    } finally {
        if (pwd != null) {
            pwd.clear();
            pwd = null;
        }
    }

    return keySpec;
}