Android AES Decrypt decrypt(byte[] data, Key key, String cipherAlgorithm)

Here you can find the source of decrypt(byte[] data, Key key, String cipherAlgorithm)

Description

decrypt

Parameter

Parameter Description
data data
key secret key
cipherAlgorithm encrypt algorithm/work pattern/padding mode

Exception

Parameter Description
Exception an exception

Return

byte[] decrypt data

Declaration

public static byte[] decrypt(byte[] data, Key key,
        String cipherAlgorithm) throws Exception 

Method Source Code

//package com.java2s;
import java.security.Key;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

public class Main {
    /**//from  w ww  . j  ava 2 s .  c  om
     * Secret Key algorithms
     */
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * decrypt
     *
     * @param data   data
     * @param key   binary secret key
     * @return byte[]   decrypt data
     * @throws Exception
     */
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        return decrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
    }

    /**
     * decrypt
     *
     * @param data   data
     * @param key   secret key
     * @return byte[]   decrypt data
     * @throws Exception
     */
    public static byte[] decrypt(byte[] data, Key key) throws Exception {
        return decrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
    }

    /**
     * decrypt
     *
     * @param data   data
     * @param key   binary secret key
     * @param cipherAlgorithm   encrypt algorithm/work pattern/padding mode
     * @return byte[]   decrypt data
     * @throws Exception
     */
    public static byte[] decrypt(byte[] data, byte[] key,
            String cipherAlgorithm) throws Exception {
        //restore secret key
        Key k = toKey(key);
        return decrypt(data, k, cipherAlgorithm);
    }

    /**
     * decrypt
     *
     * @param data   data
     * @param key   secret key
     * @param cipherAlgorithm   encrypt algorithm/work pattern/padding mode
     * @return byte[]   decrypt data
     * @throws Exception
     */
    public static byte[] decrypt(byte[] data, Key key,
            String cipherAlgorithm) throws Exception {
        //init
        Cipher cipher = Cipher.getInstance(cipherAlgorithm);
        //set decrypt mode
        cipher.init(Cipher.DECRYPT_MODE, key);
        //perform action
        return cipher.doFinal(data);
    }

    /**
     * transfer secret key
     *
     * @param key   binary secret key
     * @return secret key
     */
    private static Key toKey(byte[] key) {
        //create secret key
        return new SecretKeySpec(key, KEY_ALGORITHM);
    }
}

Related

  1. decrypt(byte[] content, String password)
  2. decrypt(byte[] content, String password)
  3. decrypt(byte[] content, String password)
  4. decrypt(byte[] content, String password)
  5. decrypt(byte[] data, Key key)
  6. decrypt(byte[] data, byte[] key)
  7. decrypt(byte[] data, byte[] key, String cipherAlgorithm)
  8. decrypt(byte[] encryptSource)
  9. decrypt(byte[] key, byte[] encrypted)