Java Utililty Methods AES Encrypt

List of utility methods to do AES Encrypt

Description

The list of methods to do AES Encrypt are organized into topic(s).

Method

byte[]aesEncrypt(byte[] source, Key key)
aes Encrypt
try {
    Cipher cipher = Cipher.getInstance(AES_ALGORITHM_NAME);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return cipher.doFinal(source);
} catch (Exception e) {
    throw new IllegalStateException("AES ecnrypt error", e);
StringaesEncrypt(String content, String encryptKey)
aes Encrypt
return parseByte2HexStr(aesEncryptToBytes(content, encryptKey));
StringaesEncrypt(String content, String key)
aes Encrypt
try {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(key.getBytes()));
    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    byte[] byteContent = content.getBytes("utf-8");
...
byte[]aesEncryptBytes(byte[] pBytes, SecretKeySpec pKeySpec, IvParameterSpec ivParameterSpec)
aes Encrypt Bytes
Cipher cipher = getAESCipher();
cipher.init(Cipher.ENCRYPT_MODE, pKeySpec, ivParameterSpec);
return cipher.doFinal(pBytes);
byte[]aesEncryptToBytes(String content, String encryptKey)
aes Encrypt To Bytes
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"));