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:be.tutul.naheulcraft.launcher.auth.Auth.java

private Cipher getCipher(int paramInt, String key) throws Exception {
    Random random = new Random(43287234L);
    byte[] salt = new byte[8];
    random.nextBytes(salt);/*from   w  w  w  . j  a  va 2s.c  o m*/
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 5);

    SecretKey pbeKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES")
            .generateSecret(new PBEKeySpec(key.toCharArray()));
    Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
    cipher.init(paramInt, pbeKey, pbeParamSpec);

    return cipher;
}

From source file:org.apache.ranger.plugin.util.PasswordUtils.java

private String encrypt() throws IOException {
    String ret = null;/*ww w  .j  a  va2s  .  c  o  m*/
    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 w ww.  ja  v a 2 s  . c om*/
    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;
}

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

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

    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    keygen.initialize(2048);//www  .ja  v a2  s .c o  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()));

    RSACrypto impl = new RSACrypto();
    impl.setAlgorithm("RSA/ECB/PKCS1Padding");
    impl.setPublicKeyBytes(key.getPublic().getEncoded());
    impl.setPrivateKeyBytes(encryptedKeyInfo.getEncoded(), password);
    return impl;
}

From source file:com.redsqirl.workflow.utils.FileStream.java

private static byte[] encrypt(byte[] plaintext) throws Exception {
    SecretKey key = generateKey();
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 42);
    Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec);
    return cipher.doFinal(plaintext);
}

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

public String decryptText(String textToDecrypt) throws Exception {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(getMasterPassword()));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return new String(pbeCipher.doFinal(Base64.decodeBase64(textToDecrypt)), "UTF-8");
}

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

/**
 * Decrypts 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 valueToDecrypt - text to decrypt
 * @return a plain text//w w  w .j a va 2s .c o m
 */
public static String decryptString(String valueToDecrypt) {
    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.DECRYPT_MODE, secretKey, paramSpec);
        // begin decrypting...
        byte[] encrypted = new Base64().decode(valueToDecrypt);
        byte[] utf8 = cipher.doFinal(encrypted);
        output = new String(utf8, "UTF8");
    } catch (Exception ex) {
        Logger.getLogger(PasswordProcessor.class.getName()).log(Level.SEVERE, null, ex);
    }
    return output;
}

From source file:org.webical.dao.encryption.impl.DesEncryptor.java

/**
 * Creates the DesEncryptor// www .j a  v  a  2s .  c  om
 * @param passPhrase the passphrase to use in encryption and decryption
 * @throws InvalidKeyException
 * @throws InvalidAlgorithmParameterException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchPaddingException
 * @throws InvalidKeySpecException
 */
public DesEncryptor(String passPhrase) throws InvalidKeyException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
    // Create the key
    KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
    SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
    encryptCipher = Cipher.getInstance(key.getAlgorithm());
    decryptCipher = Cipher.getInstance(key.getAlgorithm());

    // Prepare the parameter to the ciphers
    AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

    // Create the ciphers
    encryptCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
    decryptCipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
}

From source file:br.com.vizzatech.cryptocipher.CryptoXCipher.java

/**
 * Gera chave e os parametros para um determinado algoritmo.
 * /*  w  ww . ja  v  a2s  .com*/
 * @param algoritmo
 * @param sal
 * @return {@link SecretKey}
 * @throws NoSuchAlgorithmException
 *             Algorismo no existente
 * @throws InvalidKeySpecException
 * @throws InvalidParameterSpecException
 */
private SecretKey getKey(String algoritmo, String sal)
        throws InvalidKeySpecException, NoSuchAlgorithmException, InvalidParameterSpecException {
    KeySpec keySpec;
    if (algoritmo.indexOf("PBE") != -1) {
        byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x34,
                (byte) 0xE3, (byte) 0x03 };
        int iteracoes = 16;
        keySpec = new PBEKeySpec(sal.toCharArray(), salt, iteracoes);
        paramSpec = new PBEParameterSpec(salt, iteracoes);
        return SecretKeyFactory.getInstance(algoritmo).generateSecret(keySpec);
    }
    keySpec = new SecretKeySpec(sal.getBytes(charset), algoritmo);
    return (SecretKey) keySpec;
}

From source file:com.redsqirl.workflow.utils.FileStream.java

private static byte[] decrypt(byte[] ciphertext) throws Exception {
    SecretKey key = generateKey();
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 42);
    Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParamSpec);
    return cipher.doFinal(ciphertext);
}