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.blackcrowsys.sinscrypto.AesEncryptor.java

@Override
public String encrypt(String secretkey, String iv, String toEncrypt)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, InvalidAlgorithmParameterException, DecoderException {
    Cipher cipher = Cipher.getInstance(AESMODE);
    SecretKeySpec secretKeySpec = new SecretKeySpec(secretkey.getBytes(), AES);
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(Hex.decodeHex(iv.toCharArray())));
    return Base64.encodeBase64String(cipher.doFinal(toEncrypt.getBytes()));
}

From source file:truelauncher.utils.CryptoUtils.java

public static String encryptString(String string)
        throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException,
        BadPaddingException, IOException, ClassNotFoundException, 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.ENCRYPT_MODE, key, ivSpec);
    String encryptedString = Base64.encodeBase64String(cipher.doFinal(string.getBytes("UTF8")));
    return encryptedString;
}

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

@Override
public String decrypt(String message, String keyAlias, 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.DECRYPT_MODE, keySpec, iv);

    byte[] plainText = cipher.doFinal(Base64.decodeBase64(message.getBytes()));

    return new String(plainText);
}

From source file:com.app.utils.StringEncrypt.java

/**
 * Funcin de tipo String que recibe una llave (key), un vector de inicializacin (iv)
 * y el texto que se desea descifrar//  w  ww  .j  a  v  a 2  s  .c  o  m
 * @param key la llave en tipo String a utilizar
 * @param iv el vector de inicializacin a utilizar
 * @param encrypted el texto cifrado en modo String
 * @return el texto desencriptado en modo String
 * @throws Exception puede devolver excepciones de los siguientes tipos: NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException
 */
public static String decrypt(String key, String iv, String encrypted) throws Exception {
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    byte[] enc = decodeBase64(encrypted);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] decrypted = cipher.doFinal(enc);
    return new String(decrypted);
}

From source file:aajavafx.Kripto.java

public String decrypt(String cipherText) throws Exception {
    Base64 decoder = new Base64();
    byte[] decodedBytes = org.apache.commons.codec.binary.Base64.decodeBase64(cipherText.getBytes());
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    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(decodedBytes), "UTF-8");
}

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

@Test
public void testEncryption() throws Exception {
    SimpleCipher encryptionCipher = new SimpleCipher();
    String encrypted = encryptionCipher.encryptBase64(CLEAR_TEXT);

    Key key = encryptionCipher.getKey();
    byte[] iv = encryptionCipher.getIv();

    Cipher decryptionCipher = Cipher.getInstance(CryptoUtils.DEFAULT_AES_CIPHER_TRANSFORMATION);
    decryptionCipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));

    byte[] clearBytes = decryptionCipher.doFinal(Base64.decodeBase64(encrypted));
    String clear = StringUtils.newStringUtf8(clearBytes);

    assertEquals(CLEAR_TEXT, clear);/*  w  w w . ja  v  a  2 s .  co m*/
}

From source file:adminpassword.Decryption.java

@SuppressWarnings("static-access")
public String decrypt(String encryptedText, String idKey) throws Exception {
    String password = idKey;//from   ww  w  .  ja  va  2 s . co m

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

    //strip off the salt and iv
    ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText));
    byte[] saltBytes = new byte[20];
    buffer.get(saltBytes, 0, saltBytes.length);
    byte[] ivBytes1 = new byte[cipher.getBlockSize()];
    buffer.get(ivBytes1, 0, ivBytes1.length);
    byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length];
    buffer.get(encryptedTextBytes);

    // Deriving the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1));

    byte[] decryptedTextBytes = null;
    try {
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);

    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return new String(decryptedTextBytes);
}

From source file:com.haulmont.timesheets.EncryptDecrypt.java

public EncryptDecrypt(String key) {
    try {//from  w ww. j a v a2s.  c  o m
        String data = new StringBuilder(SALT + key).reverse().toString();
        SecretKeySpec secretKey = new SecretKeySpec(DigestUtils.md5(data), "AES");
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(INIT_VECTOR);
        eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        eCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        dCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
    } catch (Exception e) {
        throw new RuntimeException("Exception while init cipher:", e);
    }
}

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

private static byte[] decrypt(String keystring, byte[] message, int bits) throws Exception {
    byte[] decValue = null;
    byte[] nonceBytes = Arrays.copyOf(Arrays.copyOf(message, 8), 16);
    IvParameterSpec nonce = new IvParameterSpec(nonceBytes);

    Key key = generateKey(keystring, bits);
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key, nonce);
    decValue = c.doFinal(message, 8, message.length - 8);

    return decValue;
}

From source file:com.salesmanager.core.util.EncryptionUtil.java

public static String encrypt(String key, String value) throws Exception {

    // value = StringUtils.rightPad(value, 16,"*");
    // Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    // NEED TO UNDERSTAND WHY PKCS5Padding DOES NOT WORK
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
    IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
    byte[] inpbytes = value.getBytes();
    byte[] encrypted = cipher.doFinal(inpbytes);
    return new String(bytesToHex(encrypted));

}