package com.cm; import com.cm.security.License; import com.cm.security.utils.AddressUtil; import com.cm.security.utils.AesUtil; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.digest.DigestUtils; import org.junit.Test; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; /** * @author Administrator * @version 1.0 * @className LicenseTest * @description TODO * @date 2018/11/20 14:46 * **/ public class LicenseTest { private static String LICENSE_KEY = "_System_License_"; private static String LICENSE_OLD_KDY = "CMXX0471"; @Test public void licenseTest() throws Exception { // 3C-A0-67-4A-88-D2 // 巴盟三务公开:6C-92-BF-4F-07-0D // 包头东河三务公开:‎00-16-3E-00-62-F6 // 通用 00-16-3E-00-79-58 // 乌拉特中旗三务公开: // 乌海海勃湾区 50-AF-73-27-2E-B2 // 东河新三务公开:00-16-3E-00-79-BC // 本机:F0-79-60-1E-49-FC // 应急管理局测试服务器(58.18.22.25):00-50-56-BE-3C-BC // 包头政法委:00-50-56-93-2D-29 // 内蒙古艺校:84-65-69-5C-23-AA // String mac = AddressUtil.getMacAddress(); // System.out.println(mac); // 环保统一用户:FA-16-3E-17-20-7D // 包头体育局:00-16-3E-00-F3-CE // 包头安监局业务服务器(00-50-56-93-66-AC) // 西藏日喀则:08-94-EF-6B-5A-44 // 集宁环保:FA-26-00-03-D3-C4 String license = License.getLicense("2021-05-06", "10000", "FA-26-00-03-D3-C4", "_System_License_"); System.out.println(license); } @Test public void licenseOldTest() { String license = getOldLicense("2020-04-13", "10000", "00-16-3E-00-58-B4", "CMXX0471"); System.out.println(license); } @Test public void licenseTimeOldTest() { System.out.println(checkOldLicense("CMXX0471", "MzZhMGQ1NmU4ZjM3Yjc5ZTRkNzk4OTkyYTQ0OWVjZmUtZjA0NTA5MWZlZDI3OWRlNDVjZDEwZmVjZWI1MTI2YjgtZDFjYTNhYWY1MmI0MWFjZDY4ZWJiM2JmNjkwNzliZDEtVGxwdFoyZG5Wa0Z0TVZSUmFuVXZOR0YyWVRSNlRsVk5hWGczY1ZkTVYyeFZNRTlrVDBwaVdIcFVjVFF6TmpCb1dqZFdiVGxWU1RRM01GUlBXRnBsTnc9PQ==")); } public void aesTest() throws Exception { String encode = AesUtil.aesEncoder("CMXX", "123"); //String decode = AesUtil.aesDecoder("ABCDEFGHIJKLMNRS", "vqZ8mVpLg2RqdPML+9tPxma3NrQDO/pECXWvUr/RIHE="); System.out.println(encode); //System.out.println(decode); } public void webServiceTest() { } /** * 获取旧的License * @param startTime * @param timeLong * @param mac * @param token * @return */ public static String getOldLicense(String startTime, String timeLong, String mac, String token) { String result = null; if (null == startTime || startTime.isEmpty()) { return result; } if (null == timeLong || timeLong.isEmpty()) { return result; } if (null == mac || mac.isEmpty()) { return result; } if (null == token || token.isEmpty()) { return result; } try { // 开始时间 String startTimeMD5 = DigestUtils.md5Hex(startTime); // MAC地址 String macMD5 = DigestUtils.md5Hex(mac); // 使用时长 String timeMD5 = DigestUtils.md5Hex(timeLong); // 构建license数组 List licenses = new ArrayList(); licenses.add(startTimeMD5); licenses.add(macMD5); licenses.add(timeMD5); // 构建可解密内容 StringBuilder licenseFullPart = new StringBuilder(); licenseFullPart.append(startTime).append(","); licenseFullPart.append(mac).append(","); licenseFullPart.append(timeLong).append(",").append("0"); // license String licenseFull = new String(Base64.encodeBase64(AesUtil.aesEncoder(token, licenseFullPart.toString()).getBytes("UTF-8"))); licenses.add(licenseFull); StringBuilder license = new StringBuilder(); for (String str : licenses) { if (license.length() > 0) { license.append("-"); } license.append(str); } result = new String(Base64.encodeBase64((license.toString().getBytes("UTF-8"))), "UTF-8"); } catch (Exception e) { e.printStackTrace(); return result; } return result; } public static boolean checkOldLicense(String token, String license) { SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd"); if (null == token || token.isEmpty()) { return false; } if (null == license || license.isEmpty()) { return false; } try { byte[] licenseByte = Base64.decodeBase64(license); String licenseFull = new String(licenseByte, "UTF-8"); String[] checkInfos = licenseFull.split("-"); String startTime = checkInfos[0]; String mac = checkInfos[1]; String time = checkInfos[2]; String fullInfo = checkInfos[3]; String info = new String(Base64.decodeBase64(fullInfo), "UTF-8"); String[] checkInfo = AesUtil.aesDecoder(token, info).split(","); // 校验时间 if (!startTime.equals(DigestUtils.md5Hex(checkInfo[0]))) { return false; } // 开始时间大于当前时间 if (SDF.parse(checkInfo[0]).getTime() > System.currentTimeMillis()) { return false; } // mac,mac不匹配 List macIds = AddressUtil.getMacIds(); boolean confirmMac = false; for (String macId : macIds) { if (checkInfo[1].equals(macId) && mac.equals(DigestUtils.md5Hex(checkInfo[1]))) { confirmMac = true; break; } } if (!confirmMac) { return false; } // 授权时长 if (!time.equals(DigestUtils.md5Hex(checkInfo[2]))) { return false; } // 当前有效时间 if (Integer.parseInt(checkInfo[2]) < Integer.parseInt(checkInfo[3])) { return false; } } catch (Exception e) { e.printStackTrace(); return false; } return true; } }