You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					99 lines
				
				3.1 KiB
			
		
		
			
		
	
	
					99 lines
				
				3.1 KiB
			| 
											3 years ago
										 | package com.ynxbd.wx.utils;
 | ||
|  | 
 | ||
|  | import com.ynxbd.common.helper.common.CodeHelper;
 | ||
|  | import com.ynxbd.common.helper.common.JsonHelper;
 | ||
|  | import lombok.extern.slf4j.Slf4j;
 | ||
|  | 
 | ||
|  | import java.nio.charset.StandardCharsets;
 | ||
|  | import java.util.Base64;
 | ||
|  | import java.util.UUID;
 | ||
|  | 
 | ||
|  | @Slf4j
 | ||
|  | public class MsgEncodeHelper {
 | ||
|  |     /**
 | ||
|  |      * BASE64加密
 | ||
|  |      *
 | ||
|  |      * @param obj 需要加密的字符串
 | ||
|  |      * @return 加密后的字符串
 | ||
|  |      */
 | ||
|  |     public static String encode(Object obj) {
 | ||
|  |         if (obj == null) return null;
 | ||
|  |         String encode;
 | ||
|  |         if (obj.getClass() != String.class) {
 | ||
|  |             encode = JsonHelper.toJsonString(obj);
 | ||
|  |         } else {
 | ||
|  |             encode = obj.toString();
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         if ("".equals(encode)) {
 | ||
|  |             return null;
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         encode = java.util.Base64.getEncoder().encodeToString(encode.getBytes(StandardCharsets.UTF_8));
 | ||
|  |         String uuid = CodeHelper.get24UUID().toUpperCase();
 | ||
|  |         String uBeg = uuid.substring(0, 12);
 | ||
|  |         String uEnd = uuid.substring(12);
 | ||
|  | 
 | ||
|  |         log.info("uuid={}, uBeg={}, uEnd={}", uuid, uBeg, uEnd);
 | ||
|  | 
 | ||
|  |         if (encode.length() > 25) {
 | ||
|  |             String begStr = encode.substring(0, 10);
 | ||
|  |             String endStr = encode.substring(encode.length() - 15, encode.length() - 5);
 | ||
|  |             String end = encode.substring(encode.length() - 5);
 | ||
|  |             String middle = encode.substring(10, encode.length() - 15);
 | ||
|  |             return uuid + endStr + middle + begStr + end;
 | ||
|  |         } else {
 | ||
|  |             encode = uuid + encode;
 | ||
|  |         }
 | ||
|  |         return encode;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     // 判断是否只有数字和字母
 | ||
|  |     public static boolean isLetterDigit(String str) {
 | ||
|  |         return str.matches("^[a-z0-9A-Z]+$");
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 解密
 | ||
|  |      *
 | ||
|  |      * @param data 需要解密的字符串
 | ||
|  |      */
 | ||
|  |     public static String decodeEn(String data) {
 | ||
|  |         try {
 | ||
|  |             if (data == null) {
 | ||
|  |                 return null;
 | ||
|  |             }
 | ||
|  |             int dataLen = data.length();
 | ||
|  |             if (dataLen > 32) { // 截取掉uuid
 | ||
|  |                 data = data.substring(32);
 | ||
|  |             }
 | ||
|  |             if (dataLen > 57) { // 超过57位的需要调换前后位置
 | ||
|  |                 String endStr = data.substring(0, 10);
 | ||
|  |                 String begStr = data.substring(data.length() - 15, data.length() - 5);
 | ||
|  |                 String middle = data.substring(10, data.length() - 15);
 | ||
|  |                 String end = data.substring(data.length() - 5);
 | ||
|  |                 data = begStr + middle + endStr + end;
 | ||
|  |             }
 | ||
|  |             String deData = new String(Base64.getDecoder().decode(data), StandardCharsets.UTF_8);// base64解密
 | ||
|  |             if (!isLetterDigit(deData)) {
 | ||
|  |                 return null;
 | ||
|  |             }
 | ||
|  |             return deData;
 | ||
|  |         } catch (Exception e) {
 | ||
|  |             return null;
 | ||
|  |         }
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public static void main(String[] args) {
 | ||
|  | 
 | ||
|  |         for (int i = 0; i < 100; i++) {
 | ||
|  |             long begTime = System.currentTimeMillis();
 | ||
|  |             String encode = encode("123");
 | ||
|  |             long endTime = System.currentTimeMillis();
 | ||
|  | 
 | ||
|  |             System.out.println(endTime - begTime);
 | ||
|  |         }
 | ||
|  |     }
 | ||
|  | }
 |