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

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

Description

Encrypts data using the AES encryption cipher.

Parameter

Parameter Description
data - Information to be encrypted.
key - Key to use for encryption.

Return

The encrypted data.

Declaration

public static byte[] encryptBytes(byte[] data, byte[] key) 

Method Source Code

//package com.java2s;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    /**//from   ww  w  . j a v a 2 s  .  c o  m
     * Encrypts data using the AES encryption cipher.
     * 
     * @param data
     *            - Information to be encrypted.
     * @param key
     *            - Key to use for encryption.
     * @return The encrypted data.
     */
    public static byte[] encryptBytes(byte[] data, byte[] key) {
        try {
            Cipher cipher = Cipher.getInstance("AES");
            SecretKeySpec sKey = new SecretKeySpec(key, "AES");
            cipher.init(Cipher.ENCRYPT_MODE, sKey);
            return cipher.doFinal(data);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related

  1. encrypt(byte[] raw, byte[] clear)
  2. encrypt(byte[] source)
  3. encryptBase64(String content, String key)
  4. encryptBase64(byte[] source)
  5. encryptBytes(String seed, byte[] cleartext)
  6. encryptHex(String content, String key)
  7. encryptHex(String content, String username, String password)
  8. encryptNumberWithAES(String number)
  9. encryptToBase64Text(String key, String src)