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.opennms.features.scv.impl.JCEKSSecureCredentialsVault.java

@Override
public void setCredentials(String alias, Credentials credentials) {
    try {/*  ww w .jav  a  2 s  . c o  m*/
        byte[] credentialBytes = toBase64EncodedByteArray(credentials);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
        SecretKey generatedSecret = factory.generateSecret(new PBEKeySpec(
                new String(credentialBytes).toCharArray(), m_salt, m_iterationCount, m_keyLength));

        KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(m_password);
        m_keystore.setEntry(alias, new KeyStore.SecretKeyEntry(generatedSecret), keyStorePP);
        writeKeystoreToDisk();
    } catch (KeyStoreException | InvalidKeySpecException | NoSuchAlgorithmException | IOException e) {
        throw Throwables.propagate(e);
    }
}

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

public String encrypt(String plaintext) {
    SecretKeyFactory keyFac;
    SecretKey pbeKey;//  w w  w  .  j a  va  2 s.c  o  m
    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.yes.cart.shoppingcart.support.impl.AbstractCryptedTuplizerImpl.java

/**
 * Default Constructor./*from  www.j a v a 2s  .  c  o  m*/
 *
 * @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:net.bioclipse.encryption.Encrypter.java

public String decrypt(String encrypted) {
    SecretKeyFactory keyFac;
    SecretKey pbeKey;/*ww w.ja  va  2 s.com*/
    Cipher pbeCipher;

    try {
        keyFac = SecretKeyFactory.getInstance(METHOD);
        pbeKey = keyFac.generateSecret(pbeKeySpec);
        pbeCipher = Cipher.getInstance(METHOD);
        pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
        return new String(pbeCipher.doFinal(Base64.decodeBase64(encrypted.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:net.thewaffleshop.nimbus.api.EncryptionAPI.java

/**
 * Generate a {@link SecretKey} from a password and salt
 *
 * @param password//from  w ww.j  a v  a 2  s . c  o m
 * @param salt
 * @return
 */
public SecretKey createSecretKey(String password, byte[] salt) {
    try {
        PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 1024, 256);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        SecretKey secretKey = factory.generateSecret(keySpec);
        return new SecretKeySpec(secretKey.getEncoded(), "AES");
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.seer.datacruncher.utils.CryptoUtil.java

public CryptoUtil() {
    try {//from  www . ja va2  s  .  c  o  m
        String myEncryptionKey = cryWorm(Reserved.ENCRYPTIONKEY, Reserved.CHECK, 0);
        String myEncryptionSchema = DESEDE_ENCRYPTION_SCHEMA;
        byte[] keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
        KeySpec myKeySpec = new DESedeKeySpec(keyAsBytes);
        SecretKeyFactory mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionSchema);
        cipher = Cipher.getInstance(myEncryptionSchema);
        key = mySecretKeyFactory.generateSecret(myKeySpec);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

public DefaultTicket(Configuration config) throws Exception {
    try {//  ww  w.  j a  va2  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:com.paxxis.cornerstone.encryption.TripleDESEncryptionHandler.java

public void initialize() {
    if (encryptionKeyFile != null) {
        try {/*www .  ja  v a2s .c  om*/
            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:com.paypal.utilities.AESDecrypt.java

public AESDecrypt(String encryptedString) throws Exception {
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
    SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
    SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES");

    encrypt = Base64.decodeBase64(encryptedString.getBytes());

    eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    eCipher.init(Cipher.ENCRYPT_MODE, secretKey);

    byte[] iv = extractIV();
    dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
}

From source file:com.blackcrowsys.sinscrypto.AesKeyGenerator.java

@Override
public SecretKey generateSecretKey(String password, String salt)
        throws NoSuchAlgorithmException, InvalidKeySpecException, DecoderException {
    SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
    KeySpec spec = new PBEKeySpec(password.toCharArray(), Hex.decodeHex(salt.toCharArray()), ITERATION,
            KEYLENGTH);//from w  ww. ja va  2s .c  o  m
    SecretKey key = factory.generateSecret(spec);
    return new SecretKeySpec(key.getEncoded(), ENCRYPTION);
}