|

楼主 |
发表于 2022-4-5 21:08:55
|
显示全部楼层
本帖最后由 李靓 于 2022-4-5 21:18 编辑
- package StringTest;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import java.math.BigInteger;
- import java.util.Base64;
- public class AesUtil {
- //密钥 (需要前端和后端保持一致)
- public static final String KEY = "ASDFGHJKLZXCVBNM";
- //算法
- private static final String ALGORITHMSTR = "AES/CBC/PKCS5Padding";
- //密钥
- public static final String IV_KEY = "QWERTYUIOPZXCVBN";
-
- private static final String ALGORITHMSTRCSS = "AES/ECB/PKCS5Padding";
- public static String aesDecrypt(String encrypt) {
- try {
- return aesDecrypt(encrypt, KEY);
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
- }
-
-
- public static String aesEncrypt(String content) {
- try {
- return aesEncrypt(content, KEY);
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
- }
-
- public static String binary(byte[] bytes, int radix){
- return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
- }
-
- public static String base64Encode(byte[] bytes){
- return Base64.getMimeEncoder().encodeToString(bytes);
- }
- public static byte[] base64Decode(String base64Code) throws Exception{
- return base64Code == null || base64Code.trim().length() <= 0 ? null : Base64.getMimeDecoder().decode(base64Code);
- }
-
- public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
- KeyGenerator kgen = KeyGenerator.getInstance("AES");
- kgen.init(128);
- Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
-
- IvParameterSpec iv = new IvParameterSpec(IV_KEY.getBytes("UTF-8"));
- cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes("UTF-8"), "AES"),iv);
- return cipher.doFinal(content.getBytes("utf-8"));
-
- }
-
-
- public static String aesEncrypt(String content, String encryptKey) throws Exception {
- return base64Encode(aesEncryptToBytes(content, encryptKey));
- }
-
-
- public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
- KeyGenerator kgen = KeyGenerator.getInstance("AES");
- kgen.init(128);
-
- Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
-
- IvParameterSpec iv = new IvParameterSpec(IV_KEY.getBytes("UTF-8"));
- cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes("UTF-8"), "AES"),iv);
- byte[] decryptBytes = cipher.doFinal(encryptBytes);
- return new String(decryptBytes,"UTF-8");
- }
-
- public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
- return encryptStr == null || encryptStr.trim().length() <= 0 ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
- }
- public static String mobileDesensitization(String mobile) {
- if (mobile != null && mobile.trim().length() > 0 ) {
- return mobile;
- }
-
- if (mobile.length() == 11) {
- return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
- } else {
- return aesDecrypt(mobile).replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
- }
- }
-
- public static byte[] cssAesEncryptToBytes(String content, String encryptKey) throws Exception {
- KeyGenerator kgen = KeyGenerator.getInstance("AES");
- kgen.init(128);
- Cipher cipher = Cipher.getInstance(ALGORITHMSTRCSS);
- cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes("UTF-8"), "AES"));
- return cipher.doFinal(content.getBytes("utf-8"));
- }
-
- public static String cssAesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
- KeyGenerator kgen = KeyGenerator.getInstance("AES");
- kgen.init(128);
- Cipher cipher = Cipher.getInstance(ALGORITHMSTRCSS);
- cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes("UTF-8"), "AES"));
- byte[] decryptBytes = cipher.doFinal(encryptBytes);
- return new String(decryptBytes,"UTF-8");
- }
-
- public static String cssAesEncrypt(String content, String encryptKey) throws Exception {
- return base64Encode(cssAesEncryptToBytes(content, encryptKey));
- }
-
- public static byte[] cssBase64Decode(String base64Code) throws Exception{
- return base64Code == null || base64Code.trim().length() <= 0 ? null : Base64.getDecoder().decode(base64Code);
- }
-
- public static String cssAesDecrypt(String encryptStr, String decryptKey) throws Exception {
- return encryptStr == null || encryptStr.trim().length() <= 0 ? null : cssAesDecryptByBytes(cssBase64Decode(encryptStr), decryptKey);
- }
-
- public static void main(String[] args) throws Exception {
- String content = "密码学";
- System.out.println("加密前:" + content);
-
- String encrypt = aesEncrypt(content, KEY);
- System.out.println("加密后:" + encrypt);
- String decrypt = aesDecrypt(encrypt, KEY);
- System.out.println("解密后:" + decrypt);
-
- String encryptPhoneNum = "12345";
- String phoneNum = AesUtil.aesEncrypt(encryptPhoneNum);
-
- String decryptPhoneNum = AesUtil.aesDecrypt( phoneNum);
-
- }
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|