AES encrypt byte array by byte array key and return byte array - Android java.security

Android examples for java.security:AES

Description

AES encrypt byte array by byte array key and return byte array

Demo Code

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Main{


    public static byte[] encrypt(byte[] key, byte[] input) throws Exception {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"));
        byte[] encrypted = cipher.doFinal(input);
        return encrypted;
    }//from   w  w  w.j  a  v  a 2  s.c om

}

Related Tutorials