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.opentravel.pubs.config.PasswordHelper.java

/**
 * Encrypts the given password.// www  . j a va  2 s.co  m
 * 
 * @param clearTextPassword  the clear-text password to encrypt
 * @return String
 */
public static String encrypt(String clearTextPassword) {
    try {
        String encryptionPassword = ConfigSettingsFactory.getConfig().getEncryptionPassword();
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM);
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(encryptionPassword.toCharArray()));
        Cipher pbeCipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);

        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        return new String(new Base64().encode(pbeCipher.doFinal(clearTextPassword.getBytes("UTF-8"))));

    } catch (GeneralSecurityException | UnsupportedEncodingException e) {
        throw new RuntimeException("Error during password encryption.", e);
    }
}

From source file:org.opentravel.pubs.config.PasswordHelper.java

/**
 * Decrypts the given encrypted password.
 * /*from   ww  w .  java2 s . c o  m*/
 * @param encryptedPassword  the encrypted password to decrypt
 * @return
 */
public static String decrypt(String encryptedPassword) {
    try {
        String encryptionPassword = ConfigSettingsFactory.getConfig().getEncryptionPassword();
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM);
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(encryptionPassword.toCharArray()));
        Cipher pbeCipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);

        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        return new String(pbeCipher.doFinal(new Base64().decode(encryptedPassword)), "UTF-8");

    } catch (GeneralSecurityException | IOException e) {
        throw new RuntimeException("Error during password decryption.", e);
    }
}

From source file:org.underworldlabs.util.DesEncrypter.java

public static SecretKey getSecretKey(String key) throws Exception {
    byte[] keyAsBytes = key.getBytes("UTF8");
    KeySpec keySpec = new DESKeySpec(keyAsBytes);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    return keyFactory.generateSecret(keySpec);
}

From source file:com.example.license.DESUtil.java

/**
 * /*from  w w w.jav a  2 s  .  co  m*/
 * ?key
 * 
 * @param KeyStr
 *            
 * @return 
 * @throws InvalidKeyException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws Exception
 */
private static SecretKey keyGenerator(String keyStr) throws Exception {
    byte input[] = HexString2Bytes(keyStr);
    DESKeySpec desKey = new DESKeySpec(input);
    // ?DESKeySpec??
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey securekey = keyFactory.generateSecret(desKey);
    return securekey;
}

From source file:com.example.license.DESUtil.java

/**
 * ?//from w w w  .j  a v  a2  s.com
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String decryptBase64(String data, String key) throws Exception {
    DESKeySpec desKey = new DESKeySpec(key.getBytes());
    // ?DESKeySpec??
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey securekey = keyFactory.generateSecret(desKey);

    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, securekey);
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data.getBytes())));
}

From source file:com.example.license.DESUtil.java

public static String encryptBase64(String data, String key) throws Exception {

    DESKeySpec desKey = new DESKeySpec(key.getBytes());
    // ?DESKeySpec??
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey securekey = keyFactory.generateSecret(desKey);

    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, securekey);

    byte[] results = cipher.doFinal(data.getBytes("UTF-8"));
    // Encrypt/*ww  w  .  j  a v a2 s  .  c  o m*/
    // byte[] unencryptedByteArray = data.getBytes("UTF8");
    // byte[] encryptedBytes = encryptCipher.doFinal(unencryptedByteArray);

    // Encode bytes to base64 to get a string
    byte[] encodedBytes = Base64.encodeBase64(results);
    return new String(encodedBytes);
}

From source file:com.redsqirl.workflow.utils.FileStream.java

private static SecretKey generateKey() throws Exception {
    PBEKeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), salt, iterations, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    return keyFactory.generateSecret(keySpec);
}

From source file:com.aspose.showcase.qrcodegen.web.api.util.StringEncryptor.java

public static String decrypt(String encryptedData, String password) throws Exception {

    Security.addProvider(new BouncyCastleProvider());

    byte[] encrypted = Base64.decode(encryptedData);

    Cipher cipher0 = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");

    // Openssl puts SALTED__ then the 8 byte salt at the start of the file.
    // We simply copy it out.
    byte[] salt = new byte[SALT_SIZE];
    System.arraycopy(encrypted, SALT_SIZE, salt, 0, SALT_SIZE);

    SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC");
    cipher0.init(Cipher.DECRYPT_MODE,
            fact.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_SALE_SIZE)));

    // Decrypt the rest of the byte array (after stripping off the salt)
    byte[] decValue = cipher0.doFinal(encrypted, SALT_STRIP_LENGTH, encrypted.length - SALT_STRIP_LENGTH);

    return new String(decValue);
}

From source file:com.networknt.light.util.HashUtil.java

public static String generateStorngPasswordHash(String password)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    int iterations = 1000;
    char[] chars = password.toCharArray();
    byte[] salt = getSalt().getBytes();

    PBEKeySpec spec = new PBEKeySpec(chars, salt, iterations, 64 * 8);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] hash = skf.generateSecret(spec).getEncoded();
    return iterations + ":" + toHex(salt) + ":" + toHex(hash);
}

From source file:org.demo.show.providers.ThreeDes2.java

/**
 * ?/*from  www.ja va2  s.co m*/
 * 
 * @param key
 *            
 * @return Key 
 * */
public static Key toKey(byte[] key) throws Exception {
    // Des
    DESedeKeySpec dks = new DESedeKeySpec(key);
    // 
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
    // ?
    SecretKey secretKey = keyFactory.generateSecret(dks);
    return secretKey;
}