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

public static byte[] encrypt(byte[] input, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, keySpec);
    return cipher.doFinal(input);
}

From source file:Main.java

public static byte[] decrypt(byte[] input, byte[] key) throws InvalidKeyException, NoSuchPaddingException,
        NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException {
    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, keySpec);
    return cipher.doFinal(input);
}

From source file:Main.java

public static byte[] cipher(int mode, byte[] data, byte[] secret) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    SecretKeySpec secretKeySpec = new SecretKeySpec(sha256(secret), "AES");
    Cipher c = Cipher.getInstance("AES");
    c.init(mode, secretKeySpec);//from w  w  w.  j  a  v a  2 s . c  o  m
    return c.doFinal(data);
}

From source file:Main.java

public static byte[] decryptData(byte[] encryptedData, PrivateKey privateKey) {
    try {//from  ww  w  .j av  a  2 s  . c o m
        Cipher cipher = Cipher.getInstance(RSA);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(encryptedData);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "AES");

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] plainBytes = cipher.doFinal(data);
    return plainBytes;
}

From source file:MainClass.java

private static String decrypt(char[] password, String text) throws Exception {
    String salt = text.substring(0, 12);
    String ciphertext = text.substring(12, text.length());
    BASE64Decoder decoder = new BASE64Decoder();
    byte[] saltArray = decoder.decodeBuffer(salt);
    byte[] ciphertextArray = decoder.decodeBuffer(ciphertext);

    PBEKeySpec keySpec = new PBEKeySpec(password);

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");

    SecretKey key = keyFactory.generateSecret(keySpec);

    PBEParameterSpec paramSpec = new PBEParameterSpec(saltArray, 1000);

    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

    return new String(cipher.doFinal(ciphertextArray));
}

From source file:Main.java

public static byte[] encrypt(byte[] data, byte[] key, byte[] iv, String cipherAlgotirhm) {
    try {/*ww  w . j a  va2s.co  m*/
        Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, key, iv, cipherAlgotirhm);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] decrypt(byte[] data, byte[] key, byte[] iv, String cipherAlgorithm) {
    try {/*from   ww  w  . j a v  a  2  s.  c o  m*/
        Cipher cipher = initCipher(Cipher.DECRYPT_MODE, key, iv, cipherAlgorithm);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] decodeFile(byte[] key, byte[] fileData, boolean isPlainText) throws Exception {
    byte[] decrypted;
    if (!isPlainText) {
        decrypted = new byte[fileData.length - key.length * 2];
        System.arraycopy(fileData, key.length, decrypted, 0, decrypted.length);
    } else {/*from   w  ww .j  a  va2s  .com*/
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        decrypted = cipher.doFinal(fileData);
    }
    return decrypted;
}

From source file:Main.java

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