Android AES Encrypt encrypt(byte[] data, byte[] key)

Here you can find the source of encrypt(byte[] data, byte[] key)

Description

encrypt

Parameter

Parameter Description
data data
key binary secret key

Exception

Parameter Description
Exception an exception

Return

byte[] encrypt data

Declaration

public static byte[] encrypt(byte[] data, byte[] key) 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 ww w. ja v  a2 s.  c  om
     * Secret Key algorithms
     */
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

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

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

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

    /**
     * encrypt
     *
     * @param data   data
     * @param key   secret key
     * @param cipherAlgorithm   encrypt algorithm/work pattern/padding mode
     * @return byte[]   encrypt data
     * @throws Exception
     */
    public static byte[] encrypt(byte[] data, Key key,
            String cipherAlgorithm) throws Exception {
        //init
        Cipher cipher = Cipher.getInstance(cipherAlgorithm);
        //set encrypt mode
        cipher.init(Cipher.ENCRYPT_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. encrypt(byte[] content, String key)
  2. encrypt(byte[] content, String password)
  3. encrypt(byte[] content, String password)
  4. encrypt(byte[] data, Key key)
  5. encrypt(byte[] data, Key key, String cipherAlgorithm)
  6. encrypt(byte[] data, byte[] key, String cipherAlgorithm)
  7. encrypt(byte[] key, byte[] src)
  8. encrypt(byte[] key, byte[] src)
  9. encrypt(byte[] raw, byte[] clear)