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:de.marius_oe.cfs.cryption.Crypter.java

/**
 * Returns the {@link Cipher} for the encryption and decryption process.
 * //from w  ww.  ja  v  a2 s  .  co m
 * @param mode
 *            {@link Cipher.DECRYPT_MODE} or {@link Cipher.ENCRYPT_MODE}
 * @param iv
 *            the initial vector which should be used in the cipher. if the
 *            iv is <code>null</code>, a new iv will be generated.
 * @return {@link Cipher} object
 */
private static Cipher getCipher(int mode, byte[] iv) {
    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        if (iv == null) {
            cipher.init(mode, KeyManager.instance().getKey());
        } else {
            IvParameterSpec parameterSpec = new IvParameterSpec(iv);
            cipher.init(mode, KeyManager.instance().getKey(), parameterSpec);
        }
        return cipher;
    } catch (InvalidKeyException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

From source file:com.myapp.common.AES4MEncrypt.java

/**
 * //from w w w . jav a2s  .c om
 * 
 * @param sSrc ?
 * @param sKey KEY
 * @return
 * @throws Exception
 * @author cdduqiang
 * @date 201443
 */
public static String decrypt(String sSrc, String sKey) throws Exception {
    if (sKey == null) {
        log.error("Decrypt Key ??");
        throw new Exception("Decrypt Key ??");
    }

    byte[] raw = hex2byte(sKey);
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
    byte[] encrypted1 = hex2byte(sSrc);

    byte[] original = cipher.doFinal(encrypted1);
    return new String(original, ENCODING);// anslBytes2String(original);
}

From source file:Main.java

/**
 * More flexible AES decrypt that doesn't encode
 *
 * @param key AES key typically 128, 192 or 256 bit
 * @param iv Initiation Vector/* www. j  a v a  2s.  c om*/
 * @param decodedCipherText in bytes (assumed it's already been decoded)
 * @return Decrypted message cipher text (not encoded)
 * @throws GeneralSecurityException if something goes wrong during encryption
 */
public static byte[] decrypt(final SecretKeySpec key, final byte[] iv, final byte[] decodedCipherText)
        throws GeneralSecurityException {
    final Cipher cipher = Cipher.getInstance(AES_MODE);
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
    byte[] decryptedBytes = cipher.doFinal(decodedCipherText);

    log("decryptedBytes", decryptedBytes);

    return decryptedBytes;
}

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

public static String decrypt(String key, String initVector, String encrypted) {
    try {// w  w  w.jav a 2  s  . co 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.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.cloudant.sync.datastore.encryption.DPKEncryptionUtil.java

/**
 * AES Encrypt a byte array/*from  w  w w  . jav  a 2 s. c  o  m*/
 *
 * @param key              The encryption key
 * @param iv               The iv
 * @param unencryptedBytes The data to encrypt
 * @return The encrypted data
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidAlgorithmParameterException
 * @throws InvalidKeyException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
public static byte[] encryptAES(SecretKey key, byte[] iv, byte[] unencryptedBytes)
        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.ENCRYPT_MODE, encryptionKey, ivParameter);
    return aesCipher.doFinal(unencryptedBytes);
}

From source file:com.amazonaws.cognito.devauthsample.AESEncryption.java

private static byte[] encrypt(String clearText, String key, byte[] iv) {
    try {//from  www  .j av  a2s.co m
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        cipher.init(Cipher.ENCRYPT_MODE, getKey(key), params);
        return cipher.doFinal(clearText.getBytes());
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to encrypt.", e);
    }
}

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:OfficeAdministrator.Encrypt.java

public static String DecryptText(String EncText) {

    String RawText = "";
    byte[] keyArray = new byte[24];
    byte[] temporaryKey;
    String key = "developersnotedotcom";
    byte[] toEncryptArray = null;

    try {/*from w  w w .  ja v  a2  s  .  c om*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        temporaryKey = m.digest(key.getBytes("UTF-8"));

        if (temporaryKey.length < 24) // DESede require 24 byte length key
        {
            int index = 0;
            for (int i = temporaryKey.length; i < 24; i++) {
                keyArray[i] = temporaryKey[index];
            }
        }

        Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyArray, "DESede"), new IvParameterSpec(sharedvector));
        byte[] decrypted = c.doFinal(Base64.decodeBase64(EncText));

        RawText = new String(decrypted, "UTF-8");
    } catch (Exception NoEx) {
        JOptionPane.showMessageDialog(null, NoEx);
    }

    return RawText;

}

From source file:Logi.GSeries.Libraries.Encryption.java

public static String decrypt(String encryptedString, String password) {
    try {//from w  w w  .j a  v a2  s  .c  o  m
        byte[] encryptedWithIV = Base64.decodeBase64(encryptedString);
        byte initialVector[] = new byte[16];
        byte[] encrypted = new byte[encryptedWithIV.length - initialVector.length];
        System.arraycopy(encryptedWithIV, 0, encrypted, 0, encrypted.length);
        System.arraycopy(encryptedWithIV, encrypted.length, initialVector, 0, initialVector.length);
        IvParameterSpec ivspec = new IvParameterSpec(initialVector);
        SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);
        byte[] original = cipher.doFinal(encrypted);
        return new String(original);
    } catch (Exception ex) {
        Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        return "Error";
    }
}

From source file:ch.newscron.encryption.Encryption.java

/**
 * Given a JSONObject, and maybe an hash, it is encoded and returned as a String.
 * @param inviteData is a JSONObject having the data with the keys "customerId", "rew1", "rew2" and "val"
 * @param md5Hash is a String that substitute the hash computed using md5 algorithm, in case it is not null and not empty
 * @return encoded string //from ww  w .  j av a  2  s . co  m
 */
protected static String encode(JSONObject inviteData, String md5Hash) {

    try {
        //Check in case we want to add a given hash (having at the end a corrupt data)
        if (md5Hash != null && !md5Hash.isEmpty()) {
            //Add md5Hash to JSONObject
            inviteData.put("hash", md5Hash);
        } else {
            return null;
        }

        //Encode inviteData (with hash) JSONObject
        String params = inviteData.toString();
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey,
                new IvParameterSpec(initializationVector.getBytes("UTF-8")));
        return Base64.encodeBase64URLSafeString(cipher.doFinal(params.getBytes())); //to ensure valid URL characters
    } catch (Exception e) {
    }

    return null;
}