Example usage for javax.crypto.spec PBEParameterSpec PBEParameterSpec

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

Introduction

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

Prototype

public PBEParameterSpec(byte[] salt, int iterationCount) 

Source Link

Document

Constructs a parameter set for password-based encryption as defined in the PKCS #5 standard.

Usage

From source file:org.nuclos.client.LocalUserCaches.java

private static Cipher createCipher(int mode, String password) throws GeneralSecurityException {
    String alg = "PBEWithSHA1AndDESede"; //BouncyCastle has better algorithms
    PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(alg);
    SecretKey secretKey = keyFactory.generateSecret(keySpec);

    Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
    // TODO: A fixed salt doesn't help anything.
    cipher.init(mode, secretKey, new PBEParameterSpec("saltsalt".getBytes(), 2000));

    return cipher;
}

From source file:eu.vital.vitalcep.collector.Collector.java

private static String decrypt(String property) throws GeneralSecurityException, IOException {
    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));
    return new String(Base64.getDecoder().decode(property), StandardCharsets.UTF_8);
}

From source file:org.apache.nifi.security.util.crypto.CipherUtility.java

/**
 * Initializes a {@link Cipher} object with the given PBE parameters.
 *
 * @param algorithm      the algorithm/*  w w  w  . ja  v  a2  s  .c  o  m*/
 * @param provider       the JCA provider
 * @param password       the password
 * @param salt           the salt
 * @param iterationCount the KDF iteration count
 * @param encryptMode    true to encrypt; false to decrypt
 * @return the initialized Cipher
 * @throws IllegalArgumentException if any parameter is invalid
 */
public static Cipher initPBECipher(String algorithm, String provider, String password, byte[] salt,
        int iterationCount, boolean encryptMode) throws IllegalArgumentException {
    try {
        // 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, iterationCount);
        Cipher cipher = Cipher.getInstance(algorithm, provider);
        cipher.init(encryptMode ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, tempKey, parameterSpec);
        return cipher;
    } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException
            | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) {
        throw new IllegalArgumentException("One or more parameters to initialize the PBE cipher were invalid",
                e);
    }
}

From source file:com.aurel.track.vc.bl.VersionControlBL.java

private static String encrypt(String clearText, char[] password) {
    // Create PBE parameter set
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
    byte[] ciphertext = { 0 };

    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    try {/*from   w  ww  . java 2 s .  co m*/
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

        // Create PBE Cipher
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");

        // Initialize PBE Cipher with key and parameters
        pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

        // Encrypt the cleartext
        ciphertext = pbeCipher.doFinal(clearText.getBytes());
    } catch (Exception e) {
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    }
    return new String(Base64.encodeBase64String(ciphertext));
}

From source file:bioLockJ.module.agent.MailAgent.java

private static String encrypt(final String password)
        throws GeneralSecurityException, UnsupportedEncodingException {
    final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    final SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    final Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return base64Encode(pbeCipher.doFinal(password.getBytes("UTF-8")));
}

From source file:com.thruzero.common.core.security.SimpleCipher.java

/**
 * Construct using the {@code salt}, {@code passPhrase} and {@code iterationCount} defined by the given
 * {@code simpleCipherConfiguration}.// w ww.j a v a 2  s  . com
 *
 * @throws SimpleCipherException
 */
public SimpleCipher(final SimpleCipherConfiguration simpleCipherConfiguration) throws SimpleCipherException {
    try {
        int count = simpleCipherConfiguration.getIterationCount();
        byte[] salt = simpleCipherConfiguration.getSalt();

        KeySpec keySpec = new PBEKeySpec(simpleCipherConfiguration.getPassPhrase(), salt, count);

        AlgorithmParameterSpec parameterSpec = new PBEParameterSpec(salt, count);
        SecretKey key = SecretKeyFactory.getInstance(PBE_WITH_MD5_AND_DES).generateSecret(keySpec);

        encryptionCipher = Cipher.getInstance(key.getAlgorithm());
        encryptionCipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);

        decryptionCipher = Cipher.getInstance(key.getAlgorithm());
        decryptionCipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
    } catch (InvalidAlgorithmParameterException e) {
        throw new SimpleCipherException(
                "Couldn't instantiate SimpleCipher because of " + ExceptionUtils.getMessage(e), e);
    } catch (Exception e) {
        throw new SimpleCipherException(
                "Couldn't instantiate SimpleCipher because of " + ExceptionUtils.getMessage(e), e);
    }
}

From source file:org.fuin.utils4j.Utils4J.java

/**
 * Creates a cipher for encryption or decryption.
 * /* w w  w  .j  av  a  2s  .c  o m*/
 * @param algorithm
 *            PBE algorithm like "PBEWithMD5AndDES" or
 *            "PBEWithMD5AndTripleDES".
 * @param mode
 *            Encyrption or decyrption.
 * @param password
 *            Password.
 * @param salt
 *            Salt usable with algorithm.
 * @param count
 *            Iterations.
 * 
 * @return Ready initialized cipher.
 * 
 * @throws GeneralSecurityException
 *             Error creating the cipher.
 */
