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:org.casbah.provider.KeyHelper.java

public static PrivateKey readKey(String keypass, byte[] keyData) throws CAProviderException {
    try {//  w  w  w .  j  ava2s  .  c o  m
        EncryptedPrivateKeyInfo pkInfo = new EncryptedPrivateKeyInfo(keyData);
        PBEKeySpec keySpec = new PBEKeySpec(keypass.toCharArray());
        SecretKeyFactory pbeKeyFactory = SecretKeyFactory.getInstance(pkInfo.getAlgName());
        PKCS8EncodedKeySpec encodedKeySpec = pkInfo.getKeySpec(pbeKeyFactory.generateSecret(keySpec));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (RSAPrivateCrtKey) keyFactory.generatePrivate(encodedKeySpec);
    } catch (Exception e) {
        throw new CAProviderException("Could not decode private key", e);
    }

}

From source file:ch.helmchen.camlapse.user.control.Encryption.java

public static String decrypt(String aBase64String) throws GeneralSecurityException, IOException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return new String(pbeCipher.doFinal(base64Decode(aBase64String)));
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.util.StringProtector.java

public StringProtector(final String passwd) throws GeneralSecurityException {
    final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    final SecretKey key = keyFactory.generateSecret(new PBEKeySpec(passwd.toCharArray()));
    encryptor = Cipher.getInstance("PBEWithMD5AndDES");
    encryptor.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));

    decryptor = Cipher.getInstance("PBEWithMD5AndDES");
    decryptor.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
}

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

public static String Encrypt(String str) {
    // create cryptography object
    SecretKeyFactory factory;// w  ww  .ja  v a 2s  . co  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:net.bioclipse.encryption.Encrypter.java

public Encrypter(String key) {
    pbeKeySpec = new PBEKeySpec(key.toCharArray());
}

From source file:com.sapienter.jbilling.common.JBCryptoImpl.java

public JBCryptoImpl(String password) throws InvalidKeySpecException {
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray());
    SecretKeyFactory keyFactory = getSecretKeyFactory();
    mySecretKey = keyFactory.generateSecret(spec);
}

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

/**
 * Decrypts the given encrypted password.
 * /*  ww w  .j  a v a 2 s. com*/
 * @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:org.apache.juddi.v3.client.cryptor.DefaultCryptor.java

/**
 * Constructor for DefaultCryptor.//w  ww.  j a va 2s  .c o  m
 */
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);//from  ww  w.j  av a  2s . c  o  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);
}