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:it.scoppelletti.security.keygen.DESedeKeyToPropertySetProvider.java

public Properties toProperties(Key key) {
    byte[] data;//  w w  w  .  j a  va 2  s  .com
    SecretKey desKey;
    SecretKeyFactory keyFactory;
    DESedeKeySpec param;
    Properties props;

    if (!(key instanceof SecretKey)) {
        return null;
    }

    try {
        keyFactory = SecretKeyFactory.getInstance(DESedeKeyFactory.ALGORITHM);
    } catch (NoSuchAlgorithmException ex) {
        return null;
    }

    try {
        desKey = keyFactory.translateKey((SecretKey) key);
    } catch (InvalidKeyException ex) {
        return null;
    }

    try {
        param = (DESedeKeySpec) keyFactory.getKeySpec(desKey, DESedeKeySpec.class);
    } catch (InvalidKeySpecException ex) {
        return null;
    }

    props = new Properties();
    props.setProperty(CryptoUtils.PROP_KEYFACTORY, DESedeKeyFactory.class.getName());
    data = param.getKey();
    props.setProperty(DESedeKeyFactory.PROP_KEY, Hex.encodeHexString(data));
    Arrays.fill(data, Byte.MIN_VALUE);

    return props;
}

From source file:com.marvelution.jira.plugins.hudson.encryption.StringEncrypter.java

/**
 * Constructor//from w  ww  .ja v a 2s .c o m
 */
public StringEncrypter() {
    try {
        keySpec = new DESedeKeySpec(DEFAULT_ENCRYPTION_KEY.getBytes(UNICODE_FORMAT));
        keyFactory = SecretKeyFactory.getInstance(DESEDE_ENCRYPTION_SCHEME);
        cipher = Cipher.getInstance(DESEDE_ENCRYPTION_SCHEME);
    } catch (Exception e) {
        LOGGER.fatal("Failed to initilise String Encryption classes. Reason: " + e.getMessage(), e);
        throw new StringEncryptionException(
                "Failed to initilise String Encryption classes. Reason: " + e.getMessage(), e);
    }
}

From source file:org.talend.daikon.security.CryptoHelper.java

/**
 * @param passPhrase the pass phrase used to encrypt and decrypt strings.
 *///from w w  w . ja  v a  2 s .c o m
public CryptoHelper(String passPhrase) {
    try {
        // Create the key
        KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); //$NON-NLS-1$
        ecipher = Cipher.getInstance(key.getAlgorithm());
        dcipher = Cipher.getInstance(key.getAlgorithm());

        // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

        // Create the ciphers
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
    } catch (Exception e) {
        // do nothing
    }
}

From source file:club.jmint.crossing.specs.Security.java

public static String desEncrypt(String data, String key) throws CrossException {
    String ret = null;//  w ww . jav a2s.  c o m
    try {
        DESKeySpec desKey = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);

        Cipher cipher = Cipher.getInstance(CIPHER_DES_ALGORITHM);
        SecureRandom random = new SecureRandom();
        cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
        byte[] results = cipher.doFinal(data.getBytes("UTF-8"));
        ret = Base64.encodeBase64String(results);
    } catch (Exception e) {
        CrossLog.printStackTrace(e);
        throw new CrossException(ErrorCode.COMMON_ERR_ENCRYPTION.getCode(),
                ErrorCode.COMMON_ERR_ENCRYPTION.getInfo());
    }
    return ret;
}

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

public SimpleDesCipher(String base64Key) {
    try {//from   ww w .j a v  a 2 s. c om
        cipher = Cipher.getInstance("DESede");
    } catch (NoSuchAlgorithmException e1) {
        log.error(e1.getMessage(), e1);
    } catch (NoSuchPaddingException e) {
        log.error(e.getMessage(), e);
    }

    byte[] raw = Base64.decodeBase64(base64Key);

    DESedeKeySpec keyspec;
    try {
        keyspec = new DESedeKeySpec(raw);

        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        skey = keyfactory.generateSecret(keyspec);
    } catch (InvalidKeyException e) {
        log.error(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        log.error(e.getMessage(), e);
    } catch (InvalidKeySpecException e) {
        log.error(e.getMessage(), e);
    }
}

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

/**
 * Constructor for DefaultCryptor.//from w  w  w  . j a va2  s  . c o m
 */
public DefaultCryptor() throws NoSuchAlgorithmException, InvalidKeySpecException {
    // Create PBE parameter set
    pbeParamSpec = new PBEParameterSpec(salt, count);
    pbeKeySpec = new PBEKeySpec("saagar".toCharArray());
    keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    pbeKey = keyFac.generateSecret(pbeKeySpec);
}

From source file:net.bioclipse.encryption.Encrypter.java

public String encrypt(String plaintext) {
    SecretKeyFactory keyFac;//from ww  w  .java2s .  c o  m
    SecretKey pbeKey;
    Cipher pbeCipher;
    try {
        keyFac = SecretKeyFactory.getInstance(METHOD);
        pbeKey = keyFac.generateSecret(pbeKeySpec);
        pbeCipher = Cipher.getInstance(METHOD);
        pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
        return new String(Base64.encodeBase64(pbeCipher.doFinal(plaintext.getBytes())));
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException(e);
    } catch (NoSuchPaddingException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e);
    } catch (InvalidAlgorithmParameterException e) {
        throw new IllegalStateException(e);
    } catch (IllegalBlockSizeException e) {
        throw new IllegalStateException(e);
    } catch (BadPaddingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.jajim.utilidades.cifrado.Cifrador.java

/**
 * Constructor de la clase. Inicializa el cifrador.
 * <p>/*from   w  ww . jav  a  2s  .co  m*/
 * @throws ImposibleCifrarDescifrarException Si no se puede instanciar un ci frador adecuado.
 */
public Cifrador() throws ImposibleCifrarDescifrarException {

    try {
        // Inicializacin de la clave
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        String clave = "cjliamve";
        DESKeySpec kspec = new DESKeySpec(clave.getBytes());
        sk = skf.generateSecret(kspec);
        // Inicializacin del cifrador
        cifrado = Cipher.getInstance("DES");
    } catch (NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException
            | NoSuchPaddingException e) {
        // En caso de que se produzca un error se escribe en el fichero
        // de log y se lanza una excepcin
        ManejadorDeLogs mdl = ManejadorDeLogs.getManejadorDeLogs();
        mdl.escribir("No se puede crear un cifrador de DES");
        throw new ImposibleCifrarDescifrarException();
    }
}

From source file:com.hurence.logisland.util.string.Anonymizer.java

public String anonymize(String str) throws NoSuchAlgorithmException, InvalidKeySpecException {

    // Hash the password
    PBEKeySpec spec = new PBEKeySpec(str.toCharArray(), salt, PBKDF2_ITERATIONS, HASH_BYTE_SIZE * 8);
    SecretKeyFactory skf = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);
    byte[] hash = skf.generateSecret(spec).getEncoded();

    return Hex.encodeHexString(hash);
}

From source file:org.orcid.core.crypto.DesEncrypter.java

private void initDesEncrypter(final String passPhrase) {
    try {//w  w  w . j  a  v a  2  s  . com
        // Create the key
        KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        ecipher = Cipher.getInstance(key.getAlgorithm());
        dcipher = Cipher.getInstance(key.getAlgorithm());

        // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

        // Create the ciphers
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
    } catch (GeneralSecurityException e) {
        LOGGER.trace("DesEncrypter.creation failed", e);
        throw new ApplicationException("DesEncrypter creation failed", e);
    }
}