private static Cipher createCipher(final String algorithm, final int mode, final char[] password,
        final byte[] salt, final int count) throws GeneralSecurityException {

    final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
    final PBEKeySpec keySpec = new PBEKeySpec(password);
    final SecretKey key = keyFactory.generateSecret(keySpec);
    final Cipher cipher = Cipher.getInstance(algorithm);
    final PBEParameterSpec params = new PBEParameterSpec(salt, count);
    cipher.init(mode, key, params);
    return cipher;

}

From source file:org.mxupdate.eclipse.properties.ProjectProperties.java

/**
 * Returns encrypted/decrypted by salt password. Uses SHA-1 Message Digest
 * Algorithm as defined in NIST's FIPS 180-1. The output of this algorithm
 * is a 160-bit digest.//  w w w.  ja va2 s.  c o m
 *
 * @param _password     password to encrypt / decrypt
 * @param _decrypt      <i>true</i> to decrypt or <i>false</i> to encrypt
 * @return decrypted / encrypted by salt password
 * @see #PDE_ALGORITHM
 * @see #PDE_PASSWORD
 * @see #PDE_SALT
 * @see #PDE_ITERATION
 */
private String decryptEncrypt(final String _password, final boolean _decrypt) {
    String ret = null;
    if (_password != null) {
        try {
            // create PBE parameter set
            final PBEParameterSpec pbeParamSpec = new PBEParameterSpec(ProjectProperties.PDE_SALT,
                    ProjectProperties.PDE_ITERATION);
            final PBEKeySpec pbeKeySpec = new PBEKeySpec(ProjectProperties.PDE_PASSWORD,
                    ProjectProperties.PDE_SALT, ProjectProperties.PDE_ITERATION);
            final SecretKeyFactory keyFac = SecretKeyFactory.getInstance(ProjectProperties.PDE_ALGORITHM);
            final SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

            final Cipher cipher = Cipher.getInstance(pbeKey.getAlgorithm());

            if (_decrypt) {
                cipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
                // decode base64 to get bytes
                final byte[] dec = Base64.decodeBase64(_password.getBytes(ProjectProperties.ENCODING));
                // decrypt
                final byte[] ciphertext = cipher.doFinal(dec);

                ret = new String(ciphertext, ProjectProperties.ENCODING);

            } else {
                cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
                final byte[] pwdText = _password.getBytes(ProjectProperties.ENCODING);
                // encrypt the cleartext
                final byte[] ciphertext = cipher.doFinal(pwdText);

                ret = new String(Base64.encodeBase64(ciphertext), ProjectProperties.ENCODING);
            }
        } catch (final Exception e) {
            throw new Error(e);
        }
    }
    return ret;
}

From source file:com.aimluck.eip.mail.util.ALMailUtils.java

/**
 * ??????? ??PBEWithMD5AndDES//from  w  w w. j  a v a  2s  . co m
 *
 * @param cipherMode
 *          Cipher.ENCRYPT_MODE ???? Cipher.DECRYPT_MODE
 * @param password
 * @param data
 * @return
 */
public static final byte[] cryptPBEWithMD5AndDES(int cipherMode, char[] password, byte[] data) {
    byte[] ciphertext = null;
    PBEKeySpec pbeKeySpec;
    PBEParameterSpec pbeParamSpec;
    SecretKeyFactory keyFac;

    // Salt
    byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
            (byte) 0x99 };

    // Iteration count
    int count = 20;

    // Create PBE parameter set
    pbeParamSpec = new PBEParameterSpec(salt, count);

    pbeKeySpec = new PBEKeySpec(password);
    try {
        keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

        // Create PBE Cipher
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");

        // Initialize PBE Cipher with key and parameters
        pbeCipher.init(cipherMode, pbeKey, pbeParamSpec);

        // Encrypt/Decrypt the cleartext
        ciphertext = pbeCipher.doFinal(data);
    } catch (Exception e) {
        logger.error("ALMailUtils.cryptPBEWithMD5AndDES", e);
        return null;
    }
    return ciphertext;
}

From source file:csh.cryptonite.Cryptonite.java

public static String encrypt(String value, Context context) throws RuntimeException {

    try {/*from  w w w.ja va  2s  .com*/
        final byte[] bytes = value != null ? value.getBytes("utf-8") : new byte[0];
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(jniFullPw().toCharArray()));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, key,
                new PBEParameterSpec(Settings.Secure
                        .getString(context.getContentResolver(), Settings.Secure.ANDROID_ID).getBytes("utf-8"),
                        20));
        return new String(Base64.encode(pbeCipher.doFinal(bytes), Base64.NO_WRAP), "utf-8");

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}