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.cloudant.sync.datastore.encryption.DPKEncryptionUtil.java

/**
 * Decrypt an AES encrypted byte array//  w w w  .  j av  a  2s .  co  m
 *
 * @param key            The encryption key
 * @param iv             The iv
 * @param encryptedBytes The data to decrypt
 * @return The decrypted data
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidAlgorithmParameterException
 * @throws InvalidKeyException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
public static byte[] decryptAES(SecretKey key, byte[] iv, byte[] encryptedBytes)
        throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivParameter = new IvParameterSpec(iv);
    // see http://stackoverflow.com/a/11506343
    Key encryptionKey = new SecretKeySpec(key.getEncoded(), "AES");
    aesCipher.init(Cipher.DECRYPT_MODE, encryptionKey, ivParameter);
    return aesCipher.doFinal(encryptedBytes);
}

From source file:org.apache.spark.network.sasl.aes.AesCipher.java

public AesCipher(AesConfigMessage configMessage, TransportConf conf) throws IOException {
    this.properties = conf.cryptoConf();
    this.inKeySpec = new SecretKeySpec(configMessage.inKey, "AES");
    this.inIvSpec = new IvParameterSpec(configMessage.inIv);
    this.outKeySpec = new SecretKeySpec(configMessage.outKey, "AES");
    this.outIvSpec = new IvParameterSpec(configMessage.outIv);
}

From source file:org.matrix.security.crypto.encrypt.AesBytesEncryptor.java

public byte[] decrypt(byte[] encryptedBytes) {
    synchronized (decryptor) {
        byte[] iv = iv(encryptedBytes);
        initCipher(decryptor, Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
        return doFinal(decryptor,
                ivGenerator != NULL_IV_GENERATOR ? encrypted(encryptedBytes, iv.length) : encryptedBytes);
    }/*  w w w  .  jav  a 2s  .  co m*/
}

From source file:org.tomcatltpa.utils.LTPAUtils.java

public String decrypt(String ltpaToken) throws GeneralSecurityException {
    byte[] tokenBytes = Base64.decodeBase64(ltpaToken.getBytes());
    SecretKey sKey = new SecretKeySpec(secretKey, 0, 16, "AES");
    Cipher cipher = Cipher.getInstance(AES_DECRIPTING_ALGORITHM_ZERO_PADDING);
    IvParameterSpec ivs16 = new IvParameterSpec(Arrays.copyOf(secretKey, 16));
    cipher.init(Cipher.DECRYPT_MODE, sKey, ivs16);
    return new String(cipher.doFinal(tokenBytes));
}

From source file:com.springcryptoutils.core.cipher.symmetric.CiphererWithStaticKeyImpl.java

/**
 * A base64 encoded representation of the raw byte array containing the
 * initialization vector.//from   w w w. j  a  v a  2 s . co  m
 *
 * @param initializationVector the initialization vector
 * @throws SymmetricEncryptionException on runtime errors
 */
public void setInitializationVector(String initializationVector) {
    try {
        this.initializationVectorSpec = new IvParameterSpec(
                Base64.decodeBase64(initializationVector.getBytes("UTF-8")));
    } catch (UnsupportedEncodingException e) {
        throw new SymmetricEncryptionException("UTF-8 is an unsupported encoding on this platform", e);
    }
}

From source file:org.cogroo.addon.util.SecurityUtil.java

private byte[] encrypt(byte[] secretKey, String data) throws InvalidKeyException {
    byte[] encryptedData = null;
    try {//from  w  w w .  j av a 2s  . c o m
        // Encrypt data using the secret key
        Cipher aescf = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec ivspec = new IvParameterSpec(new byte[16]);
        aescf.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(secretKey, "AES"), ivspec);
        encryptedData = aescf.doFinal(data.getBytes(UTF8));
    } catch (Exception e) {
        LOG.log(Level.SEVERE, "Exception encrypting data", e);
    }
    return encryptedData;
}

From source file:org.uberfire.security.server.crypt.DefaultCryptProvider.java

