Example usage for javax.crypto.spec PBEKeySpec PBEKeySpec

List of usage examples for javax.crypto.spec PBEKeySpec PBEKeySpec

Introduction

In this page you can find the example usage for javax.crypto.spec PBEKeySpec PBEKeySpec.

Prototype

public PBEKeySpec(char[] password, byte[] salt, int iterationCount, int keyLength) 

Source Link

Document

Constructor that takes a password, salt, iteration count, and to-be-derived key length for generating PBEKey of variable-key-size PBE ciphers.

Usage

From source file:Main.java

private static Cipher genCipher(char[] pass, byte[] salt, String factory, int iterations, int mode)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException {
    PBEKeySpec keySpec = new PBEKeySpec(pass, salt, iterations, 128);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(factory);
    SecretKey key = keyFactory.generateSecret(keySpec);
    AlgorithmParameterSpec spec = new PBEParameterSpec(salt, iterations);
    Cipher cipher = Cipher.getInstance(factory);
    cipher.init(mode, key, spec);/*from   w ww.ja v  a 2s .c om*/
    return cipher;
}

From source file:adminpassword.Encryption.java

public String encrypt(String word, String idKey) throws Exception {

    byte[] ivBytes;
    String password = idKey; //you can give whatever you want. This is for testing purpose
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);// ww  w  .j  a  v a 2 s.c  o  m

    byte[] saltBytes = bytes;

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);
    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    //encrypting the word
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    AlgorithmParameters params = cipher.getParameters();
    ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
    byte[] encryptedTextBytes = cipher.doFinal(word.getBytes("UTF-8"));

    //prepend salt and vi
    byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length];
    System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length);
    System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length);
    System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length,
            encryptedTextBytes.length);
    return new Base64().encodeToString(buffer);
}

From source file:adminpassword.AESDemo.java

public String encrypt(String plainText) throws Exception {

    //get salt/*from   w ww. j  a v a2s  . c  om*/
    salt = generateSalt();
    byte[] saltBytes = salt.getBytes("UTF-8");

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, pswdIterations, keySize);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    //encrypt the message
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    AlgorithmParameters params = cipher.getParameters();
    ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
    byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
    return new Base64().encodeAsString(encryptedTextBytes);
}

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: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.openchain.certification.utility.PasswordUtil.java

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

From source file:adminpassword.Decryption.java

@SuppressWarnings("static-access")
public String decrypt(String encryptedText, String idKey) throws Exception {
    String password = idKey;/*w  ww.j  a  va2 s  . c  o  m*/

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    //strip off the salt and iv
    ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText));
    byte[] saltBytes = new byte[20];
    buffer.get(saltBytes, 0, saltBytes.length);
    byte[] ivBytes1 = new byte[cipher.getBlockSize()];
    buffer.get(ivBytes1, 0, ivBytes1.length);
    byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length];
    buffer.get(encryptedTextBytes);

    // Deriving the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1));

    byte[] decryptedTextBytes = null;
    try {
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);

    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return new String(decryptedTextBytes);
}

From source file:com.hypersocket.auth.PasswordEncryptionService.java

public byte[] getEncryptedPassword(char[] password, byte[] salt, PasswordEncryptionType type)
        throws NoSuchAlgorithmException, InvalidKeySpecException {

    KeySpec spec = new PBEKeySpec(password, salt, type.getIterations(), type.getKeyLength());

    SecretKeyFactory f = SecretKeyFactory.getInstance(type.toString());

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

From source file:org.openmrs.module.reportingsummary.api.io.download.DownloadProcessor.java

/**
 * Method to initialize the cipher object with the correct encryption algorithm.
 *
 * @throws Exception//from   w  w  w . ja  v  a 2 s  . c  o  m
 */
private Cipher initializeCipher() throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance(InputOutputConstants.SECRET_KEY_FACTORY);
    KeySpec spec = new PBEKeySpec(password.toCharArray(), password.getBytes(), 1024, 128);
    SecretKey secretKey = factory.generateSecret(spec);

    SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), InputOutputConstants.KEY_SPEC);

    if (log.isDebugEnabled())
        log.debug("Encrypting with: " + secret.getAlgorithm());

    Cipher cipher = Cipher.getInstance(InputOutputConstants.CIPHER_CONFIGURATION);
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    return cipher;
}

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  w  w .ja  v a2s. c o  m*/
    SecretKey key = factory.generateSecret(spec);
    return new SecretKeySpec(key.getEncoded(), ENCRYPTION);
}