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:dz.alkhwarizmix.framework.java.utils.CryptoUtil.java

/**
 * Constructor.//  w  ww  . j  a va2s . c om
 */
public CryptoUtil(String pKey, String pEncType, String pModeType, boolean pSimple, String pPaddingType) {

    if (pEncType == null)
        pEncType = "aes";
    pEncType = pEncType.toUpperCase();

    if (pModeType == null)
        pModeType = "cbc"; // ecb, cbc, ofb
    pModeType = pModeType.toUpperCase();

    if (pPaddingType == null)
        pPaddingType = "pkcs5";

    String pHexKey = stringToHex(pKey);
    pHexKey = pHexKey.substring(0, 64);
    String pHexIV = pHexKey.substring(0, 32);

    byte[] kdata = hex2Byte(pHexKey);
    String pad = ((pPaddingType == "pkcs5") ? "PKCS5" : "No") + "Padding";
    String algo = pEncType + "/" + pModeType + "/" + pad;
    secretKeySpec = new SecretKeySpec(kdata, pEncType);
    ivParameterSpec = new IvParameterSpec(hex2Byte(pHexIV));
    try {
        cipher = Cipher.getInstance(algo);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }
}

From source file:tk.playerforcehd.networklib.shared.utils.StringCryptionUtils.java

/**
 * Decrypt a String which is encrypted with AES | You MUST use a 128 bit key!
 *
 * @param value The String to decrypt//from  w  ww  .  j  a v a  2 s  .  co  m
 * @param key   The key for the decryption
 * @return The decrypted String
 * @throws InvalidAlgorithmParameterException -
 * @throws InvalidKeyException                -
 * @throws NoSuchPaddingException             -
 * @throws NoSuchAlgorithmException           -
 * @throws UnsupportedEncodingException       -
 * @throws BadPaddingException                -
 * @throws IllegalBlockSizeException          -
 */
public static String decrypt(String value, String key)
        throws InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException,
        NoSuchAlgorithmException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
    IvParameterSpec iv = new IvParameterSpec("9rh5os4n8m24gu9e".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(value));
    return new String(original);
}

From source file:com.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:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java

public static byte[] encrypt2(byte[] encData, byte[] key, byte[] iv) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
    return cipher.doFinal(encData);
}

From source file:com.confighub.core.security.Encryption.java

private static String encryptShared(CipherTransformation ct, String decrypted, String secret)
        throws ConfigException {
    if (null == decrypted)
        return null;

    try {/*from  w  ww . j a v  a  2 s  . com*/
        Cipher cipher = Cipher.getInstance(ct.getName());
        final int blockSize = cipher.getBlockSize();

        SecretKeySpec sharedKey = new SecretKeySpec(getKeyAsBytes(secret, ct.getKeyLen()), ct.getAlgo());

        if ("CBC".equals(ct.getMode()))
            cipher.init(Cipher.ENCRYPT_MODE, sharedKey, new IvParameterSpec(getIV(blockSize)));
        else
            cipher.init(Cipher.ENCRYPT_MODE, sharedKey);

        byte[] encrypted = cipher.doFinal(Utils.isBlank(decrypted) ? new byte[0] : decrypted.getBytes("UTF8"));
        return Base64.encodeBase64String(encrypted);
    } catch (Exception e) {
        throw new ConfigException(Error.Code.ENCRYPTION_ERROR);
    }
}

From source file:com.gvmax.common.util.Enc.java

public String encrypt(String valueToEnc) {
    if (!enabled) {
        return valueToEnc;
    }/* w  w  w. j a  va 2 s.c o m*/
    if (valueToEnc == null) {
        return null;
    }
    try {
        c.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
        byte[] encValue = c.doFinal(valueToEnc.getBytes("UTF-8"));
        return Base64.encodeBase64String(encValue);
    } catch (Exception e) {
        logger.warn("Unable to encrypt: " + valueToEnc, e);
        return null;
    }
}

From source file:de.adorsys.morphiaencryption.AES256CryptoProvider.java

protected IvParameterSpec getIV() {
    IvParameterSpec ivParameterSpec = new IvParameterSpec(new byte[16]);
    return ivParameterSpec;
}

From source file:com.eucalyptus.auth.euare.EuareServerCertificateUtil.java

public static String getEncryptedKey(final String certArn, final String certPem) throws AuthException {
    final ServerCertificate targetCert = lookupServerCertificate(certArn);
    // generate symmetric key
    final MessageDigest digest = Digest.SHA256.get();
    final byte[] salt = new byte[32];
    Crypto.getSecureRandomSupplier().get().nextBytes(salt);
    digest.update(salt);//from   w w w  .  j a  v a 2  s  .  c  om
    final SecretKey symmKey = new SecretKeySpec(digest.digest(), "AES");

    try {
        // encrypt the server pk using symm key
        Cipher cipher = Ciphers.AES_CBC.get();
        final byte[] iv = new byte[16];
        Crypto.getSecureRandomSupplier().get().nextBytes(iv);
        cipher.init(Cipher.ENCRYPT_MODE, symmKey, new IvParameterSpec(iv),
                Crypto.getSecureRandomSupplier().get());
        final byte[] cipherText = cipher.doFinal(Base64.encode(targetCert.getPrivateKey().getBytes()));
        final String encPrivKey = new String(Base64.encode(Arrays.concatenate(iv, cipherText)));

        // encrypt the symmetric key using the certPem
        X509Certificate x509Cert = PEMFiles.getCert(B64.standard.dec(certPem));
        cipher = Ciphers.RSA_PKCS1.get();
        cipher.init(Cipher.ENCRYPT_MODE, x509Cert.getPublicKey(), Crypto.getSecureRandomSupplier().get());
        byte[] symmkey = cipher.doFinal(symmKey.getEncoded());
        final String b64SymKey = new String(Base64.encode(symmkey));

        return String.format("%s\n%s", b64SymKey, encPrivKey);
    } catch (final Exception ex) {
        throw Exceptions.toUndeclared(ex);
    }
}

From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java

public static String decrypt(String key, String encrypted) throws Exception {
    Key skeySpec = generateKeySpec(key);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", new BouncyCastleProvider());
    String abc = encrypted.substring(0, 16);
    System.out.println(abc);/*from   www.j av  a 2  s  .  c om*/
    byte[] ivBytes = abc.getBytes();
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
    //cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decodedBytes = Base64.decodeBase64(encrypted.substring(16).getBytes());
    byte[] original = cipher.doFinal(decodedBytes);
    return new String(original);
}

From source file:org.mozilla.android.sync.Cryptographer.java

public static byte[] decrypt(CryptoInfo info) {

    // Check HMAC
    if (!verifyHmac(info)) {
        return null;
    }/*  w  w  w.  j  av  a  2 s . c o m*/

    Cipher cipher = getCipher();
    try {
        cipher.init(Cipher.DECRYPT_MODE,
                new SecretKeySpec(info.getKeys().getEncryptionKey(), KEY_ALGORITHM_SPEC),
                new IvParameterSpec(info.getIv()));
    } catch (InvalidKeyException e) {
        e.printStackTrace();
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
        return null;
    }

    return commonCrypto(cipher, info.getMessage());
}