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:com.aurel.track.admin.customize.category.filter.execute.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  w w .  jav  a  2 s. c  om*/
        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.debug(ExceptionUtils.getStackTrace(e), e);
    }
    return new String(Base64.encodeBase64(ciphertext));
}

From source file:offstage.crypt.PBECrypt.java

byte[] crypt(byte[] cleartext, char[] password, int cipherMode)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    // Create PBE parameter set
    pbeParamSpec = new PBEParameterSpec(salt, count);

    // Prompt user for encryption password.
    // Collect user password as char array (using the
    // "readPasswd" method from above), and convert
    // it into a SecretKey object, using a PBE key
    // factory./*from w  w  w  .  j a v a  2 s  . co  m*/
    pbeKeySpec = new PBEKeySpec(password);
    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 the cleartext
    byte[] ciphertext = pbeCipher.doFinal(cleartext);

    return ciphertext;
}

From source file:net.sf.jftp.config.Crypto.java

public static String Encrypt(String str) {
    // create cryptography object
    SecretKeyFactory factory;//w  w  w . j a  v a2s. c o m
    try {
        factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    } catch (NoSuchAlgorithmException e) {
        // We could try another algorithm, but it is highly unlikely that this would be the case
        return "";
    }

    // init key
    SecretKey key;

    try {
        key = factory.generateSecret(new PBEKeySpec(PASSWORD));
    } catch (InvalidKeySpecException e) {
        // The password is hard coded - this exception can't be the case
        return "";
    }

    // init cipher
    Cipher pbeCipher;
    try {
        pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    } catch (NoSuchPaddingException e) {
        // We could try another algorithm, but it is highly unlikely that this would be the case
        return "";
    } catch (NoSuchAlgorithmException e) {
        // We could try another algorithm, but it is highly unlikely that this would be the case
        return "";
    }

    try {
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    } catch (InvalidKeyException e) {
        return "";
    } catch (InvalidAlgorithmParameterException e) {
        return "";
    }

    // encode & return encoded string
    try {
        return base64Encode(pbeCipher.doFinal(str.getBytes()));
    } catch (IllegalBlockSizeException e) {
        return "";
    } catch (BadPaddingException e) {
        return "";
    }
}

From source file:org.talend.daikon.security.CryptoHelper.java

/**
 * @param passPhrase the pass phrase used to encrypt and decrypt strings.
 *///  ww  w  .  java  2 s  .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: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();
    }/* w w  w.  java 2 s . c o 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);
    }
}

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

private void initDesEncrypter(final String passPhrase) {
    try {// w w w.j a v  a 2  s. co  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 (GeneralSecurityException e) {
        LOGGER.trace("DesEncrypter.creation failed", e);
        throw new ApplicationException("DesEncrypter creation failed", e);
    }
}

From source file:org.eclipse.che.ide.ext.datasource.server.EncryptTextService.java

public String encrypt(final String textToEncrypt) throws Exception {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(getMasterPassword()));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    String encryptedText = Base64.encodeBase64String(pbeCipher.doFinal(textToEncrypt.getBytes("UTF-8")));
    return encryptedText;
}

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 {/*from ww w .  j  a v  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:org.datacleaner.util.convert.EncodedStringConverter.java

@Override
public String fromString(Class<?> type, String encodedPassword) {
    if (encodedPassword == null) {
        return null;
    }/* w ww  . j av  a2  s .c  o  m*/
    try {
        SecretKeyFactory instance = SecretKeyFactory.getInstance(ALGORHITM);
        SecretKey key = instance.generateSecret(new PBEKeySpec(_secret));
        Cipher cipher = Cipher.getInstance(ALGORHITM);
        cipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(_salt, 20));

        byte[] bytes = encodedPassword.getBytes("UTF-8");

        bytes = cipher.doFinal(Base64.decodeBase64(bytes));
        return new String(bytes);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to decode password", e);
    }
}

From source file:org.runway.utils.StringEncryptDecryptUtil.java

public static String encrypt(String property) throws RunwaySecurityException {
    String result = null;/*from www  .  j a v a 2s  .  co  m*/
    SecretKeyFactory keyFactory;
    try {
        keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
        Cipher pbeCipher = Cipher.getInstance(ALGORITHM);
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        result = base64Encode(pbeCipher.doFinal(property.getBytes()));
    } catch (NoSuchAlgorithmException e) {
        throw new RunwaySecurityException(e);
    } catch (InvalidKeySpecException e) {
        throw new RunwaySecurityException(e);
    } catch (NoSuchPaddingException e) {
        throw new RunwaySecurityException(e);
    } catch (InvalidKeyException e) {
        throw new RunwaySecurityException(e);
    } catch (InvalidAlgorithmParameterException e) {
        throw new RunwaySecurityException(e);
    } catch (IllegalBlockSizeException e) {
        throw new RunwaySecurityException(e);
    } catch (BadPaddingException e) {
        throw new RunwaySecurityException(e);
    }

    return result;
}