private Cipher buildCipher(final Object salt, final int mode) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
    final MessageDigest md = MessageDigest.getInstance(HASH_FUNCTION);
    if (salt != null) {
        md.update(salt.toString().getBytes(UTF8));
    }/*  w  w w . j  a v a2  s . c o  m*/
    final byte[] digestOfPassword = md.digest(KEY.getBytes(UTF8));
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
        keyBytes[k++] = keyBytes[j++];
    }

    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
    final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");

    cipher.init(mode, key, iv);

    return cipher;
}

From source file:org.opendatakit.briefcase.util.CipherFactory.java

public Cipher getCipher(String context) throws InvalidKeyException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, NoSuchPaddingException {
    ++ivSeedArray[ivCounter % ivSeedArray.length];
    ++ivCounter;// ww w.jav  a2s.  c  om
    IvParameterSpec baseIv = new IvParameterSpec(ivSeedArray);
    Cipher c = Cipher.getInstance(SYMMETRIC_ALGORITHM);

    c.init(Cipher.DECRYPT_MODE, symmetricKey, baseIv);
    return c;
}

From source file:spacetraffic.kiv.zcu.cz.gameelement.MinigamePasswordHasher.java

/**
 * Method for getting Cipher./*from  w  w w .  j av a2  s.com*/
 * @param mode mode
 * @return cipher
 * @throws Exception
 */
private Cipher getCipher(int mode) throws Exception {
    Cipher cipher = Cipher.getInstance(TRANSFORMATION);

    byte[] initVector = INIT_VECTOR.getBytes(CHARSET);
    cipher.init(mode, generateKey(), new IvParameterSpec(initVector));

    return cipher;
}

From source file:hjow.hgtable.util.SecurityUtil.java

/**
 * <p>? .</p>/*from   www  .  j a  va  2s. com*/
 * 
 * @param text : ?? ? ?
 * @param key : ? ? 
 * @param algorithm :   (null  AES )
 * @return ? ?
 */
public static String encrypt(String text, String key, String algorithm) {
    try {
        String passwords = hash(key, null);
        String methods = algorithm;
        if (methods == null)
            methods = "AES";

        String paddings;
        int need_keySize = -1;
        boolean useIv = false;

        byte[] befores = text.trim().getBytes("UTF-8");
        byte[] keyByte = passwords.getBytes("UTF-8");

        if (methods.equalsIgnoreCase("DES")) {
            paddings = "DES/CBC/PKCS5Padding";
            need_keySize = 8;
            useIv = true;
        } else if (methods.equalsIgnoreCase("DESede")) {
            paddings = "TripleDES/ECB/PKCS5Padding";
            need_keySize = 24;
            useIv = true;
        } else if (methods.equalsIgnoreCase("AES")) {
            paddings = "AES";
            need_keySize = 16;
            useIv = false;
        } else
            return null;

        byte[] checkKeyByte = new byte[need_keySize];
        byte[] ivBytes = new byte[checkKeyByte.length];

        for (int i = 0; i < checkKeyByte.length; i++) {
            if (i < keyByte.length) {
                checkKeyByte[i] = keyByte[i];
            } else {
                checkKeyByte[i] = 0;
            }
        }
        keyByte = checkKeyByte;

        SecretKeySpec keySpec = new SecretKeySpec(keyByte, algorithm);
        IvParameterSpec ivSpec = null;
        if (useIv)
            ivSpec = new IvParameterSpec(ivBytes);

        Cipher cipher = null;
        byte[] outputs;

        try {
            cipher = Cipher.getInstance(paddings);
            if (useIv) {
                cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
            } else {
                cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            }

            outputs = new byte[cipher.getOutputSize(befores.length)];
            for (int i = 0; i < outputs.length; i++) {
                outputs[i] = 0;
            }
            int enc_len = cipher.update(befores, 0, befores.length, outputs, 0);
            enc_len = enc_len + cipher.doFinal(outputs, enc_len);

            return new String(Base64.encodeBase64(outputs), "UTF-8");
        } catch (Throwable e) {
            Main.logError(e, Manager.applyStringTable("On encrypting"));
            return null;
        }
    } catch (Throwable e) {

    }
    return null;
}