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.norbl.util.StringUtil.java

public static String encrypt(SecretKey key, String s) throws Exception {
    Cipher c = Cipher.getInstance("AES");
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] enb = c.doFinal(s.getBytes());
    Base64 b64enc = new Base64();
    return (new String(b64enc.encode(enb)));
}

From source file:com.vico.license.util.rsa.RSAdoEncrypt.java

public static String encrypt(String source, byte[] publickey) throws Exception {

    String path = Thread.currentThread().getContextClassLoader().getResource("/").toURI().getPath();
    Key publicKey = null;//from   ww  w .ja  v a 2  s.co m

    publicKey = (Key) ByteArrayToObj.ByteToObject(publickey);

    /** Cipher???RSA */
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] b = source.getBytes("UTF-8");

    /** ?*/
    byte[] b1 = cipher.doFinal(b);

    String encryptedcode = Base64.encodeBase64String(b1);
    return encryptedcode;
}

From source file:com.aast.encrypt.EncryptManager.java

public static String encryptAES(String key, String value) {
    try {//from   w  w  w .  j  a v a 2 s . c  o m
        byte[] keyBytes = getKeyFromString(key);
        IvParameterSpec iv = new IvParameterSpec(keyBytes);
        SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(value.getBytes());
        //            System.out.println("encrypted string: "
        //                    + Base64.encodeBase64String(encrypted));

        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:com.norbl.util.StringUtil.java

public static String decrypt(SecretKey key, String en) throws Exception {
    Base64 b64enc = new Base64();
    byte[] enb = b64enc.decode(en.getBytes());
    Cipher c = Cipher.getInstance("AES");
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] denb = c.doFinal(enb);
    return (new String(denb));
}

From source file:com.elle.analyster.admissions.AESCrypt.java

public static String decrypt(String key, String initVector, String encrypted) {
    try {//from ww  w .  j a v  a2  s .  c om
        IvParameterSpec iv = new IvParameterSpec(initVector.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(encrypted));

        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:illab.nabal.util.SecurityHelper.java

/**
 * Decrypt a AES-encrypted message.//from  www .  j  a  va2 s. com
 * 
 * @param encrypted
 * @return String
 * @throws Exception
 */
public static String decipher(String encrypted) throws Exception {
    if (StringHelper.isEmpty(encrypted) == false) {
        Cipher cipher = Cipher.getInstance(AES);
        cipher.init(Cipher.DECRYPT_MODE, SYSTEM_SECRET_KEY_SPEC);
        return new String(cipher.doFinal(Base64.decode(encrypted, Base64.DEFAULT)), HTTP.UTF_8);
    } else {
        return null;
    }
}

From source file:com.baidu.terminator.plugin.signer.customized.app.CipherUtil.java

public static byte[] process(Cipher cipher, int blockSize, byte[] input)
        throws IllegalBlockSizeException, BadPaddingException, ShortBufferException {
    if (input.length <= blockSize) {
        return cipher.doFinal(input);
    }//from ww w  . j  a  v  a2s . c  om
    byte[] output = new byte[OUTPUT_SIZE];
    int outputSize = 0;
    for (int i = 0;; i += blockSize) {
        if (i + blockSize < input.length)
            outputSize += cipher.doFinal(input, i, blockSize, output, outputSize);
        else {
            outputSize += cipher.doFinal(input, i, input.length - i, output, outputSize);
            break;
        }
    }
    return ArrayUtils.subarray(output, 0, outputSize);
}

From source file:com.elle.analyster.admissions.AESCrypt.java

public static String encrypt(String key, String initVector, String value) {
    try {// w ww .java  2  s .  c o  m
        IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(value.getBytes());
        System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted));

        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:edu.wright.cs.sp16.ceg3120.util.PasswordEncryptionUtility.java

/**
 * Decrypts a given string using AES./* w ww .  j a v a  2  s  .co m*/
 * 
 * @param encrypted
 *            // Encrypted string.
 * @return // Returns decrypted string.
 */
public static String decrypt(String encrypted) {
    try {
        IvParameterSpec iv = new IvParameterSpec(initVector.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(encrypted));
        System.out.println("Decrypted Password: " + new String(original, "UTF-8"));
        return new String(original, "UTF-8");
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:Main.java

private static byte[] Aes(byte[] byteData, byte[] byteKey, int opmode) throws Exception {
    Cipher cipher = null;
    try {//from ww  w.j ava  2  s.  c o  m
        SecretKeySpec aesKey = new SecretKeySpec(byteKey, "AES");
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(opmode, aesKey);

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