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:org.craftercms.social.util.support.security.crypto.SimpleDesCipher.java

public SimpleDesCipher(String base64Key) {
    try {//w ww  .j  a  va  2s.c  o m
        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:com.sirius.utils.encrypt.DESEncryptor.java

private Key getKey(Object key) throws Exception {
    if (key instanceof String) {
        DESKeySpec dks = new DESKeySpec(((String) key).getBytes());
        SecretKeyFactory skf = SecretKeyFactory.getInstance(ALGORITHM, security_provider);
        return skf.generateSecret(dks);
    } else {/* ww  w  . ja v a2  s. com*/
        throw new EncryptException("Invilid key.");
    }
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.util.StringProtector.java

public StringProtector(final String passwd) throws GeneralSecurityException {
    final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    final SecretKey key = keyFactory.generateSecret(new PBEKeySpec(passwd.toCharArray()));
    encryptor = Cipher.getInstance("PBEWithMD5AndDES");
    encryptor.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));

    decryptor = Cipher.getInstance("PBEWithMD5AndDES");
    decryptor.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
}

From source file:org.datacleaner.util.convert.EncodedStringConverter.java

@Override
public String fromString(Class<?> type, String encodedPassword) {
    if (encodedPassword == null) {
        return null;
    }//from ww w  .  ja  va 2  s .  c om
    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.haulmont.cuba.core.sys.encryption.Sha1EncryptionModule.java

protected String apply(String content, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeySpec keySpec = getKeySpec(content, salt);

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);

    byte[] encoded = keyFactory.generateSecret(keySpec).getEncoded();
    return new String(Hex.encodeHex(encoded));
}

From source file:org.noroomattheinn.utils.PWUtils.java

public byte[] getEncryptedPassword(String password, byte[] salt) {
    // 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);

    try {/* w  ww  . j  a  v a2 s. com*/
        SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm);
        return f.generateSecret(spec).getEncoded();
    } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
        Logger.getLogger(PWUtils.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }

}

From source file:org.datacleaner.util.convert.EncodedStringConverter.java

@Override
public String toString(String password) {
    if (password == null) {
        return null;
    }/*from  w w w.ja  v a  2  s.  co  m*/
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORHITM);
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(_secret));
        Cipher pbeCipher = Cipher.getInstance(ALGORHITM);
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(_salt, 20));

        byte[] bytes = pbeCipher.doFinal(password.getBytes());

        bytes = Base64.encodeBase64(bytes, false);
        return new String(bytes, "UTF-8");
    } catch (Exception e) {
        throw new IllegalStateException("Unable to encode password", e);
    }
}

From source file:com.registryKit.user.userManager.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;

    // byte[] b = string.getBytes(Charset.forName("UTF-8"));
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength);

    SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm);

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

From source file:com.mmj.app.common.security.EncryptBuilder.java

private byte[] doCrypt(String source, String secretKey, ICrypt iCrypt)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] cryptedData = iCrypt.getCryptedData(source);
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    SecureRandom sr = new SecureRandom();
    byte[] rawKeyData = (new String(secretKey)).getBytes();

    DESKeySpec dks = new DESKeySpec(rawKeyData);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey key = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES");
    iCrypt.initCipher(key, sr, cipher);/*  ww  w .j av a2 s .  c  o  m*/

    return cipher.doFinal(cryptedData);
}

From source file:com.fengduo.bee.commons.security.EncryptBuilder.java

@SuppressWarnings("restriction")
private byte[] doCrypt(String source, String secretKey, ICrypt iCrypt)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] cryptedData = iCrypt.getCryptedData(source);
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    SecureRandom sr = new SecureRandom();
    byte[] rawKeyData = (new String(secretKey)).getBytes();

    DESKeySpec dks = new DESKeySpec(rawKeyData);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey key = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES");
    iCrypt.initCipher(key, sr, cipher);/*from   w ww  . ja  v  a  2  s . com*/

    return cipher.doFinal(cryptedData);
}