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:MainClass.java

private static byte[] passwordEncrypt(char[] password, byte[] plaintext) throws Exception {
    int MD5_ITERATIONS = 1000;
    byte[] salt = new byte[8];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);// ww  w. jav a 2 s  . co  m

    PBEKeySpec keySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
    SecretKey key = keyFactory.generateSecret(keySpec);
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, MD5_ITERATIONS);
    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

    byte[] ciphertext = cipher.doFinal(plaintext);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(salt);
    baos.write(ciphertext);
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] des3EncodeECB(byte[] key, byte[] data) throws Exception {

    Key deskey = null;//from  ww w  .jav a 2 s.  c o m
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

    cipher.init(Cipher.ENCRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(data);

    return bOut;
}

From source file:com.java.demo.DesDemo.java

public static byte[] Decrypt(byte[] strBytes) {
    try {/*from   w  w  w .j  a  va  2 s.  co  m*/
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
        byte[] result = cipher.doFinal(strBytes);
        return result;
    } catch (Exception e) {
        return null;
    }
}

From source file:EncDec.AES.java

public static String encrypt(String key, String initVector, String value) {
    try {/*  w  w  w .  j  a  v a  2s  .co 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:com.java.demo.DesDemo.java

public static byte[] Encrypt(String str) {
    try {//from   w  ww  .j  a v  a  2s . c  o m
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
        byte[] result = cipher.doFinal(str.getBytes());
        return result;

    } catch (Exception e) {
        return null;
    }
}

From source file:com.dragoniade.encrypt.EncryptionHelper.java

public static void encrypt(Properties p, String seedKey, String key) {
    String value = p.getProperty(key);
    String seed = p.getProperty(seedKey, seedKey);

    String encrypted;/*from  w ww . j a v  a 2  s . c  om*/
    try {
        Cipher cipher = getEncrypter(seed);
        byte[] result = cipher.doFinal(value.getBytes("UTF-16"));
        encrypted = cipher.getAlgorithm() + "{" + Base64.encode(result) + "}";
    } catch (Exception e) {
        try {
            encrypted = "{" + Base64.encode(value.getBytes("UTF-16")) + "}";
        } catch (Exception e2) {
            encrypted = "{" + Base64.encode(value.getBytes()) + "}";
        }
    }

    p.setProperty(key, encrypted);
}

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

public static String decrypt(String encrypted) {
    try {//  ww  w .j a va  2 s  .co  m
        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.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:Main.java

private static byte[] decrypt(byte[] key, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
    return cipher.doFinal(encrypted);
}

From source file:Main.java

private static byte[] encrypt(byte[] key, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
    return cipher.doFinal(clear);
}

From source file:Main.java

public static byte[] ees3DecodeECB(byte[] key, byte[] data) throws Exception {

    Key deskey = null;//w ww  .ja va  2s .  c  o  m
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

    cipher.init(Cipher.DECRYPT_MODE, deskey);

    byte[] bOut = cipher.doFinal(data);

    return bOut;

}