教学服务系统

 找回密码
 立即注册
搜索
查看: 914|回复: 1

信息计算2019级2班6号李靓

[复制链接]

8

主题

17

帖子

72

积分

注册会员

Rank: 2

积分
72
发表于 2022-4-5 16:01:28 | 显示全部楼层 |阅读模式

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

8

主题

17

帖子

72

积分

注册会员

Rank: 2

积分
72
 楼主| 发表于 2022-4-5 21:08:55 | 显示全部楼层
本帖最后由 李靓 于 2022-4-5 21:18 编辑
  1. package StringTest;

  2. import javax.crypto.Cipher;
  3. import javax.crypto.KeyGenerator;
  4. import javax.crypto.spec.IvParameterSpec;
  5. import javax.crypto.spec.SecretKeySpec;
  6. import java.math.BigInteger;
  7. import java.util.Base64;


  8. public class AesUtil {
  9.     //密钥 (需要前端和后端保持一致)
  10.     public static final String KEY = "ASDFGHJKLZXCVBNM";
  11.     //算法
  12.     private static final String ALGORITHMSTR = "AES/CBC/PKCS5Padding";
  13.     //密钥
  14.     public static final String IV_KEY = "QWERTYUIOPZXCVBN";

  15.    
  16.     private static final String ALGORITHMSTRCSS = "AES/ECB/PKCS5Padding";


  17.     public static String aesDecrypt(String encrypt) {
  18.         try {
  19.             return aesDecrypt(encrypt, KEY);
  20.         } catch (Exception e) {
  21.             e.printStackTrace();
  22.             return "";
  23.         }  
  24.     }  
  25.       

  26.     public static String aesEncrypt(String content) {  
  27.         try {
  28.             return aesEncrypt(content, KEY);
  29.         } catch (Exception e) {
  30.             e.printStackTrace();
  31.             return "";
  32.         }
  33.     }

  34.    
  35.     public static String binary(byte[] bytes, int radix){
  36.         return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
  37.     }

  38.    
  39.     public static String base64Encode(byte[] bytes){
  40.         return Base64.getMimeEncoder().encodeToString(bytes);
  41.     }

  42.     public static byte[] base64Decode(String base64Code) throws Exception{
  43.         return base64Code == null || base64Code.trim().length() <= 0 ? null : Base64.getMimeDecoder().decode(base64Code);
  44.     }


  45.   
  46.     public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
  47.         KeyGenerator kgen = KeyGenerator.getInstance("AES");
  48.         kgen.init(128);
  49.         Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
  50.       
  51.         IvParameterSpec iv = new IvParameterSpec(IV_KEY.getBytes("UTF-8"));
  52.         cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes("UTF-8"), "AES"),iv);
  53.         return cipher.doFinal(content.getBytes("utf-8"));
  54.         
  55.     }
  56.   

  57.     public static String aesEncrypt(String content, String encryptKey) throws Exception {  
  58.         return base64Encode(aesEncryptToBytes(content, encryptKey));  
  59.     }  
  60.   

  61.     public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {  
  62.         KeyGenerator kgen = KeyGenerator.getInstance("AES");  
  63.         kgen.init(128);  
  64.         
  65.         Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
  66.       
  67.         IvParameterSpec iv = new IvParameterSpec(IV_KEY.getBytes("UTF-8"));
  68.         cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes("UTF-8"), "AES"),iv);
  69.         byte[] decryptBytes = cipher.doFinal(encryptBytes);
  70.         return new String(decryptBytes,"UTF-8");

  71.     }


  72.    
  73.     public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
  74.         return encryptStr == null || encryptStr.trim().length() <= 0  ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
  75.     }


  76.     public static String mobileDesensitization(String mobile) {
  77.         if (mobile != null && mobile.trim().length() > 0 ) {
  78.             return mobile;
  79.         }

  80.         if (mobile.length() == 11) {
  81.             return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
  82.         } else {

  83.             return aesDecrypt(mobile).replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
  84.         }
  85.     }

  86.    
  87.     public static byte[] cssAesEncryptToBytes(String content, String encryptKey) throws Exception {
  88.         KeyGenerator kgen = KeyGenerator.getInstance("AES");
  89.         kgen.init(128);
  90.         Cipher cipher = Cipher.getInstance(ALGORITHMSTRCSS);
  91.         cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes("UTF-8"), "AES"));
  92.         return cipher.doFinal(content.getBytes("utf-8"));
  93.     }

  94.    
  95.     public static String cssAesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
  96.         KeyGenerator kgen = KeyGenerator.getInstance("AES");
  97.         kgen.init(128);

  98.         Cipher cipher = Cipher.getInstance(ALGORITHMSTRCSS);
  99.         cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes("UTF-8"), "AES"));
  100.         byte[] decryptBytes = cipher.doFinal(encryptBytes);
  101.         return new String(decryptBytes,"UTF-8");
  102.     }

  103.    
  104.     public static String cssAesEncrypt(String content, String encryptKey) throws Exception {
  105.         return base64Encode(cssAesEncryptToBytes(content, encryptKey));
  106.     }

  107.    
  108.     public static byte[] cssBase64Decode(String base64Code) throws Exception{
  109.         return base64Code == null || base64Code.trim().length() <= 0  ? null : Base64.getDecoder().decode(base64Code);
  110.     }

  111.   
  112.     public static String cssAesDecrypt(String encryptStr, String decryptKey) throws Exception {
  113.         return encryptStr == null || encryptStr.trim().length() <= 0 ? null : cssAesDecryptByBytes(cssBase64Decode(encryptStr), decryptKey);
  114.     }


  115.    
  116.     public static void main(String[] args) throws Exception {  
  117.         String content = "密码学";
  118.         System.out.println("加密前:" + content);  
  119.       
  120.         String encrypt = aesEncrypt(content, KEY);
  121.         System.out.println("加密后:" + encrypt);
  122.         String decrypt = aesDecrypt(encrypt, KEY);
  123.         System.out.println("解密后:" + decrypt);
  124.         
  125.         String encryptPhoneNum = "12345";
  126.         String phoneNum = AesUtil.aesEncrypt(encryptPhoneNum);
  127.         
  128.         String decryptPhoneNum = AesUtil.aesDecrypt( phoneNum);
  129.       
  130.     }
  131. }
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-5-6 10:21 , Processed in 0.016611 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表