1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject;
import java.nio.charset.StandardCharsets; import java.security.PrivateKey; import java.security.Signature; import java.util.Base64; import java.util.Random;
public class Main {
private static final String ALPHA_NUMERIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; private static final Random random = new Random();
public static String generateRandomId(int length) { StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { int index = random.nextInt(ALPHA_NUMERIC.length()); sb.append(ALPHA_NUMERIC.charAt(index)); } return sb.toString(); }
public static void main(String[] args) throws Exception {
CertificateKeyPair certificateKeyPair = ActivationCodeGeneration.readCareAndKey("idea2.crt", "idea2.key");
String licensePart = "{\"licenseId\":\"6G5NXCPJZB\",\"licenseeName\":\"洞若观火\",\"assigneeName\":\"马泽朋\",\"assigneeEmail\":\"\",\"licenseRestriction\":\"\",\"checkConcurrentUse\":false,\"products\":[{\"code\":\"PSI\",\"fallbackDate\":\"2025-08-01\",\"paidUpTo\":\"2025-08-01\",\"extended\":true},{\"code\":\"PDB\",\"fallbackDate\":\"2025-08-01\",\"paidUpTo\":\"2025-08-01\",\"extended\":true},{\"code\":\"PMYBATISHELPER\",\"fallbackDate\":\"2025-08-01\",\"paidUpTo\":\"2025-08-01\",\"extended\":true},{\"code\":\"II\",\"fallbackDate\":\"2025-08-01\",\"paidUpTo\":\"2025-08-01\",\"extended\":false},{\"code\":\"PPC\",\"fallbackDate\":\"2025-08-01\",\"paidUpTo\":\"2025-08-01\",\"extended\":true},{\"code\":\"PGO\",\"fallbackDate\":\"2025-08-01\",\"paidUpTo\":\"2025-08-01\",\"extended\":true},{\"code\":\"PSW\",\"fallbackDate\":\"2025-08-01\",\"paidUpTo\":\"2025-08-01\",\"extended\":true},{\"code\":\"PWS\",\"fallbackDate\":\"2025-08-01\",\"paidUpTo\":\"2025-08-01\",\"extended\":true},{\"code\":\"PPS\",\"fallbackDate\":\"2025-08-01\",\"paidUpTo\":\"2025-08-01\",\"extended\":true},{\"code\":\"PRB\",\"fallbackDate\":\"2025-08-01\",\"paidUpTo\":\"2025-08-01\",\"extended\":true},{\"code\":\"PCWMP\",\"fallbackDate\":\"2025-08-01\",\"paidUpTo\":\"2025-08-01\",\"extended\":true}],\"metadata\":\"0120220902PSAN000005\",\"hash\":\"TRIAL:-1078390568\",\"gracePeriodDays\":7,\"autoProlongated\":false,\"isAutoProlongated\":false}"; String licenseId = generateRandomId(10);
Gson gson = new Gson(); JsonObject jsonObject = gson.fromJson(licensePart, JsonObject.class); if (args.length > 0) { jsonObject.addProperty("licenseeName", args[0]); } else { jsonObject.addProperty("licenseeName", "专业版"); } if (args.length > 1) { jsonObject.addProperty("assigneeName", args[1]); } else { jsonObject.addProperty("assigneeName", "mazp"); }
jsonObject.addProperty("licenseId", licenseId); for (JsonElement products : jsonObject.getAsJsonArray("products")) { JsonObject product = products.getAsJsonObject(); product.addProperty("fallbackDate", "2030-08-01"); product.addProperty("paidUpTo", "2030-08-01"); } JsonObject stencil = new JsonObject(); stencil.addProperty("code","PMYBATISLOG"); stencil.addProperty("fallbackDate", "2030-08-01"); stencil.addProperty("paidUpTo", "2030-08-01"); jsonObject.getAsJsonArray("products").add(stencil);
JsonObject stencil2 = new JsonObject(); stencil2.addProperty("code","PC"); stencil2.addProperty("fallbackDate", "2030-08-01"); stencil2.addProperty("paidUpTo", "2030-08-01"); jsonObject.getAsJsonArray("products").add(stencil2);
JsonObject stencil_ws = new JsonObject(); stencil_ws.addProperty("code","WS"); stencil_ws.addProperty("fallbackDate", "2030-08-01"); stencil_ws.addProperty("paidUpTo", "2030-08-01"); jsonObject.getAsJsonArray("products").add(stencil_ws);
byte[] licensePartBytes = jsonObject.toString().getBytes(StandardCharsets.UTF_8); String licensePartBase64 = Base64.getEncoder().encodeToString(licensePartBytes); PrivateKey privateKey = certificateKeyPair.getPrivateKey();
Signature signature = Signature.getInstance("SHA1withRSA"); signature.initSign(privateKey); signature.update(licensePartBytes); byte[] signatureBytes = signature.sign();
String sigResultsBase64 = Base64.getEncoder().encodeToString(signatureBytes);
String result = licenseId + "-" + licensePartBase64 + "-" + sigResultsBase64 + "-" + Base64.getEncoder().encodeToString(certificateKeyPair.getCertificate().getEncoded()); System.out.println("激活码是:"); System.out.println(result);
}
}
|