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.report.query.ReportQueryBL.java

private static String dcl(String encryptedText, char[] password) {
    byte[] clearText = { ' ' };
    int count = 20;
    PBEKeySpec pbeKeySpec;/*from w ww  .jav a2 s . c o m*/
    PBEParameterSpec pbeParamSpec;
    SecretKeyFactory keyFac;
    // 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(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

        byte[] ciphertext = Base64.decodeBase64(encryptedText);

        // Decrypt the cleartext
        clearText = pbeCipher.doFinal(ciphertext);
    } catch (Exception e) {
    }
    return new String(clearText);
}

From source file:com.aurel.track.admin.customize.category.filter.execute.ReportQueryBL.java

private static String dcl(String encryptedText, char[] password) {
    byte[] clearText = { ' ' };
    int count = 20;
    PBEKeySpec pbeKeySpec;//from   w ww .j a  v  a  2 s .  com
    PBEParameterSpec pbeParamSpec;
    SecretKeyFactory keyFac;
    // 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(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

        byte[] ciphertext = Base64.decodeBase64(encryptedText);

        //Decrypt the cleartext
        clearText = pbeCipher.doFinal(ciphertext);
    } catch (Exception e) {
        LOGGER.debug(ExceptionUtils.getStackTrace(e), e);
    }
    return new String(clearText);
}

From source file:org.datacleaner.util.convert.EncodedStringConverter.java

@Override
public String toString(String password) {
    if (password == null) {
        return null;
    }//from   w ww  . j  a  va 2  s  . c  om
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORHITM);
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(_secret));
        Cipher pbeCipher = Cipher.getInstance(ALGORHITM);
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(_salt, 20));

        byte[] bytes = pbeCipher.doFinal(password.getBytes());

        bytes = Base64.encodeBase64(bytes, false);
        return new String(bytes, "UTF-8");
    } catch (Exception e) {
        throw new IllegalStateException("Unable to encode password", e);
    }
}

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

public AlgorithmParameterSpec newInstance(Properties props, String prefix) {
    int count;//from  w  w  w.  j a  v a2s  . co m
    String name, value;
    byte[] salt;
    PBEParameterSpec param;

    name = Strings.concat(prefix, PBEParameterSpecFactory.PROP_SALT);
    value = props.getProperty(name);
    if (Strings.isNullOrEmpty(value)) {
        throw new ArgumentNullException(name);
    }

    try {
        salt = Hex.decodeHex(value.toCharArray());
    } catch (DecoderException ex) {
        throw SecurityUtils.toSecurityException(ex);
    }

    name = Strings.concat(prefix, PBEParameterSpecFactory.PROP_ITERATIONCOUNT);
    value = props.getProperty(name);
    if (Strings.isNullOrEmpty(value)) {
        throw new ArgumentNullException(name);
    }
    count = Integer.parseInt(value);

    param = new PBEParameterSpec(salt, count);

    return param;
}

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

public static String Decrypt(String str) {
    // create cryptography object
    SecretKeyFactory keyFactory;/*from   w ww.  j  a  v a  2 s . c o m*/
    try {
        keyFactory = 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 = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    } catch (InvalidKeySpecException e) {
        return "";
    }

    // init cipher
    Cipher pbeCipher;
    try {
        pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    } catch (NoSuchAlgorithmException e) {
        return "";
    } catch (NoSuchPaddingException e) {
        return "";
    }

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

    // decode & return decoded string
    String dec;

    try {
        dec = new String(pbeCipher.doFinal(base64Decode(str)));
    } catch (IllegalBlockSizeException e) {
        return "";
    } catch (BadPaddingException e) {
        return "";
    } catch (IOException e) {
        return "";
    }

    return dec;
}

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

/**
 * Decrypt an encrypted string.//w w  w .j  a  v a 2 s . co m
 * 
 * @param encryptedText
 *            the encrypted text
 * @return text
 * @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 decrypt(final String encryptedText) 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.DECRYPT_MODE, pbeKey, pbeParamSpec);

    final byte[] text = pbeCipher.doFinal(Base64.decodeBase64(encryptedText));
    return new String(text, "UTF-8").intern();
}

From source file:com.nextep.designer.core.services.impl.RepositoryService.java

public RepositoryService() {
    pbeParamSpec = new PBEParameterSpec(salt, iterations);
    pbeKeySpec = new PBEKeySpec(encryptionPassword.toCharArray());
    try {/*w w w. j a  v a2  s .  c  om*/
        SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
        key = factory.generateSecret(pbeKeySpec);
    } catch (NoSuchAlgorithmException e) {
        LOGGER.error(CoreMessages.getString("repositoryService.encryptionNotFound"), e); //$NON-NLS-1$
    } catch (InvalidKeySpecException e) {
        LOGGER.error(CoreMessages.getString("repositoryService.encryptionKeyFail"), e); //$NON-NLS-1$
    }
}

From source file:org.jumpmind.security.SecurityService.java

protected void initializeCipher(Cipher cipher, int mode) throws Exception {
    AlgorithmParameterSpec paramSpec = Cipher.getMaxAllowedParameterSpec(cipher.getAlgorithm());

    if (paramSpec instanceof PBEParameterSpec
            || (paramSpec == null && cipher.getAlgorithm().startsWith("PBE"))) {
        paramSpec = new PBEParameterSpec(SecurityConstants.SALT, SecurityConstants.ITERATION_COUNT);
        cipher.init(mode, secretKey, paramSpec);
    } else if (paramSpec instanceof IvParameterSpec) {
        paramSpec = new IvParameterSpec(SecurityConstants.SALT);
        cipher.init(mode, secretKey, paramSpec);
    } else {/* w ww. j av  a2  s. c  om*/
        cipher.init(mode, secretKey, (AlgorithmParameterSpec) null);
    }
}

From source file:org.pentaho.platform.engine.security.CipherEncryptionService.java

public void afterPropertiesSet() throws ObjectFactoryException {
    if ((saltString == null) || (algorithm == null) || (encryptionKey == null)) {
        throw new ObjectFactoryException(
                "Required properties not set - need Salt, algorithm and encryption key");
    }//from  w w  w. j ava2s . com
    if (saltString.length() != this.saltLength) {
        // Make sure that the salt length is 8 bytes - the PBEParameterSpec doesn't anything but
        if (saltString.length() < saltLength) {
            saltString = (saltString + "!@#$%^&*").substring(0, saltLength); // postfix bytes to pad it out
        } else if (saltString.length() > saltLength) {
            saltString = saltString.substring(0, saltLength); // Trim off longer than 8-bytes
        }
    }
    byte[] saltBytes = saltString.getBytes();
    paramSpec = new PBEParameterSpec(saltBytes, getIterations());
    PBEKeySpec skeySpec = new PBEKeySpec(getEncryptionKey().toCharArray(), saltBytes, getIterations());
    try {
        secretKey = SecretKeyFactory.getInstance(getAlgorithm()).generateSecret(skeySpec);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new ObjectFactoryException("Encryption requested not available");
    }

}

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

public static String decrypt(String value) throws RunwaySecurityException {
    String result = null;//from w ww. ja v a  2 s.  c  o  m
    SecretKeyFactory keyFactory;

    try {

        keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
        Cipher pbeCipher = Cipher.getInstance(ALGORITHM);
        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        result = new String(pbeCipher.doFinal(base64Decode(value)));

    } 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);
    } catch (IOException e) {
        throw new RunwaySecurityException(e);
    }

    return result;
}