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:org.soulwing.credo.service.crypto.jca.JcaAESEncryptedSecretKeyWrapper.java

@Override
protected Cipher createCipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidParameterSpecException, InvalidAlgorithmParameterException {
    Cipher cipher = Cipher.getInstance(transform);
    cipher.init(Cipher.UNWRAP_MODE, getKey(), new IvParameterSpec(iv));
    return cipher;
}

From source file:com.baran.crypto.CryptoDES.java

public byte[] encrypt(File file, String trivia) throws Exception {
    final MessageDigest md = MessageDigest.getInstance("md5");
    // digest the trivia password in UTF-8
    final byte[] digestTrivia = md.digest(trivia.getBytes("utf-8"));

    // truncating digest
    final byte[] keyBytes = Arrays.copyOf(digestTrivia, 24);
    for (int j = 0, k = 16; j < 8;) {
        keyBytes[k++] = keyBytes[j++];/*from  w  w w  .j av  a  2  s  .c  o m*/
    }
    // DESede setting
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");

    // CBC IV setting
    final IvParameterSpec iv = new IvParameterSpec(new byte[8]);

    final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, iv);

    String allInOne = FileUtils.readFileToString(file, "utf-8");

    final byte[] plainTextBytes = allInOne.getBytes("utf-8");
    final byte[] cipherText = cipher.doFinal(plainTextBytes);

    return cipherText;
}

From source file:com.thoughtworks.go.security.AESEncrypter.java

@Override
public String encrypt(String plainText) throws CryptoException {
    try {/*  w  w w  .ja  va2  s  . c om*/
        byte[] initializationVector = getIvProviderInstance().createIV();
        Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        encryptCipher.init(Cipher.ENCRYPT_MODE, createSecretKeySpec(),
                new IvParameterSpec(initializationVector));

        byte[] bytesToEncrypt = plainText.getBytes(StandardCharsets.UTF_8);
        byte[] encryptedBytes = encryptCipher.doFinal(bytesToEncrypt);

        return String.join(":", "AES", ENCODER.encodeToString(initializationVector),
                ENCODER.encodeToString(encryptedBytes));
    } catch (Exception e) {
        throw new CryptoException(e);
    }
}

From source file:com.dasol.util.AES256Util.java

public String aesDecode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException {
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes("UTF-8")));

    byte[] byteStr = Base64.decodeBase64(str.getBytes());

    return new String(c.doFinal(byteStr), "UTF-8");
}

From source file:com.github.sshw.crypt.EncryptionBean.java

public String decrypt(String message, String password) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    Key key = keyFromPassword(password);
    byte[] cb = Base64.decodeBase64(message);
    IvParameterSpec ivb = new IvParameterSpec(key.getEncoded());
    cipher.init(Cipher.DECRYPT_MODE, key, ivb);
    String plaintext = new String(cipher.doFinal(cb));
    return plaintext;
}

From source file:org.toffi.domainmodel.encryptor.impl.AESEncryptorImpl.java

@Override
public byte[] encrypt(byte[] data) {
    try {/*w  w  w.j  a v  a  2s.  c om*/
        // generate random initialization vector
        byte[] iv = new byte[ivLen];
        synchronized (random) {
            random.nextBytes(iv);
        }

        // encrypt
        IvParameterSpec paramSpec = new IvParameterSpec(iv);
        byte[] encryptedMessage = null;
        synchronized (encCipher) {
            encCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            encryptedMessage = encCipher.doFinal(data);
        }
        // finalMessage = iv + encryptedMessage
        byte[] finalMessage = new byte[encryptedMessage.length + ivLen];
        System.arraycopy(iv, 0, finalMessage, 0, ivLen);
        System.arraycopy(encryptedMessage, 0, finalMessage, ivLen, encryptedMessage.length);

        return finalMessage;
    } catch (Exception e) {
        LOG.error("Unable to decrypt input message", e);
        throw new EncryptorException("Encryption error!", e);
    }
}

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

public byte[] encrypt(byte[] bytes) {
    synchronized (encryptor) {
        byte[] iv = ivGenerator.generateKey();
        initCipher(encryptor, Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv));
        byte[] encrypted = doFinal(encryptor, bytes);
        return ivGenerator != NULL_IV_GENERATOR ? concatenate(iv, encrypted) : encrypted;
    }/*from w  w w. ja v  a  2  s. c  o m*/
}

From source file:net.navasoft.madcoin.backend.services.security.Encrypter.java

/**
 * Instantiates a new encrypter.//from  w ww . ja  v a  2 s .  c om
 * 
 * @param keyString
 *            the key string
 * @param ivString
 *            the iv string
 * @since 5/08/2014, 08:03:33 PM
 */
public Encrypter(String keyString, String ivString) {
    try {
        final MessageDigest md = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = md.digest(Base64.decodeBase64(keyString.getBytes("utf-8")));
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        keySpec = new DESedeKeySpec(keyBytes);

        key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);

        iv = new IvParameterSpec(ivString.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:io.hawkcd.agent.services.SecurityService.java

private Cipher getCipher(int mode) throws Exception {
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    byte[] iv = getBytes(IV);
    c.init(mode, generateKey(), new IvParameterSpec(iv));
    return c;/*from ww w.j a  va 2s.c  o m*/
}

From source file:com.aqnote.shared.cryptology.symmetric.Blowfish.java

public Blowfish(String keySpec, byte[] paramSpec) {
    this.keySpec = new SecretKeySpec(keySpec.getBytes(), ALGORITHM);
    this.paramSpec = new IvParameterSpec(paramSpec);
    initBlowfish();//from  w w w.j  av a 2s.c o  m
}