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) 

Source Link

Document

Constructor that takes a password.

Usage

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

/**
 * Encrypt a string./*from  ww w  .  j  a  va2s .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: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  . j av  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.error(ExceptionUtils.getStackTrace(e));
    }
    return new String(Base64.encodeBase64(ciphertext));
}

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

License:asdf

public SecretKey generatePBKey(char secret[]) {
    try {/* w  w  w.  j av a 2s  . c o m*/
        PBEKeySpec keySpec = new PBEKeySpec(secret);
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndTripleDES");

        return keyFac.generateSecret(keySpec);
    } catch (Exception e) {
        System.out.println("Error generando la clave:" + e);
        e.printStackTrace();

        return null;
    }
}

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  ww  .  ja  v a  2s.  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: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.datacleaner.util.convert.EncodedStringConverter.java

@Override
public String fromString(Class<?> type, String encodedPassword) {
    if (encodedPassword == null) {
        return null;
    }/* w ww . ja  v a 2 s  .  com*/
    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;// www. 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.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;
}

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  a2s.c om
    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:org.apache.ranger.plugin.util.PasswordUtils.java

private String encrypt() throws IOException {
    String ret = null;/*from   w w  w. ja  v a  2s.c om*/
    String strToEncrypt = null;
    if (password == null) {
        strToEncrypt = "";
    } else {
        strToEncrypt = password.length() + LEN_SEPARATOR_STR + password;
    }
    try {
        Cipher engine = Cipher.getInstance(CRYPT_ALGO);
        PBEKeySpec keySpec = new PBEKeySpec(encryptKey);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(CRYPT_ALGO);
        SecretKey key = skf.generateSecret(keySpec);
        engine.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, ITERATION_COUNT));
        byte[] encryptedStr = engine.doFinal(strToEncrypt.getBytes());
        ret = new String(Base64.encode(encryptedStr));
    } catch (Throwable t) {
        LOG.error("Unable to encrypt password due to error", t);
        throw new IOException("Unable to encrypt password due to error", t);
    }
    return ret;
}

From source file:cherry.goods.crypto.RSASignatureTest.java

private RSASignature create2(char[] password) throws Exception {

    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    keygen.initialize(2048);//from  ww w.  j a  v  a  2  s .co  m
    KeyPair key = keygen.generateKeyPair();

    String pbeAlgName = "PBEWithMD5AndDES";
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(RandomUtils.nextBytes(8), 20);
    SecretKey pbeKey = SecretKeyFactory.getInstance(pbeAlgName).generateSecret(pbeKeySpec);
    AlgorithmParameters pbeParam = AlgorithmParameters.getInstance(pbeAlgName);
    pbeParam.init(pbeParamSpec);
    Cipher cipher = Cipher.getInstance(pbeAlgName);
    cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParam);
    EncryptedPrivateKeyInfo encryptedKeyInfo = new EncryptedPrivateKeyInfo(pbeParam,
            cipher.doFinal(key.getPrivate().getEncoded()));

    RSASignature impl = new RSASignature();
    impl.setAlgorithm("SHA256withRSA");
    impl.setPublicKeyBytes(key.getPublic().getEncoded());
    impl.setPrivateKeyBytes(encryptedKeyInfo.getEncoded(), password);
    return impl;
}