The Java ASE encryption code is as follows
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
/** * Aes encryption tool class **@author chengh
* @date2021/07/14 * /
public class AesEncryptUtils {
/** * Indicates the algorithm name, encryption mode, and data filling mode */
private static final String ALGORITHMS = "AES/CBC/PKCS5Padding";
/** * Initialize the vector (adjust the value of the vector as required, or add the vector to the input variables) */
private static final byte[] SIV = new byte[16];
/** * Encryption **@paramContent Encrypted string *@param* the encryptKey key values@returnEncrypted content *@throwsThe Exception Exception * /
public static String encrypt(String content, String encryptKey) throws Exception {
KeyGenerator.getInstance("AES").init(128);
Cipher cipher = Cipher.getInstance(ALGORITHMS);
// Encryption vector
IvParameterSpec iv = new IvParameterSpec(SIV);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"), iv);
byte[] b = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
// Use base64 algorithm for transcoding to avoid Chinese garbled characters
return Base64.encodeBase64String(b);
}
/** * Decrypt **@paramEncryptStr The string * to decrypt@paramDecryptKey Specifies the key value * that is decrypted@returnDecrypted content *@throwsThe Exception Exception * /
public static String decrypt(String encryptStr, String decryptKey) throws Exception {
KeyGenerator.getInstance("AES").init(128);
Cipher cipher = Cipher.getInstance(ALGORITHMS);
// Encryption vector
IvParameterSpec iv = new IvParameterSpec(SIV);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"), iv);
// Use base64 algorithm for transcoding to avoid Chinese garbled characters
byte[] encryptBytes = Base64.decodeBase64(encryptStr);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return newString(decryptBytes); }}Copy the code