import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.commons.lang3.RandomStringUtils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.text.SimpleDateFormat;
import java.time.Year;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class MainTest {
/**
* 密钥,必须是16位
*/
private static final String KEY = "1143456789uuukkl";
/**
* 偏移量,必须是16位
*/
private static final String IV = "pppokl9806543210";
public static void main(String[] args) throws Exception {
System.out.println(encrypt("小傅哥"));
// System.out.println(decrypt(""));
}
private final static Set<String> set = new HashSet<>();
public static String encrypt(String content) throws Exception {
// Create a new Cipher object using AES/CBC/PKCS5Padding algorithm
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// Get the raw key from the KEY variable
byte[] raw = KEY.getBytes();
// Create a new SecretKeySpec object with the raw key
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
// Create a new IvParameterSpec object with the IV variable
IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
// Initialize the Cipher object with the encryption mode, secret key spec, and IV parameter spec
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
// Do the encryption
byte[] encrypted = cipher.doFinal(content.getBytes());
// Encode the encrypted content to a Base64 string
return Base64.getEncoder().encodeToString(encrypted);
}
/**
* AES解密
*
* @param content 密文
* @return 明文
* @throws Exception 异常
*/
public static String decrypt(String content) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = KEY.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = Base64.getDecoder().decode(content);
byte[] original = cipher.doFinal(encrypted);
return new String(original);
}
}