修改license的加密方式

This commit is contained in:
wenc000 2020-02-18 08:37:27 +08:00
parent 0252086b72
commit d71cf3db1d
3 changed files with 48 additions and 5 deletions

View File

@ -54,7 +54,7 @@ public class License {
licenseFullPart.append(mac).append(","); licenseFullPart.append(mac).append(",");
licenseFullPart.append(timeLong).append(",").append("0"); licenseFullPart.append(timeLong).append(",").append("0");
// license // 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); licenses.add(licenseFull);
StringBuilder license = new StringBuilder(); StringBuilder license = new StringBuilder();
for (String str : licenses) { for (String str : licenses) {
@ -94,7 +94,7 @@ public class License {
String time = checkInfos[2]; String time = checkInfos[2];
String fullInfo = checkInfos[3]; String fullInfo = checkInfos[3];
String info = new String(Base64.decodeBase64(fullInfo), "UTF-8"); 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]))) { if (!startTime.equals(DigestUtils.md5Hex(checkInfo[0]))) {
return false; return false;
@ -149,7 +149,7 @@ public class License {
String time = checkInfos[2]; String time = checkInfos[2];
String fullInfo = checkInfos[3]; String fullInfo = checkInfos[3];
String[] checkInfo = AesUtil String[] checkInfo = AesUtil
.aesDecoder(token, .aesCommonDecoder(token,
new String(Base64.decodeBase64(fullInfo), "UTF-8")) new String(Base64.decodeBase64(fullInfo), "UTF-8"))
.split(","); .split(",");
checkInfo[3] = String.valueOf(Integer.parseInt(checkInfo[3]) + 1); checkInfo[3] = String.valueOf(Integer.parseInt(checkInfo[3]) + 1);
@ -160,7 +160,7 @@ public class License {
} }
newCheckInfo.append(newCheck); 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("-") result = new String(Base64.encodeBase64(new StringBuilder(startTime).append("-").append(mac).append("-")
.append(time).append("-").append(licenseFull).toString().getBytes()), "UTF-8"); .append(time).append("-").append(licenseFull).toString().getBytes()), "UTF-8");
} catch (Exception e) { } catch (Exception e) {

View File

@ -5,10 +5,13 @@ import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator; import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom; import java.security.SecureRandom;
public class AesUtil { public class AesUtil {
private static final String IV_STRING = "16-Bytes--String";
/** /**
* AES加密 * AES加密
* *
@ -68,4 +71,44 @@ public class AesUtil {
// 8.解密 // 8.解密
return new String(cipher.doFinal(contentByte), "UTF-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");
}
} }

View File

@ -26,7 +26,7 @@ public class LicenseTest {
// 本机F0-79-60-1E-49-FC // 本机F0-79-60-1E-49-FC
// String mac = AddressUtil.getMacAddress(); // String mac = AddressUtil.getMacAddress();
// System.out.println(mac); // 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); System.out.println(license);
} }