package com.hollysys.smartfactory.equipmentdiagnosis.util; import lombok.extern.log4j.Log4j2; import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; /** * Description: 对称加密 * * @author cy * @date 2019年08月05日 14:18 * Version 1.0 */ @Log4j2 public class AesUtil { /** * 密钥 */ private static String key = "AD42F6697B045B7580E4FEF93BE80BRG"; Private static String charset = " UTF-. 8 " ; / * * * Offset * / Private static int offset = 16 ; / * * * Type encryption: encryption algorithm is AES, the encryption mode is the CBC, complement way PKCS5Padding * / Private static String Transformation = " AES / the CBC / PKCS5Padding " ; / * * * algorithm type: specifies the AES key generation * / Private static String algorithm = " AES " ; / * * * encryption * @Param Content * * @return * / public static String the encrypt (String Content) { return the encrypt (Content, Key); } / * * * decryption * * @param Content * @return * / public static String the decrypt (String Content) { return the decrypt (content, Key); } / * * * encrypted * * @param content needs to be encrypted content * @param key encrypted password * @return * / public staticThe encrypt String (String Content, String Key) { the try { // configured key SecretKeySpec SKey = new new SecretKeySpec (key.getBytes (charset), algorithm); // create a key for designating iv initial vector offset (self- but it is necessary to specify 128), because the packet is AES encryption, the next group IV set to use it to serve as a ciphertext encrypted IvParameterSpec IV = new new IvParameterSpec (key.getBytes (charset), 0 , offset); // AES encrypter to create the cipher cipher = Cipher.getInstance (Transformation); byte [] = byteContent content.getBytes (charset); // use encryption mode scrambler cipher.init (Cipher.ENCRYPT_MODE, SKey, IV); // encrypted byte [] Result = Cipher.doFinal (byteContent); // use the binary array BASE64 encoded encrypted return new new the Base64 () encodeToString (Result). ; } the catch (Exception E) { log.info ( "" , E); } return null ; } / * * * the AES (256) decrypts * * @param content to be decrypted content * @param key decryption key * @return after decryption * @throws Exception * / public staticThe decrypt String (String Content, String Key) { the try { SecretKeySpec SKey = new new SecretKeySpec (key.getBytes (charset), algorithm); IvParameterSpec IV = new new IvParameterSpec (key.getBytes (charset), 0 , offset); the Cipher cipher = the Cipher .getInstance (Transformation); // use encryptor decryption in decryption mode // initialize cipher.init (Cipher.DECRYPT_MODE, SKey, IV); byte [] = Cipher.doFinal Result ( new new . the Base64 () decode (Content) ); // decryption return new String(result,charset); } catch (Exception e) { log.info("", e); } return null; } public static void main(String[] args) { String s = "hello World!123.加解密"; String encryptResultStr = encrypt(s); // 加密 System.out.println("加密前:" + s); System.out.println(" Encrypted: " + encryptResultStr); // decrypt the System. OUT .println ( " decrypted: " + the decrypt (encryptResultStr)); } }