· 7 years ago · Aug 03, 2018, 06:40 PM
1import javax.crypto.Cipher;
2import javax.crypto.spec.SecretKeySpec;
3import java.io.ByteArrayInputStream;
4import java.io.InputStream;
5import java.security.MessageDigest;
6import java.security.NoSuchAlgorithmException;
7import java.text.DateFormat;
8import java.text.SimpleDateFormat;
9import java.util.Calendar;
10import java.util.Date;
11import java.util.Properties;
12import java.util.Scanner;
13
14class Keygen {
15 private String companyName;
16 private String contactEmail;
17 private String contactName;
18 private String licenseStartDate;
19 private String licenseStopDate;
20 private String databaseName;
21 private String databaseServer;
22 private String nbMaxUsers;
23
24 private String licenseType = "REGISTERED";
25 private String supportType = "SUPPORT_PLATINUM";
26 private String xStudioVersion = "";
27
28 private final byte[] key = {31, 109, 13, 121, 64, 115, -17, -3, -27, 73, -89, -43, -50, -104, -39, 38, 31, 109, 13, 121, 64, 115, -17, -3};
29
30 Keygen() {
31 }
32
33 public void getLicenseInfo() {
34 Scanner scan = new Scanner(System.in);
35 System.out.println("Please enter license details");
36
37 System.out.print("Company name: ");
38 this.companyName = scan.nextLine();
39
40 System.out.print("Contact email: ");
41 this.contactEmail = scan.nextLine();
42
43 System.out.print("Contact name: ");
44 this.contactName = scan.nextLine();
45
46 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
47 Date startDate = new Date();
48 this.licenseStartDate = dateFormat.format(startDate);
49
50 System.out.print("License duration (years): ");
51 Integer years = Integer.valueOf(scan.nextLine());
52 Calendar endDate = Calendar.getInstance();
53 endDate.setTime(startDate);
54 endDate.add(Calendar.YEAR, years);
55 this.licenseStopDate = dateFormat.format(endDate.getTime());
56
57 System.out.print("Database server: ");
58 this.databaseName = scan.nextLine();
59
60 System.out.print("Database name: ");
61 this.databaseServer = scan.nextLine();
62
63 System.out.print("Max user count: ");
64 this.nbMaxUsers = Integer.toString(scan.nextInt());
65 }
66
67 public void decode(String license) {
68 final String encryptedProperties;
69
70 encryptedProperties = license.substring(8, license.length() - 40 - 2);
71
72 byte[] content = stringToByteArrayString(encryptedProperties);
73 byte[] decodedProperties = this.decrypt(content);
74 InputStream targetStream = new ByteArrayInputStream(decodedProperties);
75 Properties properties = new Properties();
76 try {
77 properties.load(targetStream);
78 } catch (Exception localException) {
79 System.out.println("Error loading properties");
80 System.exit(1);
81 }
82
83 this.companyName = properties.getProperty("customer.company.name");
84 this.contactEmail = properties.getProperty("customer.contact.email");
85 this.contactName = properties.getProperty("customer.contact.name");
86 this.licenseStartDate = properties.getProperty("license.start");
87 this.licenseStopDate = properties.getProperty("license.stop");
88 this.licenseType = properties.getProperty("license.type");
89 this.databaseName = properties.getProperty("plugin.database.name");
90 this.nbMaxUsers = properties.getProperty("plugin.database.nbmaxusers");
91 this.databaseServer = properties.getProperty("plugin.database.server");
92 this.supportType = properties.getProperty("plugin.name");
93 this.xStudioVersion = properties.getProperty("xstudio.version");
94
95 }
96
97 public String generateLicense() {
98 String properties = this.formatValues();
99 byte[] encoded = this.encrypt(properties.getBytes());
100 String encodedProperties = bytesToHex(encoded);
101 String digest = this.computeDigest(properties);
102
103 StringBuilder license = new StringBuilder("E6A5A1D0");
104 license.append(encodedProperties.toUpperCase());
105 license.append(digest.toUpperCase());
106 license.append("01");
107 return license.toString();
108 }
109
110 public String formatValues() {
111 StringBuilder sb = new StringBuilder("");
112 sb.append("customer.company.name = ").append(this.companyName).append("\n");
113 sb.append("customer.contact.email = ").append(this.contactEmail).append("\n");
114 sb.append("customer.contact.name = ").append(this.contactName).append("\n");
115 sb.append("license.start = ").append(this.licenseStartDate).append("\n");
116 sb.append("license.stop = ").append(this.licenseStopDate).append("\n");
117 sb.append("license.type = ").append(this.licenseType).append("\n");
118 sb.append("plugin.database.name = ").append(this.databaseName).append("\n");
119 sb.append("plugin.database.nbmaxusers = ").append(this.nbMaxUsers).append("\n");
120 sb.append("plugin.database.server = ").append(this.databaseServer).append("\n");
121 sb.append("plugin.name = ").append(this.supportType).append("\n");
122 sb.append("xstudio.version = ").append(this.xStudioVersion).append("\n");
123 return sb.toString();
124 }
125
126 private String computeDigest(String properties) {
127 try {
128 return sha1(properties);
129 } catch (NoSuchAlgorithmException localException) {
130 System.out.println("Error loading properties");
131 }
132 return null;
133 }
134
135
136 private byte[] encrypt(byte[] strToEncrypt) {
137 try {
138 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5PADDING");
139 final SecretKeySpec secretKey = new SecretKeySpec(this.key, "DESede");
140 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
141 return cipher.doFinal(strToEncrypt);
142 } catch (Exception e) {
143 e.printStackTrace();
144 }
145 return null;
146
147 }
148
149 private byte[] decrypt(byte[] strToDecrypt) {
150 try {
151 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5PADDING");
152 final SecretKeySpec secretKey = new SecretKeySpec(this.key, "DESede");
153 cipher.init(Cipher.DECRYPT_MODE, secretKey);
154 return cipher.doFinal(strToDecrypt);
155 } catch (Exception e) {
156 e.printStackTrace();
157
158 }
159 return null;
160 }
161
162 private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
163
164 public static String bytesToHex(byte[] bytes) {
165 char[] hexChars = new char[bytes.length * 2];
166 for (int j = 0; j < bytes.length; j++) {
167 int v = bytes[j] & 0xFF;
168 hexChars[j * 2] = hexArray[v >>> 4];
169 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
170 }
171 return new String(hexChars);
172 }
173
174
175 public static byte[] stringToByteArrayString(String paramString) {
176 byte[] arrayOfByte = new byte[paramString.length() / 2];
177 for (int i = 0; i < paramString.length(); i += 2) {
178 try {
179 arrayOfByte[(i / 2)] = ((byte) Short.parseShort(paramString.substring(i, i + 2), 16));
180 } catch (NumberFormatException localNumberFormatException) {
181 }
182 }
183 return arrayOfByte;
184 }
185
186
187 static String sha1(String input) throws NoSuchAlgorithmException {
188 MessageDigest mDigest = MessageDigest.getInstance("SHA1");
189 byte[] result = mDigest.digest(input.getBytes());
190 StringBuffer sb = new StringBuffer();
191 for (int i = 0; i < result.length; i++) {
192 sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
193 }
194
195 return sb.toString();
196 }
197}
198
199
200public class Main {
201 public static void main(String args[]) {
202 System.out.println("XStudio key");
203
204 Scanner scan = new Scanner(System.in);
205 System.out.println("* Check license (1)");
206 System.out.println("* Generate license (2)");
207
208 System.out.print("Choice 1-2: ");
209 Integer choice = scan.nextInt();
210
211 if (choice == 1) {
212 System.out.print("Enter your license: ");
213 String license = scan.next();
214 Keygen keygen = new Keygen();
215 keygen.decode(license);
216 System.out.println();
217 System.out.println(keygen.formatValues());
218 } else if (choice == 2) {
219 Keygen keygen = new Keygen();
220 keygen.getLicenseInfo();
221 String license = keygen.generateLicense();
222 System.out.println(keygen.formatValues());
223 System.out.print("Your license is: ");
224 System.out.println();
225 System.out.println(license);
226 } else {
227 System.out.println("Invalid choice…");
228 }
229 }
230}