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.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java

public static String encrypt(String key, String toEncrypt) throws Exception {
    Key skeySpec = generateKeySpec(key);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", new BouncyCastleProvider());
    String abc = RandomStringUtils.randomAlphanumeric(16);
    System.out.println(abc);//from   w  ww  .  j a va 2 s  . c  o m
    byte[] ivBytes = abc.getBytes();
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
    byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());
    byte[] encryptedValue;
    encryptedValue = Base64.encodeBase64(encrypted);
    String result = new String();
    result += new String(encryptedValue);
    result = abc + result;
    System.out.println(result);
    return result;
}

From source file:com.slimroms.myapplication.MainActivity.java

private void extractAndDecryptAssets() {
    String toPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/extracted-assets";
    AssetManager am = getAssets();/* w ww. jav  a  2s  . co m*/
    SecretKey secret = new SecretKeySpec(BuildConfig.DECRYPTION_KEY, "AES");

    IvParameterSpec iv = new IvParameterSpec(BuildConfig.IV_KEY);

    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secret, iv);
        AssetUtils.copyAssetFolder(am, "testing", toPath, cipher);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.AES256Util.java

public String aesEncode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException,

        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,

        IllegalBlockSizeException, BadPaddingException {

    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");

    c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes()));

    byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));

    String enStr = new String(Base64.encodeBase64(encrypted));

    return enStr;

}

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

/**
 * Decrypt.//  w  w w  .  j a va  2  s .  c  o  m
 *
 * @param cipherText
 *            the cipher text
 * @return the string
 */
public static String decrypt(String cipherText) {
    try {
        byte[] cipherBytes = toBytes(cipherText);
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
        return new String(cipher.doFinal(cipherBytes), "UTF-8").trim();
    } catch (Exception e) {
        throw new JKSecurityException(e);
    }
}

From source file:io.kahu.hawaii.util.encryption.CryptoUtil.java

private static Cipher initCipher(int mode, String key, String initVector) throws GeneralSecurityException {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");

    SecretKeySpec secretKeySpec = new SecretKeySpec(hexStringToByteArray(key), "AES");
    IvParameterSpec initVectorSpec = new IvParameterSpec(hexStringToByteArray(initVector));
    cipher.init(mode, secretKeySpec, initVectorSpec);

    return cipher;
}

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

public static String decryptString(String encryptedText, String password)
        throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    String decrypted = null;//from   w w  w .j  a v  a2 s. c  o  m
    byte[] key = password.getBytes();
    if (key.length != 16) {
        throw new IllegalArgumentException("Invalid key size.");
    }
    byte[] value = Base64.decodeBase64(encryptedText);

    // Decrypt with AES/CBC/PKCS5Padding
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
    byte[] original = cipher.doFinal(value);
    decrypted = new String(original);

    return decrypted;
}

From source file:org.craftercms.commons.crypto.SimpleCipherTest.java

@Test
public void testDecryption() throws Exception {
    Key key = CryptoUtils.generateAesKey();
    byte[] iv = CryptoUtils.generateAesIv();

    Cipher encryptionCipher = Cipher.getInstance(CryptoUtils.DEFAULT_AES_CIPHER_TRANSFORMATION);
    encryptionCipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));

    String encrypted = Base64//  w w w .ja  v  a2s.  co m
            .encodeBase64String(encryptionCipher.doFinal(StringUtils.getBytesUtf8(CLEAR_TEXT)));

    SimpleCipher decryptionCipher = new SimpleCipher();
    decryptionCipher.setKey(key);
    decryptionCipher.setIv(iv);

    String clear = decryptionCipher.decryptBase64(encrypted);

    assertEquals(CLEAR_TEXT, clear);
}

From source file:org.emmanet.util.Encrypter.java

public Encrypter() {
    try {//www.  j a  v  a  2s. co m
        ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        //System.out.println("byte size of key == " + KEY.getBytes().length);
        //System.out.println("byte size of ivparam == " + IV.getBytes().length);
        SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes());
        ecipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        //ecipher.init(Cipher.ENCRYPT_MODE, key);
        //dcipher.init(Cipher.DECRYPT_MODE, key);
        dcipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

    } catch (InvalidKeyException ex) {
        Logger.getLogger(Encrypter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidAlgorithmParameterException ex) {
        Logger.getLogger(Encrypter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(Encrypter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchPaddingException ex) {
        Logger.getLogger(Encrypter.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.adaptris.security.password.AesCrypto.java

public String decode(String encrypted, String charset) throws PasswordException {

    String encryptedString = encrypted;
    String result;/*from w w  w  .  j  a v a2  s  .  c o  m*/

    if (encrypted.startsWith(Password.PORTABLE_PASSWORD)) {
        encryptedString = encrypted.substring(Password.PORTABLE_PASSWORD.length());
    }
    try {
        Input input = new Input(encryptedString);
        input.read();
        SecretKey sessionKey = new SecretKeySpec(input.getSessionKey(), ALG);
        Cipher cipher = Cipher.getInstance(CIPHER);
        if (input.getSessionVector() != null) {
            IvParameterSpec spec = new IvParameterSpec(input.getSessionVector());
            cipher.init(Cipher.DECRYPT_MODE, sessionKey, spec);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, sessionKey);
        }
        byte[] decrypted = cipher.doFinal(input.getEncryptedData());
        result = unseed(decrypted, charset);
    } catch (Exception e) {
        throw new PasswordException(e);
    }
    return result;
}

From source file:org.apache.ranger.authorization.hbase.Crypt.java

private Crypt() {
    try {/*from   w w  w . j a  v  a 2s . c om*/
        encrypter = Cipher.getInstance(CIPHER_ALGO);
        SecretKeySpec enckey = new SecretKeySpec(encryptionKey, CIPHER_INIT_ALGO);
        encrypter.init(Cipher.ENCRYPT_MODE, enckey, new IvParameterSpec(IV));

        descrypter = Cipher.getInstance(CIPHER_ALGO);
        SecretKeySpec deckey = new SecretKeySpec(encryptionKey, CIPHER_INIT_ALGO);
        descrypter.init(Cipher.DECRYPT_MODE, deckey, new IvParameterSpec(IV));
    } catch (Throwable t) {
        LOG.error("Unable to initialzie Encrypt/Decrypt module - Exiting from HBase", t);
        throw new RuntimeException(t);
    }
}