Example usage for javax.crypto SecretKeyFactory generateSecret

List of usage examples for javax.crypto SecretKeyFactory generateSecret

Introduction

In this page you can find the example usage for javax.crypto SecretKeyFactory generateSecret.

Prototype

public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException 

Source Link

Document

Generates a SecretKey object from the provided key specification (key material).

Usage

From source file:com.liusoft.dlog4j.upgrade.StringUtils.java

/**
 * //from w ww  .  ja v  a 2 s  . c  o m
 * @param src   ??
 * @param key   8?
 * @return      ??
 * @throws Exception
 */
public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
    //      DES????
    SecureRandom sr = new SecureRandom();
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??
    // SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
    // ??
    // ??
    return cipher.doFinal(src);
}

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 {/*w w  w. j  a  va  2 s. c  o m*/
        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:com.networknt.utility.HashUtil.java

public static boolean validatePassword(char[] originalPassword, String storedPassword)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    String[] parts = storedPassword.split(":");
    int iterations = Integer.parseInt(parts[0]);
    byte[] salt = fromHex(parts[1]);
    byte[] hash = fromHex(parts[2]);

    PBEKeySpec spec = new PBEKeySpec(originalPassword, salt, iterations, hash.length * 8);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] testHash = skf.generateSecret(spec).getEncoded();

    int diff = hash.length ^ testHash.length;
    for (int i = 0; i < hash.length && i < testHash.length; i++) {
        diff |= hash[i] ^ testHash[i];//from  w ww. jav  a  2s  . c om
    }
    return diff == 0;
}

From source file:org.talend.commons.utils.PasswordEncryptUtil.java

private static SecretKey getSecretKey() throws Exception {
    if (key == null) {

        byte rawKeyData[] = rawKey.getBytes();
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //$NON-NLS-1$
        key = keyFactory.generateSecret(dks);
    }//from   w  w w. ja  v a2  s . c  om
    return key;
}

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

public static String encrypt(String property) throws RunwaySecurityException {
    String result = null;// w w  w .  j  av  a2  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:org.casbah.provider.SSLeayEncoder.java

private static byte[] performCipherOperation(byte[] data, byte[] salt, String keypass, int opMode)
        throws GeneralSecurityException, IOException {
    Cipher cipher = Cipher.getInstance(JAVA_ENC_ALGORITHM);
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(JAVA_KEY_TYPE);
    SecretKey secretKey = secretKeyFactory.generateSecret(calculateKeyFromPassKey(keypass.getBytes(), salt));
    IvParameterSpec iv = new IvParameterSpec(salt);
    cipher.init(opMode, secretKey, iv);/*from   w  ww.  ja  v a2s  . c  o  m*/
    return cipher.doFinal(data);
}

From source file:org.talend.commons.utils.PasswordEncryptUtil.java

private static SecretKey getSecretKeyUTF8() throws Exception {
    if (passwordKey == null) {
        byte rawKeyData[] = rawKey.getBytes(CHARSET);
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //$NON-NLS-1$
        passwordKey = keyFactory.generateSecret(dks);
    }/*from  w  w w  . j a  v a 2  s  .  c  om*/
    return passwordKey;
}

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

public static String decrypt(String value) throws RunwaySecurityException {
    String result = null;// www.  j  a v  a2  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;
}

From source file:com.greenline.hrs.admin.util.encrypt.DESUtil.java

/**
 * Description ?//from w ww. j  av  a 2 s  .  c  o  m
 *
 * @param data
 * @param key  byte
 * @return
 * @throws Exception
 */
private static byte[] decrypt(byte[] data, byte[] key) throws GeneralSecurityException {
    // ????
    SecureRandom sr = new SecureRandom();

    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);

    // ?DESKeySpec??SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);

    // Cipher??
    Cipher cipher = Cipher.getInstance(DES);

    // ?Cipher
    cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

    return cipher.doFinal(data);
}

From source file:com.greenline.hrs.admin.util.encrypt.DESUtil.java

/**
 * Description ?/*from   w w w  .  ja v a 2 s  . co  m*/
 *
 * @param data
 * @param key  byte
 * @return
 * @throws Exception
 */
private static byte[] encrypt(byte[] data, byte[] key) throws GeneralSecurityException {
    // ????
    SecureRandom sr = new SecureRandom();

    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);

    // ?DESKeySpec??SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);

    // Cipher??
    Cipher cipher = Cipher.getInstance(DES);

    // ?Cipher
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

    return cipher.doFinal(data);

}