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:org.openintents.safe.CryptoHelper.java

/**
 * Initialize the class.  Sets the encryption level for the instance
 * and generates the secret key factory.
 *
 * @param Strength// w w w  . j a v  a 2 s  .c om
 */
private void initialize(int Strength) {
    switch (Strength) {
    case EncryptionMedium:
        algorithm = algorithmMedium;
        break;
    case EncryptionStrong:
        algorithm = algorithmStrong;
        break;
    }
    pbeParamSpec = new PBEParameterSpec(salt, count);
    try {
        keyFac = SecretKeyFactory.getInstance(algorithm, "BC");
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "CryptoHelper(): " + e.toString());
    } catch (NoSuchProviderException e) {
        Log.e(TAG, "CryptoHelper(): " + e.toString());
    }
}

From source file:org.hawk.core.security.FileBasedCredentialsStore.java

private String encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException {

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(encryptionKey));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, 20));
    return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
}

From source file:com.asquareb.kaaval.MachineKey.java

/**
 * Method to encrypt a string. Accepts the string to be encrypted and the
 * name of the file to store the key vale which can be used for decryption
 *///from w w w .  j av  a2s.c o m
public static String encrypt(String property, String app) throws IOException, KaavalException {

    InetAddress ip = null;
    String ipAddress = null;
    ObjectOutputStream os = null;
    NetworkInterface macAddress = null;
    byte[] macId = null;
    Cipher pbeCipher = null;
    Random rand = new Random();
    rand.nextBytes(salt);
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password));
        ip = InetAddress.getLocalHost();
        ipAddress = ip.getHostAddress();
        macAddress = NetworkInterface.getByInetAddress(ip);
        macId = macAddress.getHardwareAddress();
        MachineKey mKey = new MachineKey();
        mKey.api = ipAddress;
        mKey.macad = new String(macId);
        mKey.yek = key;
        mKey.tlas = salt;
        mKey.eti = rand.nextInt(1000);
        os = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(app)));
        os.writeObject(mKey);
        os.close();
        pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, mKey.eti));
        return base64Encode(pbeCipher.doFinal(property.getBytes()));
    } catch (IOException e) {
        throw new KaavalException(1, "Error in key file during encryption", e);
    } catch (Exception e) {
        throw new KaavalException(2, "Errors during encryption", e);
    } finally {
        if (os != null)
            os.close();
    }
}

From source file:de.alpharogroup.crypto.simple.SimpleDecryptor.java

/**
 * Initializes the {@link SimpleDecryptor} object.
 *
 * @throws InvalidAlgorithmParameterException
 *             is thrown if initialization of the cypher object fails.
 * @throws NoSuchPaddingException//from  ww  w.j a v a2 s . co m
 *             is thrown if instantiation of the cypher object fails.
 * @throws InvalidKeySpecException
 *             is thrown if generation of the SecretKey object fails.
 * @throws NoSuchAlgorithmException
 *             is thrown if instantiation of the SecretKeyFactory object fails.
 * @throws InvalidKeyException
 *             is thrown if initialization of the cypher object fails.
 */
private void initialize() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        InvalidKeyException, InvalidAlgorithmParameterException {
    if (!isInitialized()) {
        KeySpec keySpec = null;
        if (this.getPrivateKey() != null) {
            keySpec = new PBEKeySpec(this.getPrivateKey().toCharArray());
        }
        if (this.getPrivateKey() == null) {
            keySpec = new PBEKeySpec(CryptConst.PRIVATE_KEY.toCharArray());
        }
        final SecretKeyFactory factory = SecretKeyFactory.getInstance(CryptConst.PBEWITH_MD5AND_DES);
        final SecretKey key = factory.generateSecret(keySpec);
        this.cipher = Cipher.getInstance(key.getAlgorithm());
        final AlgorithmParameterSpec paramSpec = new PBEParameterSpec(CryptConst.SALT,
                CryptConst.ITERATIONCOUNT);
        this.cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
        initialized = true;
    }
}

From source file:de.alpharogroup.crypto.simple.SimpleEncryptor.java

/**
 * Initializes the {@link SimpleEncryptor} object.
 * /*from ww w.ja v  a 2 s  .  c om*/
 * @throws InvalidAlgorithmParameterException
 *             is thrown if initialization of the cypher object fails.
 * @throws NoSuchPaddingException
 *             is thrown if instantiation of the cypher object fails.
 * @throws InvalidKeySpecException
 *             is thrown if generation of the SecretKey object fails.
 * @throws NoSuchAlgorithmException
 *             is thrown if instantiation of the SecretKeyFactory object fails.
 * @throws InvalidKeyException
 *             is thrown if initialization of the cypher object fails.
 */
