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.cherong.mock.common.base.util.EncryptionUtil.java

/**
 * DES bytKey8?//from   ww  w  . ja  v a 2  s .com
 * 
 * @param bytE
 * @param bytKey
 * @return
 * @throws Exception
 */
public static byte[] decryptByDES(byte[] bytE, byte[] bytKey) throws Exception {
    DESKeySpec desKS = new DESKeySpec(bytKey);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey sk = skf.generateSecret(desKS);
    Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM);
    cip.init(Cipher.DECRYPT_MODE, sk);
    return cip.doFinal(bytE);
}

From source file:org.suren.autotest.web.framework.util.Encryptor.java

/** 
 * ?//  w ww. j ava  2 s  .  c om
 *  
 * @param desKey 
 * @param algorithm ??? {@link #DES}? {@link #DESEDE}?{@link #AES}?
 *            {@link #BLOWFISH}?{@link #RC2}? {@link #RC4}
 * @return 
 */
public static SecretKey getSecretKey(byte[] desKey, String algorithm) {
    if (ALG_DES.equalsIgnoreCase(algorithm)) {
        try {
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
            DESKeySpec dks = new DESKeySpec(desKey);
            return keyFactory.generateSecret(dks);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getCause());
        } catch (Exception e) {
            throw new IllegalArgumentException("?", e);
        }
    }

    return new SecretKeySpec(desKey, algorithm);
}

From source file:io.github.goadinggoat.BungeeQuarantine.Commands.AcceptRules.java

private static String hash(String password, byte[] salt) {

    SecretKeyFactory f;
    SecretKey key;/*w  w w  .  j av  a  2s.c om*/
    try {
        f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");

        key = f.generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLen));
        return Base64.encodeBase64String(key.getEncoded());
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

/**
 * deskey//from   w w  w  .  j  a  v  a2 s  . co  m
 * 
 * @param content
 * @param key
 * @return
 * @throws Exception
 */
public static byte[] encryptByDES(String content, String key) {
    try {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        SecretKey sk = skf.generateSecret(dks);
        Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM);
        cip.init(Cipher.ENCRYPT_MODE, sk);
        return cip.doFinal(content.getBytes());
    } catch (Exception e) {
        LOGGER.error("{}", e);
        return null;
    }

}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

/**
 * des//  w  ww  .j  ava 2s  .  co m
 * 
 * @param content
 * @param key
 * @return
 * @throws Exception
 */
public static String decryptByDES(byte[] content, String key) {
    try {
        DESKeySpec desKS = new DESKeySpec(key.getBytes());
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        SecretKey sk = skf.generateSecret(desKS);
        Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM);
        cip.init(Cipher.DECRYPT_MODE, sk);
        byte[] result = cip.doFinal(content);
        return new String(result);
    } catch (Exception e) {
        LOGGER.error("{}", e);
        return null;
    }
}

From source file:pl.kotcrab.crypto.CryptoUtils.java

/** Generates AES key from provided password and salt. Used algorithm is PBKDF2WithHmacSHA1.
 * @param password password in char array, using {@link CryptoUtils#fillZeros(char[])} is recommend after generating key
 * @param salt salt for this key//from   ww w.  j a  va2 s.c o m
 * @return SecretKeySpec */
public static SecretKeySpec getAESKeyFromPassword(char[] password, byte[] salt) {
    try {
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(password, salt, 65536, 256);
        return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.linagora.linshare.core.utils.SymmetricEnciphermentPBEwithAES.java

private static SecretKey getSecretKey(String pw) throws NoSuchAlgorithmException, InvalidKeySpecException {

    PBEKeySpec key_spec = new PBEKeySpec(pw.toCharArray());
    SecretKeyFactory key_factory = SecretKeyFactory.getInstance(SECRETKEYFACTORY_ALGO);
    SecretKey secret_key = key_factory.generateSecret(key_spec);

    return secret_key;
}

From source file:com.beligum.core.utils.Toolkit.java

public static String hash(String password, String salt) {
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 2048, 160);
    try {/*from w w w .ja  v  a  2s .co  m*/
        SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        byte[] hash = f.generateSecret(spec).getEncoded();
        return new String(Hex.encodeHex(hash));
    } catch (Exception e) {
        return null;
    }

}

From source file:Main.java

/**
 *
 * @throws InvalidKeySpecException// w  ww .  j a v  a  2s  .c om
 * @throws NoSuchAlgorithmException
 */
private static Key deriveKeyPbkdf2(byte[] salt, String password)
        throws InvalidKeySpecException, NoSuchAlgorithmException {

    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
    SecretKeyFactory factory;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1And8bit");
    } else {
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    }

    byte[] keyBytes = factory.generateSecret(keySpec).getEncoded();

    return new SecretKeySpec(keyBytes, "AES");
}

From source file:org.coronastreet.gpxconverter.AccountManager.java

public static String encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
}