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:Main.java

public static Key getDESKey(byte[] key)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    DESKeySpec des = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_DES);
    return keyFactory.generateSecret(des);
}

From source file:Main.java

private static SecretKey getKeyFromPassphrase(String passphrase, byte[] salt, int iterations)
        throws GeneralSecurityException {
    PBEKeySpec keyspec = new PBEKeySpec(passphrase.toCharArray(), salt, iterations);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA1AND128BITAES-CBC-BC");
    return skf.generateSecret(keyspec);
}

From source file:MainClass.java

private static byte[] passwordEncrypt(char[] password, byte[] plaintext) throws Exception {
    int MD5_ITERATIONS = 1000;
    byte[] salt = new byte[8];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);//from w w w.j av a  2 s.  c om

    PBEKeySpec keySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
    SecretKey key = keyFactory.generateSecret(keySpec);
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, MD5_ITERATIONS);
    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

    byte[] ciphertext = cipher.doFinal(plaintext);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(salt);
    baos.write(ciphertext);
    return baos.toByteArray();
}

From source file:Main.java

public static Key updGenerateKey(String key) {
    try {//from   ww w .  j av a 2  s  . co m
        DESedeKeySpec KeySpec = new DESedeKeySpec(UdpHexDecode(key));
        SecretKeyFactory KeyFactory = SecretKeyFactory.getInstance("DESede");
        Key k = ((KeyFactory.generateSecret(((KeySpec)))));
        return k;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.sds.acube.ndisc.xnapi.XNApiDesCipher.java

/**
 *   ?<br>/*from w  w  w  .  java2 s . c o m*/
 * 3DES ?? 24bit KEY ?<br>
 * DES ??  16bit KEY ?.<br>
 * 
 * @return 
 */
private static Key getKey() {
    String key = "x0134-ad17s658601j56-q75k2we0des-key".substring(0, 24); // ??   .
    try {
        DESedeKeySpec desKeySpec = new DESedeKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        return keyFactory.generateSecret(desKeySpec);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:rs.htec.cms.cms_bulima.helper.Password.java

private static String hash(String password, byte[] salt) throws Exception {
    if (password == null || password.length() == 0) {
        throw new IllegalArgumentException("Password must have at least one character");
    }/*from w w  w  . ja  va  2s. c  o  m*/
    SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    SecretKey key = f.generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLen));
    return Base64.encodeBase64String(key.getEncoded());
}

From source file:Main.java

private static SecretKey getKeyFromPassphrase(String passphrase, byte[] salt) throws GeneralSecurityException {
    PBEKeySpec keyspec = new PBEKeySpec(passphrase.toCharArray(), salt, 100);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA1AND128BITAES-CBC-BC");
    return skf.generateSecret(keyspec);
}

From source file:Main.java

private static Cipher genCipher(char[] pass, byte[] salt, String factory, int iterations, int mode)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException {
    PBEKeySpec keySpec = new PBEKeySpec(pass, salt, iterations, 128);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(factory);
    SecretKey key = keyFactory.generateSecret(keySpec);
    AlgorithmParameterSpec spec = new PBEParameterSpec(salt, iterations);
    Cipher cipher = Cipher.getInstance(factory);
    cipher.init(mode, key, spec);//from   www  .  j  ava  2 s  . c  o m
    return cipher;
}

From source file:Main.java

public static byte[] TDesDec(byte[] key, byte[] in) throws Exception {

    Key deskey = null;//w w  w .  ja  v  a2  s . c  o  m
    byte[] tdesKey = new byte[24];
    if (key.length % 8 != 0)
        return null;

    if (key.length == 8) {
        System.arraycopy(key, 0, tdesKey, 0, 8);
        System.arraycopy(key, 0, tdesKey, 8, 8);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    if (key.length == 16) {
        System.arraycopy(key, 0, tdesKey, 0, 16);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    DESedeKeySpec spec = new DESedeKeySpec(tdesKey);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding");

    cipher.init(Cipher.DECRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(in);

    return bOut;
}

From source file:Main.java

public static byte[] TDesEnc(byte[] key, byte[] in) throws Exception {

    Key deskey = null;// w w w.j a va2s.  co  m
    byte[] tdesKey = new byte[24];
    if (key.length % 8 != 0)
        return null;

    if (key.length == 8) {
        System.arraycopy(key, 0, tdesKey, 0, 8);
        System.arraycopy(key, 0, tdesKey, 8, 8);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    if (key.length == 16) {
        System.arraycopy(key, 0, tdesKey, 0, 16);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    DESedeKeySpec spec = new DESedeKeySpec(tdesKey);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding");

    cipher.init(Cipher.ENCRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(in);

    return bOut;
}