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:DesEncrypter.java

private DesEncrypter(String passPhrase) {
    try {/* w  ww .  ja va 2s  . c o  m*/
        // Create the key
        KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        ecipher = Cipher.getInstance(key.getAlgorithm());
        dcipher = Cipher.getInstance(key.getAlgorithm());

        // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

        // Create the ciphers
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
    } catch (java.security.InvalidAlgorithmParameterException e) {
    } catch (java.security.spec.InvalidKeySpecException e) {
    } catch (javax.crypto.NoSuchPaddingException e) {
    } catch (java.security.NoSuchAlgorithmException e) {
    } catch (java.security.InvalidKeyException e) {
    }
}

From source file:es.jamisoft.comun.utils.cipher.CifradoPBE3DES.java

License:asdf

public CifradoPBE3DES(char secret[], byte salt[], int count) {
    secretKey = generatePBKey(secret);//from   w w w  . j a v  a  2  s.c  o m

    try {
        pbeCipher = Cipher.getInstance("PBEWithMD5AndTripleDES");
        paramSpec = new PBEParameterSpec(salt, count);
    } catch (Exception e) {
        System.out.println("Error en la inicializacion de la clase de cifrado:" + e);
        e.printStackTrace();
    }
}

From source file:org.apache.juddi.v3.client.cryptor.DefaultCryptor.java

/**
 * Constructor for DefaultCryptor./*w  w w .  j av  a2 s .c om*/
 */
public DefaultCryptor() throws NoSuchAlgorithmException, InvalidKeySpecException {
    // Create PBE parameter set
    pbeParamSpec = new PBEParameterSpec(salt, count);
    pbeKeySpec = new PBEKeySpec("saagar".toCharArray());
    keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    pbeKey = keyFac.generateSecret(pbeKeySpec);
}

From source file:energy.usef.environment.tool.security.VaultService.java

private static String computeMaskedPassword(String keystorePassword) throws Exception {
    // Create the PBE secret key
    SecretKeyFactory factory = SecretKeyFactory.getInstance(ToolConfig.VAULT_ENC_ALGORITHM);

    char[] password = "somearbitrarycrazystringthatdoesnotmatter".toCharArray();
    PBEParameterSpec cipherSpec = new PBEParameterSpec(ToolConfig.VAULT_SALT.getBytes(),
            ToolConfig.VAULT_ITERATION);
    PBEKeySpec keySpec = new PBEKeySpec(password);
    SecretKey cipherKey = factory.generateSecret(keySpec);

    String maskedPass = PBEUtils.encode64(keystorePassword.getBytes(), ToolConfig.VAULT_ENC_ALGORITHM,
            cipherKey, cipherSpec);/*w  ww. ja v  a2 s  .  co  m*/

    return PicketBoxSecurityVault.PASS_MASK_PREFIX + maskedPass;
}

From source file:com.glaf.core.security.DefaultEncryptor.java

public DefaultEncryptor() throws NoSuchAlgorithmException, InvalidKeySpecException {
    pbeParamSpec = new PBEParameterSpec(salt, count);
    pbeKeySpec = new PBEKeySpec("saagar".toCharArray());
    keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    pbeKey = keyFac.generateSecret(pbeKeySpec);
}

From source file:org.kitodo.data.encryption.DesEncrypter.java

private void initialise(String passPhrase) {
    int iterationCount = 19;

    KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), defaultSalt, iterationCount);

    try {/*from  ww  w .jav  a 2s.c  o  m*/
        SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        encryptionCipher = Cipher.getInstance(secretKey.getAlgorithm());
        decryptionCipher = Cipher.getInstance(secretKey.getAlgorithm());
        AlgorithmParameterSpec algorithmParameterSpec = new PBEParameterSpec(defaultSalt, iterationCount);
        encryptionCipher.init(Cipher.ENCRYPT_MODE, secretKey, algorithmParameterSpec);
        decryptionCipher.init(Cipher.DECRYPT_MODE, secretKey, algorithmParameterSpec);
    } catch (InvalidKeySpecException e) {
        logger.info("Catched InvalidKeySpecException with message: " + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        logger.info("Catched NoSuchAlgorithmException with message: " + e.getMessage());
    } catch (NoSuchPaddingException e) {
        logger.info("Catched NoSuchPaddingException with message: " + e.getMessage());
    } catch (InvalidAlgorithmParameterException e) {
        logger.info("Catched InvalidAlgorithmParameterException with message: " + e.getMessage());
    } catch (InvalidKeyException e) {
        logger.info("Catched InvalidKeyException with message: " + e.getMessage());
    }
}

From source file:com.almende.util.EncryptionUtil.java

/**
 * Encrypt a string./*from  w  ww  . ja v a  2  s  . c  o m*/
 * 
 * @param text
 *            the text
 * @return encryptedText
 * @throws InvalidKeyException
 *             the invalid key exception
 * @throws InvalidAlgorithmParameterException
 *             the invalid algorithm parameter exception
 * @throws NoSuchAlgorithmException
 *             the no such algorithm exception
 * @throws InvalidKeySpecException
 *             the invalid key spec exception
 * @throws NoSuchPaddingException
 *             the no such padding exception
 * @throws IllegalBlockSizeException
 *             the illegal block size exception
 * @throws BadPaddingException
 *             the bad padding exception
 * @throws UnsupportedEncodingException
 *             the unsupported encoding exception
 */
public static String encrypt(final String text) throws InvalidKeyException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException,
        BadPaddingException, UnsupportedEncodingException {
    final PBEParameterSpec pbeParamSpec = new PBEParameterSpec(S, C);
    final PBEKeySpec pbeKeySpec = new PBEKeySpec(P);
    final SecretKeyFactory keyFac = SecretKeyFactory.getInstance(ENC);
    final SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

    final Cipher pbeCipher = Cipher.getInstance(ENC);
    pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

    final byte[] encryptedText = pbeCipher.doFinal(text.getBytes("UTF-8"));
    return Base64.encodeBase64String(encryptedText);
}

From source file:org.j2free.security.DESEncrypter.java

/**
 * /* w  w w. j ava2 s.  co  m*/
 * @param passPhrase
 */
public DESEncrypter(String passPhrase) {

    try {
        // Create the key based on the passPhrase
        KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, ITERATION_COUNT);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);

        encoder = Cipher.getInstance(key.getAlgorithm());
        decoder = Cipher.getInstance(key.getAlgorithm());

        // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATION_COUNT);

        // Create the ciphers
        encoder.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        decoder.init(Cipher.DECRYPT_MODE, key, paramSpec);

    } catch (Exception e) {
        throw LaunderThrowable.launderThrowable(e);
    }
}

From source file:org.opentravel.pubs.config.PasswordHelper.java

/**
 * Decrypts the given encrypted password.
 * // w ww  . j ava 2s. co  m
 * @param encryptedPassword  the encrypted password to decrypt
 * @return
 */
public static String decrypt(String encryptedPassword) {
    try {
        String encryptionPassword = ConfigSettingsFactory.getConfig().getEncryptionPassword();
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM);
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(encryptionPassword.toCharArray()));
        Cipher pbeCipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);

        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        return new String(pbeCipher.doFinal(new Base64().decode(encryptedPassword)), "UTF-8");

    } catch (GeneralSecurityException | IOException e) {
        throw new RuntimeException("Error during password decryption.", e);
    }
}

From source file:com.aurel.track.report.query.ReportQueryBL.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.  java2s. 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.encodeBase64(ciphertext));
}