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.aurel.track.admin.customize.category.filter.execute.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 {/*from   w  ww  .  j  a v a 2 s. c om*/
        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.debug(ExceptionUtils.getStackTrace(e), e);
    }
    return new String(Base64.encodeBase64(ciphertext));
}

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.sapienter.jbilling.common.JBCryptoImpl.java

private static SecretKeyFactory getSecretKeyFactory() {
    if (ourKeyFactory == null) {
        try {/*  ww w.  j a  v  a  2 s  . co m*/
            ourKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("Algorithm is not supported: " + ALGORITHM, e);
        }
    }
    return ourKeyFactory;
}

From source file:org.tomcatltpa.utils.LTPAUtils.java

private byte[] getSecretKey(String ltpa3DESKey, String ltpaPassword) throws GeneralSecurityException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(ltpaPassword.getBytes());//  w ww .  j  a  v a  2 s  .c o  m
    byte[] hash3DES = new byte[24];
    System.arraycopy(md.digest(), 0, hash3DES, 0, 20);
    Arrays.fill(hash3DES, 20, 24, (byte) 0);
    Cipher cipher = Cipher.getInstance(DES_DECRIPTING_ALGORITHM);
    KeySpec keySpec = new DESedeKeySpec(hash3DES);
    Key secretKey2 = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
    cipher.init(Cipher.DECRYPT_MODE, secretKey2);
    byte[] secret = cipher.doFinal(Base64.decodeBase64(ltpa3DESKey.getBytes()));
    return secret;
}

From source file:de.codesourcery.eve.apiclient.utils.PasswordCipherProvider.java

@Override
public Cipher createCipher(boolean decrypt) {

    final char[] password = getPassword();

    if (ArrayUtils.isEmpty(password)) {
        log.warn("createCipher(): No password , returning NULL cipher.");
        return new NullCipher();
    }//  w w w  .ja va 2 s .  c  o  m

    try {

        final int iterations = 20;

        final byte[] salt = new byte[] { (byte) 0xab, (byte) 0xfe, 0x03, 0x47, (byte) 0xde, (byte) 0x99,
                (byte) 0xff, 0x1c };

        final PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iterations);

        final PBEKeySpec spec = new PBEKeySpec(password, salt, iterations);
        final SecretKeyFactory fac = SecretKeyFactory.getInstance("PBEWithMD5andDES");
        final SecretKey secretKey;
        try {
            secretKey = fac.generateSecret(spec);
        } catch (InvalidKeySpecException e) {
            throw e;
        }

        final Cipher cipher = Cipher.getInstance("PBEWithMD5andDES");
        if (decrypt) {
            cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeSpec);
        } else {
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeSpec);
        }
        return cipher;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.sldeditor.common.property.EncryptedPropertiesApache.java

@Override
public void initialise(String password) {
    PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);
    SecretKeyFactory kf;/*from   w  w w  . jav  a 2 s  . co  m*/
    try {
        kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(password.toCharArray()));
        encrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
        decrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
        encrypter.init(Cipher.ENCRYPT_MODE, k, ps);
        decrypter.init(Cipher.DECRYPT_MODE, k, ps);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Creates an encoded DESedeKeySpec from a SecretKey
 * //from  ww w .java2  s. c  om
 * @param key
 * @return
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
public static byte[] createDESedeKeySpec(SecretKey key)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    DESedeKeySpec keyspec = (DESedeKeySpec) keyfactory.getKeySpec(key, DESedeKeySpec.class);
    return keyspec.getKey();
}

From source file:net.navasoft.madcoin.backend.services.security.Encrypter.java

/**
 * Instantiates a new encrypter./*from ww  w  .ja v a2 s .c o m*/
 * 
 * @param keyString
 *            the key string
 * @param ivString
 *            the iv string
 * @since 5/08/2014, 08:03:33 PM
 */
public Encrypter(String keyString, String ivString) {
    try {
        final MessageDigest md = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = md.digest(Base64.decodeBase64(keyString.getBytes("utf-8")));
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        keySpec = new DESedeKeySpec(keyBytes);

        key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);

        iv = new IvParameterSpec(ivString.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.yes.cart.shoppingcart.support.impl.AbstractCryptedTuplizerImpl.java

/**
 * Default Constructor.//from   w w  w.  j  av  a 2  s .com
 *
 * @param keyRingPassword      key ring password to use.
 * @param secretKeyFactoryName Secret Key Factory Name.
 * @param cipherName           Cipher name.
 */
public AbstractCryptedTuplizerImpl(final String keyRingPassword, final String secretKeyFactoryName,
        final String cipherName) {

    try {
        final DESKeySpec desKeySpec = new DESKeySpec(keyRingPassword.getBytes());

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretKeyFactoryName);
        secretKey = keyFactory.generateSecret(desKeySpec);

        // Create Cipher
        desCipher = Cipher.getInstance(cipherName);
        desCipher.init(Cipher.ENCRYPT_MODE, secretKey);

        // create uncipher
        desUnCipher = Cipher.getInstance(cipherName);
        desUnCipher.init(Cipher.DECRYPT_MODE, secretKey);
    } catch (Exception ike) {
        LOG.error(ike.getMessage(), ike);
        throw new RuntimeException("Unable to load Cipher for CookieTuplizer", ike);
    }

}

From source file:offstage.crypt.PBECrypt.java

byte[] crypt(byte[] cleartext, char[] password, int cipherMode)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    // Create PBE parameter set
    pbeParamSpec = new PBEParameterSpec(salt, count);

    // Prompt user for encryption password.
    // Collect user password as char array (using the
    // "readPasswd" method from above), and convert
    // it into a SecretKey object, using a PBE key
    // factory./*from w  w  w .  java  2 s.c om*/
    pbeKeySpec = new PBEKeySpec(password);
    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(cipherMode, pbeKey, pbeParamSpec);

    // Encrypt the cleartext
    byte[] ciphertext = pbeCipher.doFinal(cleartext);

    return ciphertext;
}