private void initialize() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        InvalidKeyException, InvalidAlgorithmParameterException {
    if (!isInitialized()) {
        final KeySpec keySpec;
        if (this.getPrivateKey() != null) {
            keySpec = new PBEKeySpec(this.getPrivateKey().toCharArray());
        } else {
            keySpec = new PBEKeySpec(CryptConst.PRIVATE_KEY.toCharArray());
        }
        final SecretKeyFactory factory = SecretKeyFactory.getInstance(CryptConst.PBEWITH_MD5AND_DES);
        final SecretKey key = factory.generateSecret(keySpec);
        this.cipher = Cipher.getInstance(key.getAlgorithm());
        final AlgorithmParameterSpec paramSpec = new PBEParameterSpec(CryptConst.SALT,
                CryptConst.ITERATIONCOUNT);
        this.cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        initialized = true;
    }
}

From source file:org.jasig.portlet.courses.dao.xml.SecureRequestCredentials.java

private static Cipher getCipher(String username, int opmode) {
    final Cipher cipher;
    try {//from   ww w .  j  ava  2s.  c  o  m
        cipher = Cipher.getInstance(PASSWORD_ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        throw new Error("Failed to create Cipher for algorithm '" + PASSWORD_ALGORITHM + "'. "
                + SecureRequestCredentials.class.getSimpleName() + " will not work", e);
    } catch (NoSuchPaddingException e) {
        throw new Error("Failed to create Cipher for algorithm '" + PASSWORD_ALGORITHM + "'. "
                + SecureRequestCredentials.class.getSimpleName() + " will not work", e);
    }

    final byte[] salt = getSalt(username);
    final PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATIONS);
    try {
        cipher.init(opmode, SECRET_KEY, pbeParamSpec);
    } catch (InvalidKeyException e) {
        throw new Error("Failed to init Cipher for SecretKey '" + SECRET_KEY + "'. "
                + SecureRequestCredentials.class.getSimpleName() + " will not work", e);
    } catch (InvalidAlgorithmParameterException e) {
        throw new Error("Failed to init Cipher for PBEParameterSpec '" + pbeParamSpec + "'. "
                + SecureRequestCredentials.class.getSimpleName() + " will not work", e);
    }

    return cipher;
}

From source file:org.hawk.core.security.FileBasedCredentialsStore.java

private String decrypt(String property) throws GeneralSecurityException, IOException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(encryptionKey));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, 20));
    return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8");
}

From source file:org.jets3t.service.security.EncryptionUtil.java

/**
 * Constructs class configured with the provided password, and set up to use the encryption
 * method specified.//from  www  .j ava2s.  c  om
 *
 * @param encryptionKey
 *        the password to use for encryption/decryption.
 * @param algorithm
 *        the Java name of an encryption algorithm to use, eg PBEWithMD5AndDES
 * @param version
 *        the version of encyption to use, for historic and future compatibility.
 *        Unless using an historic version, this should always be
 *        {@link #DEFAULT_VERSION}
 *
 * @throws InvalidKeyException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchPaddingException
 * @throws InvalidKeySpecException
 */
public EncryptionUtil(String encryptionKey, String algorithm, String version)
        throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
    this.algorithm = algorithm;
    this.version = version;
    if (log.isDebugEnabled()) {
        log.debug("Cryptographic properties: algorithm=" + this.algorithm + ", version=" + this.version);
    }

    if (!DEFAULT_VERSION.equals(version)) {
        throw new RuntimeException("Unrecognised crypto version setting: " + version);
    }

    PBEKeySpec keyspec = new PBEKeySpec(encryptionKey.toCharArray(), salt, ITERATION_COUNT, 32);
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
    key = skf.generateSecret(keyspec);
    algParamSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
}

From source file:org.apache.nifi.processors.standard.util.crypto.OpenSSLPKCS5CipherProvider.java

protected Cipher getInitializedCipher(EncryptionMethod encryptionMethod, String password, byte[] salt,
        boolean encryptMode) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
    if (encryptionMethod == null) {
        throw new IllegalArgumentException("The encryption method must be specified");
    }/*from   w w  w.  jav  a2 s.  c o m*/

    if (StringUtils.isEmpty(password)) {
        throw new IllegalArgumentException("Encryption with an empty password is not supported");
    }

    validateSalt(encryptionMethod, salt);

    String algorithm = encryptionMethod.getAlgorithm();
    String provider = encryptionMethod.getProvider();

    // Initialize secret key from password
    final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
    final SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm, provider);
    SecretKey tempKey = factory.generateSecret(pbeKeySpec);

    final PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, getIterationCount());
    Cipher cipher = Cipher.getInstance(algorithm, provider);
    cipher.init(encryptMode ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, tempKey, parameterSpec);
    return cipher;
}

From source file:org.artifactory.security.CryptoHelper.java

public static String encryptSymmetric(String plainText, SecretKey pbeKey) {
    try {//from  w  w  w. java2  s  . c  om
        Cipher pbeCipher = Cipher.getInstance(SYM_ALGORITHM);
        PBEParameterSpec pbeParamSpec = new PBEParameterSpec(PBE_SALT, PBE_ITERATION_COUNT);
        pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
        byte[] encrypted = pbeCipher.doFinal(stringToBytes(plainText));
        return getEncryptionPrefix() + toBase64(encrypted);
    } catch (Exception e) {
        throw new UnsupportedOperationException(e);
    }
}