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:net.thewaffleshop.nimbus.api.EncryptionAPI.java

/**
 * Generate a {@link SecretKey} from a password and salt
 *
 * @param password// ww  w.java 2  s  . c om
 * @param salt
 * @return
 */
public SecretKey createSecretKey(String password, byte[] salt) {
    try {
        PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 1024, 256);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        SecretKey secretKey = factory.generateSecret(keySpec);
        return new SecretKeySpec(secretKey.getEncoded(), "AES");
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}

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);
    }// w  w  w  .  j  a v a 2  s.co m
    return passwordKey;
}

From source file:org.kitodo.production.security.password.SecurityPasswordEncoder.java

private void initialize(String passPhrase) {
    int iterationCount = 19;
    KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), defaultSalt, iterationCount);
    try {/*from w  w  w  . ja va2 s.c o m*/
        SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        encryptionCipher = Cipher.getInstance(secretKey.getAlgorithm());
        decryptionCipher = Cipher.getInstance(secretKey.getAlgorithm());
        AlgorithmParameterSpec algorithmParameterSpec = new PBEParameterSpec(defaultSalt, iterationCount);
        encryptionCipher.init(Cipher.ENCRYPT_MODE, secretKey, algorithmParameterSpec);
        decryptionCipher.init(Cipher.DECRYPT_MODE, secretKey, algorithmParameterSpec);
    } catch (InvalidKeySpecException e) {
        logger.info("Catched InvalidKeySpecException with message: " + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        logger.info("Catched NoSuchAlgorithmException with message: " + e.getMessage());
    } catch (NoSuchPaddingException e) {
        logger.info("Catched NoSuchPaddingException with message: " + e.getMessage());
    } catch (InvalidAlgorithmParameterException e) {
        logger.info("Catched InvalidAlgorithmParameterException with message: " + e.getMessage());
    } catch (InvalidKeyException e) {
        logger.info("Catched InvalidKeyException with message: " + e.getMessage());
    }
}

From source file:org.eclipse.che.ide.ext.datasource.server.EncryptTextService.java

public String encrypt(final String textToEncrypt) throws Exception {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(getMasterPassword()));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    String encryptedText = Base64.encodeBase64String(pbeCipher.doFinal(textToEncrypt.getBytes("UTF-8")));
    return encryptedText;
}

From source file:org.apache.juddi.v3.client.cryptor.TripleDESCrytor.java

/**
 *default constructor//from  w  ww  .j av  a 2 s .  c o  m
 * @throws Exception
 */
public TripleDESCrytor() throws Exception {
    String keyfromfile = CryptorFactory.loadKeyFromFile("TripleDESCrytor");
    if (keyfromfile != null)
        myEncryptionKey = keyfromfile;
    else
        myEncryptionKey = "rioTEBCe/RAHRs6tTyYxDqettnVbZA6z";
    myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
    arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
    ks = new DESedeKeySpec(arrayBytes);
    skf = SecretKeyFactory.getInstance(myEncryptionScheme);
    cipher = Cipher.getInstance(myEncryptionScheme);
    key = skf.generateSecret(ks);
}

From source file:org.kuali.rice.core.impl.encryption.EncryptionServiceImplTest.java

private String generateDESKey() throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    SecretKey desKey = keygen.generateKey();
    SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES");
    DESKeySpec desSpec = (DESKeySpec) desFactory.getKeySpec(desKey, javax.crypto.spec.DESKeySpec.class);
    byte[] rawDesKey = desSpec.getKey();
    return new String(Base64.encodeBase64(rawDesKey));
}

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

public static String encrypt(String property) throws RunwaySecurityException {
    String result = null;/*w  ww.j  a va2s .c om*/
    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.datacleaner.util.convert.EncodedStringConverter.java

@Override
public String fromString(Class<?> type, String encodedPassword) {
    if (encodedPassword == null) {
        return null;
    }/* w  w w.  j  av a 2 s  .  c o m*/
    try {
        SecretKeyFactory instance = SecretKeyFactory.getInstance(ALGORHITM);
        SecretKey key = instance.generateSecret(new PBEKeySpec(_secret));
        Cipher cipher = Cipher.getInstance(ALGORHITM);
        cipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(_salt, 20));

        byte[] bytes = encodedPassword.getBytes("UTF-8");

        bytes = cipher.doFinal(Base64.decodeBase64(bytes));
        return new String(bytes);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to decode password", e);
    }
}

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 va2 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:es.jamisoft.comun.utils.cipher.CifradoPBE3DES.java

License:asdf

public SecretKey generatePBKey(char secret[]) {
    try {/*  ww  w .j av a  2 s .  c  om*/
        PBEKeySpec keySpec = new PBEKeySpec(secret);
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndTripleDES");

        return keyFac.generateSecret(keySpec);
    } catch (Exception e) {
        System.out.println("Error generando la clave:" + e);
        e.printStackTrace();

        return null;
    }
}