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:org.apache.spark.network.crypto.TransportCipher.java

private CryptoOutputStream createOutputStream(WritableByteChannel ch) throws IOException {
    return new CryptoOutputStream(cipher, conf, ch, key, new IvParameterSpec(outIv));
}

From source file:com.tapchatapp.android.service.GCMReceiver.java

private byte[] decrypt(byte[] cipherText, byte[] key, byte[] iv) throws Exception {
    SecretKey keySpec = new SecretKeySpec(key, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
    aes.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
    return aes.doFinal(cipherText);
}

From source file:io.personium.core.model.file.DataCryptor.java

/**
 * Generate InputStream for encryption from Input and return it.
 * If encryptEnable is false, it returns input as is.
 * @param input input data//w w  w.j  ava  2  s.com
 * @param encryptEnable encryption flag
 * @return InputStream for encryption
 */
public InputStream encode(InputStream input, boolean encryptEnable) {
    if (!encryptEnable) {
        return input;
    }
    try {
        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(iv));
        CipherInputStream encodedInputStream = new CipherInputStream(input, cipher);

        return encodedInputStream;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:CipherSocket.java

public OutputStream getOutputStream() throws IOException {
    OutputStream os = delegate == null ? super.getOutputStream() : delegate.getOutputStream();
    Cipher cipher = null;//  w  w  w. j a  va 2  s .c o m
    try {
        cipher = Cipher.getInstance(algorithm);
        int size = cipher.getBlockSize();
        byte[] tmp = new byte[size];
        Arrays.fill(tmp, (byte) 15);
        IvParameterSpec iv = new IvParameterSpec(tmp);
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);
    } catch (Exception e) {
        throw new IOException("Failed to init cipher: " + e.getMessage());
    }
    CipherOutputStream cos = new CipherOutputStream(os, cipher);
    return cos;
}

From source file:jeffaschenk.tomcat.zuul.util.KeyFileUtils.java

public void InitCiphers() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,
        InvalidKeyException, InvalidAlgorithmParameterException {
    //1. create the cipher using Bouncy Castle Provider
    encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider());
    //2. create the key
    SecretKey keyValue = new SecretKeySpec(key, "AES");
    //3. create the IV
    AlgorithmParameterSpec IVspec = new IvParameterSpec(IV);
    //4. init the cipher
    encryptCipher.init(Cipher.ENCRYPT_MODE, keyValue, IVspec);

    //1 create the cipher
    decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider());
    //2. the key is already created
    //3. the IV is already created
    //4. init the cipher
    decryptCipher.init(Cipher.DECRYPT_MODE, keyValue, IVspec);
}

From source file:com.aqnote.shared.cryptology.symmetric.AES.java

private static void generateCipher(String rawKey) {
    try {//from   w  w w  .jav  a2s .c om
        SecretKeySpec keySpec = new SecretKeySpec(rawKey.getBytes(ENCODE_UTF_8), CIPHER_NAME);
        encodeCipher = Cipher.getInstance(CIPHER_NAME);
        encodeCipher.init(Cipher.ENCRYPT_MODE, keySpec);

        decodeCipher = Cipher.getInstance(CIPHER_NAME);
        byte iv[] = encodeCipher.getIV();
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
        decodeCipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }

}

From source file:com.forsrc.utils.AesUtils.java

/**
 * Decrypt string./*ww w . ja v a2 s .c  om*/
 *
 * @param code the code
 * @return String string
 * @throws AesException the aes exception
 * @Title: decrypt
 * @Description:
 */
public String decrypt(String code) throws AesException {

    byte[] raw = null;
    try {
        raw = KEY.getBytes(CHARSET_ASCII);
    } catch (UnsupportedEncodingException e) {
        throw new AesException(e);
    }

    SecretKeySpec skeySpec = new SecretKeySpec(raw, SECRET_KEY);

    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance(CIPHER_KEY);
    } catch (NoSuchAlgorithmException e) {
        throw new AesException(e);
    } catch (NoSuchPaddingException e) {
        throw new AesException(e);
    }

    IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes());

    try {
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
    } catch (InvalidKeyException e) {
        throw new AesException(e);
    } catch (InvalidAlgorithmParameterException e) {
        throw new AesException(e);
    }

    byte[] encrypted = null;
    try {
        encrypted = new Base64().decode(code);
    } catch (Exception e) {
        throw new AesException(e);
    }

    byte[] original = null;
    try {
        original = cipher.doFinal(encrypted);
    } catch (IllegalBlockSizeException e) {
        throw new AesException(e);
    } catch (BadPaddingException e) {
        throw new AesException(e);
    }

    try {
        return new String(original, CHARSET_UTF8);
    } catch (UnsupportedEncodingException e) {
        throw new AesException(e);
    }

}

From source file:org.apache.spark.network.crypto.TransportCipher.java

private CryptoInputStream createInputStream(ReadableByteChannel ch) throws IOException {
    return new CryptoInputStream(cipher, conf, ch, key, new IvParameterSpec(inIv));
}

From source file:ie.peternagy.jcrypto.algo.AesWrapper.java

/**
 * Initialize the cipher//from  w  ww. j  a v a 2  s.  c  o  m
 *
 * @param isEncrypt - true >> encryption
 */
public void initCipher(boolean isEncrypt) {
    try {
        state = isEncrypt;
        cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
    } catch (GeneralSecurityException e) {
        System.err.println(e);
        throw new RuntimeException("Invalid environment, check max key size xx", e);
    }
}

From source file:com.titilink.camel.rest.util.PasswordUtils.java

/**
 * ??????// ww  w .j a  v  a  2 s  . co  m
 *
 * @param content 
 * @param ivparam ??
 */
private static void initParams(String content, byte[] ivparam) {
    checkParams(content, ivparam);

    if (null == skeySpec) {
        loadKey();
    }

    ivpspec = new IvParameterSpec(ivparam);
}