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:Main.java

private static byte[] doFinal(String key, int opmode, byte[] input)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException {
    key = checkNull(key) ? DEFAULT_KEY : key;
    if (checkNull(key)) {
        return null;
    }// w w  w.  j  ava 2s .c o m
    SecureRandom sr = new SecureRandom();
    DESKeySpec dks = new DESKeySpec(key.getBytes());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance(MODE);
    cipher.init(opmode, securekey, sr);
    return cipher.doFinal(input);
}

From source file:org.openchain.certification.utility.PasswordUtil.java

private static String hash(String password, byte[] salt)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    SecretKey key = factory
            .generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLength));
    return Base64.encodeBase64String(key.getEncoded());
}

From source file:Main.java

public static String decryptData(String ciphertext, String password) throws Exception {
    int iterationCount = 100; //because polaroid
    int keyLength = 256;

    String[] fields = ciphertext.split("]");
    byte[] iv = Base64.decode(fields[0], 0);
    byte[] salt = Base64.decode(fields[1], 0);
    byte[] cipherBytes = Base64.decode(fields[2], 0);

    Log.d(TAG, "ciphertext: " + ciphertext + "\n" + "iv length is " + "\n" + iv.length);

    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
    SecretKey key = new SecretKeySpec(keyBytes, "AES");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivParams = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, ivParams);
    byte[] plaintext = cipher.doFinal(cipherBytes);
    String plainStr = new String(plaintext, "UTF-8");

    return plainStr;
}

From source file:com.connorbrezinsky.msg.security.Hash.java

private static 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:Main.java

/**
 *
 * Creates a SecretKey from an encoded DESedeKeySpec
 * //  ww  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:Main.java

public static byte[] generateHash(char[] pass, byte[] salt)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    PBEKeySpec keySpec = new PBEKeySpec(pass, salt, ITERATION_COUNT, 128);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_FACTORY);
    SecretKey key = keyFactory.generateSecret(keySpec);

    return key.getEncoded();
}

From source file:org.craftercms.commons.crypto.impl.PbkAesTextEncryptor.java

private static Key generateKey(String password, String salt) throws CryptoException {
    try {//from   ww  w  .  ja  va2  s .  c o m
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), Base64.decodeBase64(salt), PBK_ITER, PBK_LEN);
        SecretKeyFactory factory = SecretKeyFactory.getInstance(PBK_ALGORITHM);

        return new SecretKeySpec(factory.generateSecret(keySpec).getEncoded(),
                CryptoUtils.AES_CIPHER_ALGORITHM);
    } catch (GeneralSecurityException e) {
        throw new CryptoException("Unable to generate PBK key", e);
    }
}

From source file:ch.helmchen.camlapse.user.control.Encryption.java

public static String decrypt(String aBase64String) throws GeneralSecurityException, IOException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return new String(pbeCipher.doFinal(base64Decode(aBase64String)));
}

From source file:ch.helmchen.camlapse.user.control.Encryption.java

/**
 * Verschlsselt den bergebenen String.//from  ww w.  j  av  a 2  s.c  o m
 * 
 * @param aString zu verschlsselnder String.
 */
public static String encrypt(final String aString) throws GeneralSecurityException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return base64Encode(pbeCipher.doFinal(aString.getBytes()));
}

From source file:com.ro.ssc.app.client.licensing.TrialKeyGenerator.java

public static String generateKey(String toEncode) {

    String encoded = "";
    try {//from www.  j  av  a 2 s  .  c  o  m
        byte[] saltEncrypt = SALT_ENCRYPT.getBytes();
        SecretKeyFactory factoryKeyEncrypt = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY);
        SecretKey tmp = factoryKeyEncrypt.generateSecret(
                new PBEKeySpec(PASS_ENCRYPT.toCharArray(), saltEncrypt, ITERATIONS_ENCRYPT, KEY_LENGTH));
        SecretKeySpec encryptKey = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);
        Cipher aesCipherEncrypt = Cipher.getInstance(CIPHER);
        aesCipherEncrypt.init(Cipher.ENCRYPT_MODE, encryptKey);
        byte[] bytes = StringUtils.getBytesUtf8(toEncode);
        byte[] encryptBytes = aesCipherEncrypt.doFinal(bytes);
        encoded = Base64.encodeBase64URLSafeString(encryptBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encoded;
}