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.alegen.datpass.library.crypto.CryptoManager.java

public SecretKey derivateKey(KeyDerivationFunctions function, String password, byte[] salt, int length,
        int iterations) {
    try {/*  ww w  . j a  v  a2  s.  c  om*/
        SecretKeyFactory factory = SecretKeyFactory.getInstance(function.toString());
        KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, length);
        return factory.generateSecret(spec);
    } catch (NoSuchAlgorithmException e) {
        log.error("The required algorithm is not supported by the current JVM.");
        return null;
    } catch (InvalidKeySpecException e) {
        log.error("The key spec is invalid.");
        return null;
    }
}

From source file:org.apache.ranger.plugin.util.PasswordUtils.java

private String encrypt() throws IOException {
    String ret = null;//  w  ww.  j a v  a 2s  .c  om
    String strToEncrypt = null;
    if (password == null) {
        strToEncrypt = "";
    } else {
        strToEncrypt = password.length() + LEN_SEPARATOR_STR + password;
    }
    try {
        Cipher engine = Cipher.getInstance(CRYPT_ALGO);
        PBEKeySpec keySpec = new PBEKeySpec(encryptKey);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(CRYPT_ALGO);
        SecretKey key = skf.generateSecret(keySpec);
        engine.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, ITERATION_COUNT));
        byte[] encryptedStr = engine.doFinal(strToEncrypt.getBytes());
        ret = new String(Base64.encode(encryptedStr));
    } catch (Throwable t) {
        LOG.error("Unable to encrypt password due to error", t);
        throw new IOException("Unable to encrypt password due to error", t);
    }
    return ret;
}

From source file:org.craftercms.social.util.support.security.crypto.SimpleDesCipher.java

public void setKey(byte[] key) throws KeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    DESedeKeySpec keyspec;/*w w w  .  j  av  a  2s.co  m*/
    keyspec = new DESedeKeySpec(key);

    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    skey = keyfactory.generateSecret(keyspec);
}

From source file:com.paxxis.cornerstone.encryption.TripleDESEncryptionHandler.java

public void initialize() {
    if (encryptionKeyFile != null) {
        try {//from   w ww. j  ava2 s . com
            FileReader fr = new FileReader(encryptionKeyFile);
            int cnt;
            char[] cbuf = new char[256];
            StringBuilder builder = new StringBuilder();
            while (-1 != (cnt = fr.read(cbuf, 0, 256))) {
                String s = new String(cbuf, 0, cnt);
                builder.append(s);
            }

            encryptionKey = builder.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    if (encryptionKey == null) {
        throw new RuntimeException("No Encryption Key");
    }

    try {
        byte[] arrayBytes = encryptionKey.getBytes(UNICODE_FORMAT);
        KeySpec ks = new DESedeKeySpec(arrayBytes);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(DESEDE_ENCRYPTION_SCHEME);
        cipher = Cipher.getInstance(DESEDE_ENCRYPTION_SCHEME);
        key = skf.generateSecret(ks);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.openmrs.module.clinicalsummary.io.DownloadSummariesTask.java

/**
 * Method to initialize the cipher object with the correct encryption algorithm.
 *
 * @throws Exception//from ww w.  ja v a 2  s  . c  o  m
 */
protected final void initializeCipher() throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance(TaskConstants.SECRET_KEY_FACTORY);
    KeySpec spec = new PBEKeySpec(password.toCharArray(), password.getBytes(), 1024, 128);
    SecretKey tmp = factory.generateSecret(spec);

    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), TaskConstants.KEY_SPEC);

    if (log.isDebugEnabled())
        log.debug("Encrypting with: " + secret.getAlgorithm());

    cipher = Cipher.getInstance(TaskConstants.CIPHER_CONFIGURATION);
    cipher.init(Cipher.ENCRYPT_MODE, secret);
}

From source file:Main.java

/**
 *
 * Creates a SecretKey from an encoded DESedeKeySpec
 * /*  w  w w  .  j  a  v  a 2 s.  c o  m*/
 * @param rawkey
 * @return
 * @throws InvalidKeyException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
public static SecretKey createSecretKey(byte[] rawkey)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    return keyfactory.generateSecret(keyspec);
}

From source file:org.alfresco.util.encryption.impl.AES256PasswordBasedEncrypter.java

/**
 * Constructor for the class./*from w w w  .j a  va2 s.c o  m*/
 * 
 * @param password The password to use when encrypting data <i>(must not be null, empty or blank)</i>.
 */
public AES256PasswordBasedEncrypter(final char[] password)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException {
    // PRECONDITIONS
    assert password != null && password.length > 0 : "password must not be null or empty";

    // Body
    SecretKeyFactory factory = SecretKeyFactory.getInstance(PASSWORD_ALGORITHM);
    KeySpec spec = new PBEKeySpec(password, SALT, NUM_ITERATIONS, KEY_LENGTH);

    secretKey = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), SECRET_KEY_ALGORITHM);
}

From source file:es.juntadeandalucia.framework.ticket.impl.DefaultTicket.java

public DefaultTicket(Configuration config) throws Exception {
    try {/*from w ww  .  j a  va 2  s .c o m*/
        ticketLifeTime = config.getLong(TIME_TICKET_LIFETIME, 0);
    } catch (Exception e) {
        e = new Exception(msg.getString("ticket.error.lifetimeerror")); //$NON-NLS-1$
        log.warn(e);
        throw e;
    }

    List<?> textKey = config.getList(TICKET_KEY);

    try {
        SecretKeyFactory kf = SecretKeyFactory.getInstance("DESede");
        key = kf.generateSecret(new DESedeKeySpec(hexToByte(textKey)));
    } catch (Exception e) {
        e = new Exception(msg.getString("ticket.error.keycreationerror")); //$NON-NLS-1$
        log.warn(e);
        throw e;
    }
}

From source file:be.tutul.naheulcraft.launcher.auth.Auth.java

private Cipher getCipher(int paramInt, String key) throws Exception {
    Random random = new Random(43287234L);
    byte[] salt = new byte[8];
    random.nextBytes(salt);/*  w  w w .j a va 2 s  . co  m*/
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 5);

    SecretKey pbeKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES")
            .generateSecret(new PBEKeySpec(key.toCharArray()));
    Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
    cipher.init(paramInt, pbeKey, pbeParamSpec);

    return cipher;
}

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

private String generateDESedeKey() throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance("DESede");
    SecretKey desedeKey = keygen.generateKey();

    SecretKeyFactory desedeFactory = SecretKeyFactory.getInstance("DESede");
    DESedeKeySpec desedeSpec = (DESedeKeySpec) desedeFactory.getKeySpec(desedeKey,
            javax.crypto.spec.DESedeKeySpec.class);
    byte[] rawDesedeKey = desedeSpec.getKey();
    return new String(Base64.encodeBase64(rawDesedeKey));
}