Example usage for javax.crypto.spec IvParameterSpec IvParameterSpec

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

Introduction

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

Prototype

public IvParameterSpec(byte[] iv) 

Source Link

Document

Creates an IvParameterSpec object using the bytes in iv as the IV.

Usage

From source file:com.elle.analyster.admissions.AESCrypt.java

public static String encrypt(String key, String initVector, String value) {
    try {/*  w w w .  j av a 2s.c o  m*/
        IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(value.getBytes());
        System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted));

        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:com.sv.udb.controlador.CtrlContras.java

public String encrypt(String cleartext) throws Exception {
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] encrypted = cipher.doFinal(cleartext.getBytes());
    return new String(encodeBase64(encrypted));
}

From source file:com.salesmanager.core.util.EncryptionUtil.java

public static String decrypt(String key, String value) throws Exception {

    if (value == null || value.equals(""))
        return "";

    // NEED TO UNDERSTAND WHY PKCS5Padding DOES NOT WORK
    // Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
    IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
    byte[] outText;
    outText = cipher.doFinal(hexToBytes(value));
    return new String(outText);

}

From source file:ec.edu.uce.medicina.seguimiento.util.EncryptionUtility.java

/**
 *Mtodo que permite la desencriptacin de la contrasea
 * @param encrypted//from   w  ww  .  jav  a 2 s.  com
 * @return
 * @throws Exception
 */
public static String decrypt(String encrypted) throws Exception {
    String key = "02AE31B79CCCB2A3"; //llave
    String iv = "0123456789ABCDEF";
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    byte[] enc = decodeBase64(encrypted);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] decrypted = cipher.doFinal(enc);
    return new String(decrypted);
}

From source file:com.jk.security.JKEncDec.java

/**
 * Encrypt.//w ww . j  a  v a2  s.c  om
 *
 * @param plainText
 *            the plain text
 * @return the string
 */
public static String encrypt(String plainText) {
    try {
        plainText = fixText(plainText);
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
        return toString(cipher.doFinal(plainText.getBytes("UTF-8")));
    } catch (Exception e) {
        throw new JKSecurityException(e);
    }
}

From source file:com.vmware.fdmsecprotomgmt.PasswdEncrypter.java

/**
 * Encrypt the value with key provided/*from w  w w  .  ja v a 2  s  .  com*/
 */
private static String encrypt(String key, String value) {
    String encryptedString = null;
    try {
        IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(value.getBytes());
        encryptedString = Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        System.out.println("Caught exception while encrypting string : " + value);
        ex.printStackTrace();
    }

    return encryptedString;
}

From source file:it.latraccia.pkcs11.reader.util.AESUtil.java

public static String encryptString(String clearText, String password)
        throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    String encrypted;// w w  w .  jav a  2  s .  co m
    byte[] key = password.getBytes();
    // Check the length of the password
    if (key.length != 16) {
        throw new IllegalArgumentException("Invalid password length.");
    }
    byte[] value = clearText.getBytes();

    // Encrypt with AES/CBC/PKCS5Padding
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
    byte[] encryptedBytes = cipher.doFinal(value);

    // Encode the bytes in base64
    encrypted = Base64.encodeBase64String(encryptedBytes);
    return encrypted;
}

From source file:encrypt.algorithms.AESCBC.java

public String decrypt(String key1, String key2, String encrypted) {
    try {/*  w w w .j a  v a 2s  .co  m*/
        IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));

        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/NOPADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.rr.familyPlanning.ui.security.decryptObject.java

/**
 * Decrypts the String and serializes the object
 *
 * @param base64Data/*from  w ww. ja  va2s  .c  o m*/
 * @param base64IV
 * @return
 * @throws Exception
 */
public Object decryptObject(String base64Data, String base64IV) throws Exception {
    // Decode the data
    byte[] encryptedData = Base64.decodeBase64(base64Data.getBytes());

    // Decode the Init Vector
    byte[] rawIV = Base64.decodeBase64(base64IV.getBytes());

    // Configure the Cipher
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivSpec = new IvParameterSpec(rawIV);
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.update(keyString.getBytes());
    byte[] key = new byte[16];
    System.arraycopy(digest.digest(), 0, key, 0, key.length);
    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

    // Decrypt the data..
    byte[] decrypted = cipher.doFinal(encryptedData);

    // Deserialize the object
    ByteArrayInputStream stream = new ByteArrayInputStream(decrypted);

    ObjectInput in = new ObjectInputStream(stream);
    Object obj = null;
    try {
        obj = in.readObject();
    } finally {
        stream.close();
        in.close();
    }
    return obj;
}

From source file:org.bigmouth.nvwa.utils.degist.DesUtils.java

public static String decrypt(String content, String key, byte[] iv) {
    try {//w  w w  .ja v a  2s.  co  m
        if (StringUtils.length(key) != 8) {
            throw new IllegalArgumentException("Key must be 8 byte");
        }
        byte[] contentBytes = Base64.decodeBase64(content);
        SecretKeySpec secretkey = new SecretKeySpec(key.getBytes(), "DES");
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretkey, zeroIv);
        byte[] decryptedData = cipher.doFinal(contentBytes);
        return new String(decryptedData);
    } catch (Exception e) {
        LOGGER.error("decrypt:", e);
        return null;
    }
}