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

public static byte[] getEncCode(byte[] byteE, String seed) {
    byte[] byteFina = null;
    Cipher cipher = null;//from  ww w.  ja v a 2 s .  com

    try {
        SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(seed.getBytes()), "AES");

        cipher = Cipher.getInstance("AES/CFB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        //cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        byteFina = cipher.doFinal(byteE);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }

    return byteFina;
}

From source file:EncDec.AES.java

public static String encrypt(String key, String initVector, String value) {
    try {/*from   w w w  .  j  ava2 s  . 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:org.cifssynchronizer.core.Security.java

public static String encrypt(String key1, String key2, String value) {
    String str = null;/* w  ww. j  ava  2s  .c o m*/

    try {
        IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(value.getBytes());

        str = Base64.encodeBase64String(encrypted);
    } catch (Exception ignored) {
    }

    return str;
}

From source file:authentication.AES.java

public static String encrypt(String textopuro) throws Exception {
    Cipher encripta = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(chaveencriptacao.getBytes("UTF-8"), "AES");
    encripta.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
    byte[] a = encripta.doFinal(textopuro.getBytes("UTF-8"));
    return StringUtils.newStringUtf8(Base64.encodeBase64(a, false));
}

From source file:Encrypt.java

private static String decrypt(String message) throws Exception {
    byte[] bytesrc = convertHexString(message);
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
    IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));

    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
    byte[] retByte = cipher.doFinal(bytesrc);
    return new String(retByte);
}

From source file:clases.Seguridad.java

public static String encriptar(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.avbravo.avbravoutils.crypto.Encriptador.java

public static String encrypt(String key, 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:Controller.StringEncrypt.java

public final static String encrypt(String cleartext) {
    byte[] encrypted = null;
    try {//ww  w .  j  a  va  2  s. co m
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec skeySpec = new SecretKeySpec("92AE31A79FEEB2A3".getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec("0123456789ABCDEF".getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
        encrypted = cipher.doFinal(cleartext.getBytes());
    } catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException
            | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
        JOptionPane.showMessageDialog(null, "Error encriptando contrasea");
    }
    return new String(encodeBase64(encrypted));
}

From source file:com.lwr.software.reporter.utils.EncryptionUtil.java

public static String encrypt(String value) {
    try {/*from ww w.  j  ava 2  s .  c  om*/
        IvParameterSpec iv = new IvParameterSpec(
                DashboardConstants.INIT_VECTOR.getBytes(DashboardConstants.ENCODING));
        SecretKeySpec skeySpec = new SecretKeySpec(
                DashboardConstants.INIT_KEY.getBytes(DashboardConstants.ENCODING),
                DashboardConstants.ALGORITHM);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(value.getBytes());
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] encipherAes256(byte[] clearText, String keyString) throws NullPointerException {
    if (keyString == null || keyString.length() == 0) {
        throw new NullPointerException("Please give Password");
    }// w w  w.  ja v  a2 s.  c o  m

    if (clearText == null || clearText.length <= 0) {
        throw new NullPointerException("Please give clearText");
    }

    try {
        SecretKeySpec skeySpec = getKey(keyString);

        // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
        final byte[] iv = new byte[16];
        Arrays.fill(iv, (byte) 0x00);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

        // Cipher is not thread safe
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);

        return cipher.doFinal(clearText);

    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    return null;
}