Java AES Encrypt aesEncryptToBytes(String content, String encryptKey)

Here you can find the source of aesEncryptToBytes(String content, String encryptKey)

Description

aes Encrypt To Bytes

License

Open Source License

Declaration

private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception 

Method Source Code


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

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

public class Main {
    private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128, new SecureRandom(encryptKey.getBytes()));

        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

        return cipher.doFinal(content.getBytes("utf-8"));
    }/*from ww w  .  j ava2 s  . c  om*/
}

Related

  1. aesEncrypt(byte[] source, Key key)
  2. aesEncrypt(String content, String encryptKey)
  3. aesEncrypt(String content, String key)
  4. aesEncryptBytes(byte[] pBytes, SecretKeySpec pKeySpec, IvParameterSpec ivParameterSpec)