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

public static String EncryptText(String RawText) {
    String EncText = "";
    byte[] keyArray = new byte[24];
    byte[] temporaryKey;
    String key = "developersnotedotcom";
    byte[] toEncryptArray = null;

    try {/*from  w  ww  . j av  a 2s .  c  o m*/

        toEncryptArray = RawText.getBytes("UTF-8");
        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.ENCRYPT_MODE, new SecretKeySpec(keyArray, "DESede"), new IvParameterSpec(sharedvector));
        byte[] encrypted = c.doFinal(toEncryptArray);
        EncText = Base64.encodeBase64String(encrypted);

    } catch (Exception NoEx) {
    }

    return EncText;
}

From source file:org.sakuli.services.cipher.AesCbcCipher.java

public static IvParameterSpec createIV(final int ivSizeBytes, final Optional<SecureRandom> rng) {
    final byte[] iv = new byte[ivSizeBytes];
    final SecureRandom theRNG = rng.orElse(new SecureRandom());
    theRNG.nextBytes(iv);/*  ww  w . j  a v  a2 s.com*/
    return new IvParameterSpec(iv);
}

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

/**
 * //from   w  ww . j av  a2  s  . c  o  m
 * 
 * @param sSrc 
 * @param sKey KEY
 * @return
 * @throws Exception
 * @author cdduqiang
 * @date 201443
 */
public static String encrypt(String sSrc, String sKey) throws Exception {
    if (sKey == null) {
        log.error("Encrypt Key ??");
        throw new Exception("Encrypt 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.ENCRYPT_MODE, skeySpec, iv);
    byte[] anslBytes = sSrc.getBytes(ENCODING);// string2AnslBytes(sSrc);
    byte[] encrypted = cipher.doFinal(anslBytes);
    return byte2hex(encrypted).toUpperCase();
}

From source file:com.hp.application.automation.tools.EncryptionUtils.java

public static String Decrypt(String text, String key) throws Exception {

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    byte[] keyBytes = new byte[16];
    byte[] b = key.getBytes("UTF-8");
    int len = b.length;
    if (len > keyBytes.length)
        len = keyBytes.length;/*from w w w .  j a  v a  2  s . c o m*/
    System.arraycopy(b, 0, keyBytes, 0, len);
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
    byte[] results = cipher.doFinal(Base64.decodeBase64(text));

    return new String(results, "UTF-8");
}

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

public static String decrypt(String encrypted) {
    try {/*from   w  w  w.  ja  v  a 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.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:truelauncher.utils.CryptoUtils.java

public static String decryptString(String string)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, ClassNotFoundException, IOException, InvalidAlgorithmParameterException {
    byte[] keyBytes = getKey();
    SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
    String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(string)));
    return decryptedString;
}

From source file:aajavafx.Kripto.java

public String encrypt(String plainText) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
    byte[] byteCipher = cipher.doFinal(plainText.getBytes("UTF-8"));
    Base64 base64 = new Base64();
    String stringToStore = new String(base64.encode(byteCipher));
    //byte[] restoredBytes = Base64.decode(stringToStore.getBytes());
    return stringToStore;
}

From source file:org.cryptomath.function.AESCryptoFunction.java

@Override
public String encrypt(final String message, final String keyAlias, final String ivAlias) throws Exception {
    AESConfig config = CryptoConfigSpec.getInstance().getAesConfig();
    Cipher cipher = Cipher.getInstance(config.getAlgorithm(), "BC");

    AESFunctionUtil util = new AESFunctionUtil();
    IvParameterSpec iv = new IvParameterSpec(util.getIV(ivAlias));

    SecretKeySpec keySpec = new SecretKeySpec(util.getSecretKey(keyAlias), config.getScheme());

    cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);

    byte[] cipherText = cipher.doFinal(message.getBytes());

    return new String(Base64.encodeBase64(cipherText));
}

From source file:com.bytecode.util.Crypto.java

private static byte[] encrypt(String keystring, String message, int bits) throws Exception {
    byte[] encValue = null;
    SecureRandom random = new SecureRandom();
    byte[] nonceBytes = new byte[8];
    random.nextBytes(nonceBytes);//from w w w .  j  av  a2  s  .com
    IvParameterSpec nonce = new IvParameterSpec(Arrays.copyOf(nonceBytes, 16));

    Key key = generateKey(keystring, bits);
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key, nonce);
    byte[] ciphertextWithoutNonce = c.doFinal(message.getBytes("UTF-8"));
    encValue = Arrays.copyOf(nonceBytes, nonceBytes.length + ciphertextWithoutNonce.length);
    for (int i = 0; i < ciphertextWithoutNonce.length; i++) {
        encValue[i + 8] = ciphertextWithoutNonce[i];
    }

    return encValue;
}

From source file:com.netsteadfast.greenstep.util.EncryptorUtils.java

public static String decrypt(String key1, String iv1, String encrypted) {
    try {/*from  ww  w.  j av  a  2 s .  c o m*/
        IvParameterSpec iv = new IvParameterSpec(iv1.getBytes(Constants.BASE_ENCODING));
        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes(Constants.BASE_ENCODING), "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;
}