Java AES Encrypt aesEncryptBytes(byte[] pBytes, SecretKeySpec pKeySpec, IvParameterSpec ivParameterSpec)

Here you can find the source of aesEncryptBytes(byte[] pBytes, SecretKeySpec pKeySpec, IvParameterSpec ivParameterSpec)

Description

aes Encrypt Bytes

License

Open Source License

Declaration

public static byte[] aesEncryptBytes(byte[] pBytes, SecretKeySpec pKeySpec, IvParameterSpec ivParameterSpec)
            throws NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, InvalidKeyException,
            IllegalBlockSizeException, InvalidAlgorithmParameterException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import java.security.*;

public class Main {
    public static byte[] aesEncryptBytes(byte[] pBytes, SecretKeySpec pKeySpec, IvParameterSpec ivParameterSpec)
            throws NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, InvalidKeyException,
            IllegalBlockSizeException, InvalidAlgorithmParameterException {
        Cipher cipher = getAESCipher();
        cipher.init(Cipher.ENCRYPT_MODE, pKeySpec, ivParameterSpec);
        return cipher.doFinal(pBytes);
    }/*ww  w  .j  a v a  2  s  .c o  m*/

    private static Cipher getAESCipher() throws NoSuchPaddingException, NoSuchAlgorithmException {
        return Cipher.getInstance("AES/CBC/PKCS5Padding");
    }
}

Related

  1. aesEncrypt(byte[] source, Key key)
  2. aesEncrypt(String content, String encryptKey)
  3. aesEncrypt(String content, String key)
  4. aesEncryptToBytes(String content, String encryptKey)