Example usage for javax.crypto Cipher doFinal

List of usage examples for javax.crypto Cipher doFinal

Introduction

In this page you can find the example usage for javax.crypto Cipher doFinal.

Prototype

public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException 

Source Link

Document

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

Usage

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

public static String encrypt(String value) {
    try {/* w ww . j  a  v a 2  s  .com*/
        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.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(value.getBytes());
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] TriDesDecryption(byte[] byteKey, byte[] dec) {
    try {/*from   w  ww  . j  av a 2  s.  co m*/
        byte[] en_key = new byte[24];
        if (byteKey.length == 16) {
            System.arraycopy(byteKey, 0, en_key, 0, 16);
            System.arraycopy(byteKey, 0, en_key, 16, 8);
        }
        SecretKey key = new SecretKeySpec(en_key, "DESede");
        Cipher dcipher = Cipher.getInstance("DESede/ECB/NoPadding");
        dcipher.init(Cipher.DECRYPT_MODE, key);

        byte[] de_b = dcipher.doFinal(dec);

        return de_b;
    } catch (Exception e) {
        e.printStackTrace();

    }
    return null;
}

From source file:com.esri.geoevent.datastore.Crypto.java

static public String doEncrypt(String stringToEncrypt) throws GeneralSecurityException {
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, encryptionKey);
    byte[] encVal = c.doFinal(stringToEncrypt.getBytes());
    String encodedEncryptedString = new String(Base64.encodeBase64(encVal));
    return encodedEncryptedString;
}

From source file:Main.java

public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(encryptKey.getBytes()));

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

    return cipher.doFinal(content.getBytes("utf-8"));
}

From source file:br.com.manish.ahy.kernel.util.HashUtil.java

public static String enc(String text) {
    String ret = null;/*from ww  w .  jav  a  2  s. com*/
    try {
        Cipher cipher = getCipher(true);
        byte[] encr = cipher.doFinal(text.getBytes());
        ret = getHex(encr);
    } catch (Exception e) {
        log.error(e);
        throw new OopsException(e, "Enc Error.");
    }

    return ret;

}

From source file:br.com.manish.ahy.kernel.util.HashUtil.java

public static String dec(String text) {
    String ret = null;//  ww  w .j  a va 2s  .c  o  m
    try {
        Cipher cipher = getCipher(false);
        byte[] orig = cipher.doFinal(getBytes(text));
        ret = new String(orig);
    } catch (Exception e) {
        log.error(e);
        throw new OopsException(e, "Dec Error.");
    }

    return ret;
}

From source file:io.cloudslang.content.database.utils.TripleDES.java

static byte[] encryptString(final byte[] text) throws Exception {
    final SecretKey key = new SecretKeySpec(
            TripleDES.md5Hash("NpWsCaJQj1LaXt)YYnzr\\%zP~RydB*3YGutr*@|A\\ckG3\\Yf%k"), ENCRYPTION_KEYSPECTYPE);
    final Cipher cipher = Cipher.getInstance(ENCRYPTION_MODE);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return cipher.doFinal(text);
}

From source file:Main.java

private static byte[] Rsa(byte[] byteData, Key pKey, int opmode) throws Exception {
    Cipher cipher = null;
    try {/*from   w w  w .  j  a  v  a2 s. c o m*/
        cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(opmode, pKey);

        return cipher.doFinal(byteData);
    } finally {
        cipher = null;
    }
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static String encrypt(String Data) throws Exception {
    Key key = generateKey();//from  w  w  w .  j av  a 2 s . c o m
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}

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

public static String decrypt(String key1, String iv1, String encrypted) {
    try {/*from   w  ww.j  av  a  2  s . c om*/
        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;
}