From d71cf3db1d965bd6aeb05f54f50c5fab83dc9a2e Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Tue, 18 Feb 2020 08:37:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9license=E7=9A=84=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/cm/security/License.java | 8 ++-- .../java/com/cm/security/utils/AesUtil.java | 43 +++++++++++++++++++ .../src/test/java/com/cm/LicenseTest.java | 2 +- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/cloud-security/src/main/java/com/cm/security/License.java b/cloud-security/src/main/java/com/cm/security/License.java index 8313101..64ba7c8 100644 --- a/cloud-security/src/main/java/com/cm/security/License.java +++ b/cloud-security/src/main/java/com/cm/security/License.java @@ -54,7 +54,7 @@ public class License { 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"))); + String licenseFull = new String(Base64.encodeBase64(AesUtil.aesCommonEncoder(token, licenseFullPart.toString()).getBytes("UTF-8"))); licenses.add(licenseFull); StringBuilder license = new StringBuilder(); for (String str : licenses) { @@ -94,7 +94,7 @@ public class License { String time = checkInfos[2]; String fullInfo = checkInfos[3]; String info = new String(Base64.decodeBase64(fullInfo), "UTF-8"); - String[] checkInfo = AesUtil.aesDecoder(token, info).split(","); + String[] checkInfo = AesUtil.aesCommonDecoder(token, info).split(","); // 校验时间 if (!startTime.equals(DigestUtils.md5Hex(checkInfo[0]))) { return false; @@ -149,7 +149,7 @@ public class License { String time = checkInfos[2]; String fullInfo = checkInfos[3]; String[] checkInfo = AesUtil - .aesDecoder(token, + .aesCommonDecoder(token, new String(Base64.decodeBase64(fullInfo), "UTF-8")) .split(","); checkInfo[3] = String.valueOf(Integer.parseInt(checkInfo[3]) + 1); @@ -160,7 +160,7 @@ public class License { } newCheckInfo.append(newCheck); } - licenseFull = new String(Base64.encodeBase64(AesUtil.aesEncoder(token, newCheckInfo.toString()).getBytes("UTF-8")), "UTF-8"); + licenseFull = new String(Base64.encodeBase64(AesUtil.aesCommonEncoder(token, newCheckInfo.toString()).getBytes("UTF-8")), "UTF-8"); result = new String(Base64.encodeBase64(new StringBuilder(startTime).append("-").append(mac).append("-") .append(time).append("-").append(licenseFull).toString().getBytes()), "UTF-8"); } catch (Exception e) { diff --git a/cloud-security/src/main/java/com/cm/security/utils/AesUtil.java b/cloud-security/src/main/java/com/cm/security/utils/AesUtil.java index 6c3d3fb..4911bca 100644 --- a/cloud-security/src/main/java/com/cm/security/utils/AesUtil.java +++ b/cloud-security/src/main/java/com/cm/security/utils/AesUtil.java @@ -5,10 +5,13 @@ import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; +import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.SecureRandom; public class AesUtil { + private static final String IV_STRING = "16-Bytes--String"; + /** * AES加密 * @@ -68,4 +71,44 @@ public class AesUtil { // 8.解密 return new String(cipher.doFinal(contentByte), "UTF-8"); } + + /** + * 通用aes加密,兼容IOS + * + * @param key + * @param content + * @return + * @throws Exception + */ + public static String aesCommonEncoder(String key, String content) throws Exception { + byte[] byteContent = content.getBytes("UTF-8"); + byte[] enCodeFormat = key.getBytes(); + SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); + byte[] initParam = IV_STRING.getBytes(); + IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam); + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); + byte[] encryptedBytes = cipher.doFinal(byteContent); + return new String(Base64.encodeBase64(encryptedBytes), "UTF-8"); + } + + /** + * aes通用解密,兼容IOS + * + * @param key + * @param content + * @return + * @throws Exception + */ + public static String aesCommonDecoder(String key, String content) throws Exception { + byte[] encryptedBytes = Base64.decodeBase64(content); + byte[] enCodeFormat = key.getBytes(); + SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES"); + byte[] initParam = IV_STRING.getBytes(); + IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam); + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); + byte[] result = cipher.doFinal(encryptedBytes); + return new String(result, "UTF-8"); + } } diff --git a/cloud-security/src/test/java/com/cm/LicenseTest.java b/cloud-security/src/test/java/com/cm/LicenseTest.java index feed536..bfda527 100644 --- a/cloud-security/src/test/java/com/cm/LicenseTest.java +++ b/cloud-security/src/test/java/com/cm/LicenseTest.java @@ -26,7 +26,7 @@ public class LicenseTest { // 本机:F0-79-60-1E-49-FC // String mac = AddressUtil.getMacAddress(); // System.out.println(mac); - String license = License.getLicense("2020-02-17", "1000", "F0-79-60-1E-49-FC", "CMXX0471"); + String license = License.getLicense("2020-02-17", "1000", "F0-79-60-1E-49-FC", "_System_License_"); System.out.println(license); }