Example usage for javax.crypto SecretKeyFactory generateSecret

List of usage examples for javax.crypto SecretKeyFactory generateSecret

Introduction

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

Prototype

public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException 

Source Link

Document

Generates a SecretKey object from the provided key specification (key material).

Usage

From source file:es.jamisoft.comun.utils.cipher.CifradoPBE3DES.java

License:asdf

public SecretKey generatePBKey(char secret[]) {
    try {//  w  w w  .  j av  a  2 s.  c  o  m
        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;
    }
}

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  .jav a  2s.  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: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.craftercms.social.util.support.security.crypto.SimpleDesCipher.java

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

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

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

public String decryptText(String textToDecrypt) throws Exception {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(getMasterPassword()));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return new String(pbeCipher.doFinal(Base64.decodeBase64(textToDecrypt)), "UTF-8");
}

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:ch.rgw.tools.PasswordEncryptionService.java

public byte[] getEncryptedPassword(String password, byte[] salt)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    // PBKDF2 with SHA-1 as the hashing algorithm. Note that the NIST
    // specifically names SHA-1 as an acceptable hashing algorithm for PBKDF2
    String algorithm = "PBKDF2WithHmacSHA1";
    // SHA-1 generates 160 bit hashes, so that's what makes sense here
    int derivedKeyLength = 160;
    // Pick an iteration count that works for you. The NIST recommends at
    // least 1,000 iterations:
    // http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
    // iOS 4.x reportedly uses 10,000:
    // http://blog.crackpassword.com/2010/09/smartphone-forensics-cracking-blackberry-backup-passwords/
    int iterations = 20000;

    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength);
    SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm);

    return f.generateSecret(spec).getEncoded();
}

From source file:net.sf.hajdbc.codec.crypto.CipherCodecFactoryTest.java

@Before
public void before() throws Exception {
    File file = File.createTempFile("ha-jdbc", "keystore");

    SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
    this.key = factory.generateSecret(new DESKeySpec(Base64.decodeBase64(KEY.getBytes())));
    KeyStore store = KeyStore.getInstance(CipherCodecFactory.Property.KEYSTORE_TYPE.defaultValue);
    store.load(null, null);/*from  www .  j av  a2 s.c  o  m*/
    store.setKeyEntry(CipherCodecFactory.Property.KEY_ALIAS.defaultValue, this.key, KEY_PASSWORD.toCharArray(),
            null);

    FileOutputStream out = new FileOutputStream(file);
    try {
        store.store(out, STORE_PASSWORD.toCharArray());
    } finally {
        Resources.close(out);
    }

    System.setProperty(CipherCodecFactory.Property.KEYSTORE_FILE.name, file.getPath());
    System.setProperty(CipherCodecFactory.Property.KEYSTORE_PASSWORD.name, STORE_PASSWORD);
    System.setProperty(CipherCodecFactory.Property.KEY_PASSWORD.name, KEY_PASSWORD);
}

From source file:org.starnub.utilities.crypto.PasswordHash.java

/**
 * using PBKDF2 from Sun, an alternative is https://github.com/wg/scrypt
 * cf. http://www.unlimitednovelty.com/2012/03/dont-use-bcrypt.html
 * @param password String password to be salted
 * @param salt Byte[] of the salt/*  www .j ava  2  s  .c  om*/
 * @return String of the hashed password
 * @throws Exception
 */
private String hash(String password, byte[] salt) throws Exception {
    if (password == null || password.length() == 0)
        throw new IllegalArgumentException("Empty passwords are not supported.");
    SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    SecretKey key = f.generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLen));
    return Base64.encodeBase64String(key.getEncoded());
}

From source file:com.sapienter.jbilling.common.JBCryptoImpl.java

public JBCryptoImpl(String password) throws InvalidKeySpecException {
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray());
    SecretKeyFactory keyFactory = getSecretKeyFactory();
    mySecretKey = keyFactory.generateSecret(spec);
}