Here you can find the source of decrypt(byte[] data, Key key, String cipherAlgorithm)
Parameter | Description |
---|---|
data | data |
key | secret key |
cipherAlgorithm | encrypt algorithm/work pattern/padding mode |
Parameter | Description |
---|---|
Exception | an exception |
public static byte[] decrypt(byte[] data, Key key, String cipherAlgorithm) throws Exception
//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); } }