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, byte[] salt, int iterationCount) 

Source Link

Document

Constructor that takes a password, salt, iteration count for generating PBEKey of fixed-key-size PBE ciphers.

Usage

From source file:com.rdonasco.security.utils.EncryptionUtil.java

public static String encryptWithPassword(String stringToEncrypt, String password) throws Exception {
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), SALT, COUNT);
    SecretKey pbeKey = getKeyFactory().generateSecret(pbeKeySpec);
    Cipher pbeCipher = Cipher.getInstance(CIPHER_KEY_SPEC);
    pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, PBE_PARAM_SPEC);
    byte[] encryptedBytes = pbeCipher.doFinal(stringToEncrypt.getBytes());
    return Base64.encodeBase64String(encryptedBytes);
}

From source file:net.mobid.codetraq.utils.PasswordProcessor.java

/**
 * Encrypts a text using the <code>passPhrase</code> above and an algorithm supported
 * by your virtual machine implementation. You can change the default algorithm with
 * another algorithm, but please make sure your virtual machine supports it.
 * @param valueToEncrypt - text to encrypt
 * @return an encrypted, Base64 encoded text
 *//*from  w w w. ja va2 s.co m*/
public static String encryptString(String valueToEncrypt) {
    String output = null;
    try {
        KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterations);
        SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterations);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        // begin encrypting...
        byte[] byteToEncrypt = valueToEncrypt.getBytes("UTF8");
        byte[] encrypted = cipher.doFinal(byteToEncrypt);
        output = new Base64().encodeToString(encrypted);
    } catch (Exception ex) {
        Logger.getLogger(PasswordProcessor.class.getName()).log(Level.SEVERE, null, ex);
    }
    return output;
}

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 {/*w  w w.  jav a2  s .  co 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.aspose.showcase.qrcodegen.web.api.util.StringEncryptor.java

public static String decrypt(String encryptedData, String password) throws Exception {

    Security.addProvider(new BouncyCastleProvider());

    byte[] encrypted = Base64.decode(encryptedData);

    Cipher cipher0 = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");

    // Openssl puts SALTED__ then the 8 byte salt at the start of the file.
    // We simply copy it out.
    byte[] salt = new byte[SALT_SIZE];
    System.arraycopy(encrypted, SALT_SIZE, salt, 0, SALT_SIZE);

    SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC");
    cipher0.init(Cipher.DECRYPT_MODE,
            fact.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_SALE_SIZE)));

    // Decrypt the rest of the byte array (after stripping off the salt)
    byte[] decValue = cipher0.doFinal(encrypted, SALT_STRIP_LENGTH, encrypted.length - SALT_STRIP_LENGTH);

    return new String(decValue);
}

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

/**
 * //  w w w. j a  v a2  s.  c o  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.talend.daikon.security.CryptoHelper.java

/**
 * @param passPhrase the pass phrase used to encrypt and decrypt strings.
 *//*from  www . j  ava 2s . c  o  m*/
public CryptoHelper(String passPhrase) {
    try {
        // Create the key
        KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); //$NON-NLS-1$
        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 (Exception e) {
        // do nothing
    }
}

From source file:org.orcid.core.crypto.DesEncrypter.java

private void initDesEncrypter(final String passPhrase) {
    try {/*w w w .  ja va2s . c om*/
        // 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 (GeneralSecurityException e) {
        LOGGER.trace("DesEncrypter.creation failed", e);
        throw new ApplicationException("DesEncrypter creation failed", e);
    }
}

From source file:com.rdonasco.security.utils.EncryptionUtil.java

public static String decryptWithPassword(String encryptedString, String password) throws Exception {
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), SALT, COUNT);
    SecretKey pbeKey = getKeyFactory().generateSecret(pbeKeySpec);
    Cipher pbeCipher = Cipher.getInstance(CIPHER_KEY_SPEC);
    pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, PBE_PARAM_SPEC);
    byte[] decryptedBytes = pbeCipher.doFinal(Base64.decodeBase64(encryptedString));
    return new String(decryptedBytes);
}

From source file:org.kitodo.production.security.password.SecurityPasswordEncoder.java

private void initialize(String passPhrase) {
    int iterationCount = 19;
    KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), defaultSalt, iterationCount);
    try {/*  w  w  w . ja  v a 2  s .c om*/
        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:de.codesourcery.eve.apiclient.utils.PasswordCipherProvider.java

@Override
public Cipher createCipher(boolean decrypt) {

    final char[] password = getPassword();

    if (ArrayUtils.isEmpty(password)) {
        log.warn("createCipher(): No password , returning NULL cipher.");
        return new NullCipher();
    }/*from w ww .  j  av a  2s  . co m*/

    try {

        final int iterations = 20;

        final byte[] salt = new byte[] { (byte) 0xab, (byte) 0xfe, 0x03, 0x47, (byte) 0xde, (byte) 0x99,
                (byte) 0xff, 0x1c };

        final PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iterations);

        final PBEKeySpec spec = new PBEKeySpec(password, salt, iterations);
        final SecretKeyFactory fac = SecretKeyFactory.getInstance("PBEWithMD5andDES");
        final SecretKey secretKey;
        try {
            secretKey = fac.generateSecret(spec);
        } catch (InvalidKeySpecException e) {
            throw e;
        }

        final Cipher cipher = Cipher.getInstance("PBEWithMD5andDES");
        if (decrypt) {
            cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeSpec);
        } else {
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeSpec);
        }
        return cipher;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}