Example usage for javax.crypto SecretKeyFactory getInstance

List of usage examples for javax.crypto SecretKeyFactory getInstance

Introduction

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

Prototype

public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a SecretKeyFactory object that converts secret keys of the specified algorithm.

Usage

From source file:com.gvmax.common.util.Enc.java

public Enc(String password, int keyLength) {
    if (password == null || password.trim().equals("")) {
        enabled = false;/*from  w w  w.j a  v  a 2 s  .com*/
    } else {
        enabled = true;
        try {
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
            KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 1024, keyLength);
            key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
            c = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (Exception e) {
            logger.error(e);
        }
    }
}

From source file:org.acegisecurity.util.EncryptionUtils.java

private static byte[] cipher(String key, byte[] passedBytes, int cipherMode) throws EncryptionException {
    try {//ww  w.  ja  va 2s.  c o m
        final KeySpec keySpec = new DESedeKeySpec(stringToByteArray(key));
        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        final SecretKey secretKey = keyFactory.generateSecret(keySpec);
        cipher.init(cipherMode, secretKey);
        return cipher.doFinal(passedBytes);
    } catch (final Exception e) {
        throw new EncryptionException(e.getMessage(), e);
    }
}

From source file:org.toobsframework.util.Crypto.java

protected Crypto() {
    try {//from www .  j  av  a  2s.  c o m
        DESKeySpec keySpec = new DESKeySpec(keyData);
        SecretKey key = SecretKeyFactory.getInstance(ALGORITHM).generateSecret(keySpec);
        initEncryptCipher(key);
        initDecryptCipher(key);
    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.billing.ng.crypto.profile.hash.PBEKeySpecProfile.java

public SecretKeyFactory getSecretKeyFactory() {
    try {/*  w ww  .  jav  a 2 s  .  co m*/
        return SecretKeyFactory.getInstance(getAlgorithm());
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Secret Key algorithm not supported by the JCE provider.");
    }
}

From source file:com.glaf.core.security.SecurityUtils.java

/**
 * DES/*from w ww  .j  av a2s.c  o m*/
 * 
 * @param data
 *            
 * @param key
 *            ???8?
 * @return ?
 */
public static String decode(String key, String data) {
    if (data == null) {
        return null;
    }
    try {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // key??8?
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
        AlgorithmParameterSpec paramSpec = iv;
        cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
        return new String(cipher.doFinal(hex2byte(data.getBytes())));
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new SecurityException(ex);
    }
}

From source file:com.glaf.core.security.DefaultEncryptor.java

public DefaultEncryptor() throws NoSuchAlgorithmException, InvalidKeySpecException {
    pbeParamSpec = new PBEParameterSpec(salt, count);
    pbeKeySpec = new PBEKeySpec("saagar".toCharArray());
    keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    pbeKey = keyFac.generateSecret(pbeKeySpec);
}

From source file:com.thoughtworks.go.domain.AccessTokenTest.java

@Test
void hashToken_shouldHashTheProvidedString() throws Exception {
    AccessToken.AccessTokenWithDisplayValue token = AccessToken.create(null, null, null, new TestingClock());

    SecretKey key = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
            .generateSecret(new PBEKeySpec(token.getDisplayValue().substring(8).toCharArray(),
                    token.getSaltValue().getBytes(StandardCharsets.UTF_8), 4096, 256));

    assertThat(token.getValue()).isEqualTo(Hex.encodeHexString(key.getEncoded()));
}

From source file:Util.PassGen.java

private String hash(String password, byte[] salt) throws Exception {
    if (password == null || password.length() == 0) {
        throw new IllegalArgumentException("Empty passwords are not supported.");
    }/*from  w w  w  . java  2 s . com*/
    SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    SecretKey key = f.generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLen));
    return Base64.encodeBase64String(key.getEncoded());
}

From source file:adminpassword.AESDemo.java

@SuppressWarnings("static-access")
public String decrypt(String encryptedText) throws Exception {

    byte[] saltBytes = salt.getBytes("UTF-8");
    byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText);

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, pswdIterations, keySize);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    // Decrypt the message
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));

    byte[] decryptedTextBytes = null;
    try {/* w  w  w . j  a v a 2  s . com*/
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return new String(decryptedTextBytes);
}

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

/**
 * Encrypt a string.//from  ww  w.ja  v  a 2 s .  c o m
 * 
 * @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);
}