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:club.jmint.crossing.specs.Security.java

public static String desEncrypt(String data, String key) throws CrossException {
    String ret = null;//from   w  ww .j a  va 2s.  co m
    try {
        DESKeySpec desKey = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);

        Cipher cipher = Cipher.getInstance(CIPHER_DES_ALGORITHM);
        SecureRandom random = new SecureRandom();
        cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
        byte[] results = cipher.doFinal(data.getBytes("UTF-8"));
        ret = Base64.encodeBase64String(results);
    } catch (Exception e) {
        CrossLog.printStackTrace(e);
        throw new CrossException(ErrorCode.COMMON_ERR_ENCRYPTION.getCode(),
                ErrorCode.COMMON_ERR_ENCRYPTION.getInfo());
    }
    return ret;
}

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

/**
 * Encrypt a string./*from   w ww .j a  va  2 s .  c om*/
 * 
 * @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.almende.util.EncryptionUtil.java

/**
 * Decrypt an encrypted string./*from   w w w.j  av  a2 s  .c om*/
 * 
 * @param encryptedText
 *            the encrypted text
 * @return text
 * @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 decrypt(final String encryptedText) 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.DECRYPT_MODE, pbeKey, pbeParamSpec);

    final byte[] text = pbeCipher.doFinal(Base64.decodeBase64(encryptedText));
    return new String(text, "UTF-8").intern();
}

From source file:com.intera.roostrap.util.EncryptionUtil.java

private static void encryptOrDecrypt(String encryptionKey, int mode, InputStream is, OutputStream os)
        throws InvalidKeyException, IOException {
    DESKeySpec keySpec = new DESKeySpec(toBytes(encryptionKey));

    SecretKey key = null;// w w  w.  j  a v  a  2s .c om
    Cipher cipher = null;
    try {
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        key = secretKeyFactory.generateSecret(keySpec);
        cipher = Cipher.getInstance("DES");

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    if (mode == Cipher.ENCRYPT_MODE) {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        doCopy(cis, os);
    } else if (mode == Cipher.DECRYPT_MODE) {
        cipher.init(Cipher.DECRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        doCopy(is, cos);
    }
}

From source file:com.aqnote.shared.cryptology.symmetric.DES.java

private static void generateCipher(String rawKey) {
    try {/*from  ww  w  .  ja v  a2  s  .co  m*/
        DESKeySpec dks = new DESKeySpec(rawKey.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey deskey = keyFactory.generateSecret(dks);
        encodeCipher = Cipher.getInstance(ALGORITHM);
        encodeCipher.init(Cipher.ENCRYPT_MODE, deskey);
        decodeCipher = Cipher.getInstance(ALGORITHM);
        decodeCipher.init(Cipher.DECRYPT_MODE, deskey);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.wms.studio.security.utils.Digests.java

public static byte[] desEncrypt(String pwd, byte[] key) throws Exception {
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??/* ww  w .  j  a  v a2  s .c  o m*/
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
    return cipher.doFinal(pwd.getBytes());
}

From source file:com.wms.studio.security.utils.Digests.java

public static byte[] desDecrypt(byte[] pwd, byte[] key) throws Exception {
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??//from  www .  ja v a 2  s  .  c o  m
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.DECRYPT_MODE, securekey, random);
    return cipher.doFinal(pwd);
}

From source file:com.networknt.utility.HashUtil.java

public static String generateStrongPasswordHash(String password)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    int iterations = 1000;
    char[] chars = password.toCharArray();
    byte[] salt = getSalt().getBytes(UTF_8);

    PBEKeySpec spec = new PBEKeySpec(chars, salt, iterations, 64 * 8);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] hash = skf.generateSecret(spec).getEncoded();
    return iterations + ":" + toHex(salt) + ":" + toHex(hash);
}

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 {// 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.error(ExceptionUtils.getStackTrace(e));
    }
    return new String(Base64.encodeBase64(ciphertext));
}

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

/**
 * //w  w w  .  j  ava2  s . c om
 * @param src ??
 * @param key 8?
 * @return     ??
 * @throws Exception
 */
public static byte[] encrypt(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.ENCRYPT_MODE, securekey, sr);
    // ??
    // ??
    return cipher.doFinal(src);
}