|
| 1 | +package io.cryptolens.methods; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import io.cryptolens.*; |
| 5 | +import io.cryptolens.internal.HelperMethods; |
| 6 | +import io.cryptolens.models.ActivateModel; |
| 7 | +import io.cryptolens.models.ActivateResult; |
| 8 | +import io.cryptolens.models.BasicResult; |
| 9 | +import io.cryptolens.models.LicenseKey; |
| 10 | + |
| 11 | +import java.lang.reflect.Field; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.Base64; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +public class Key { |
| 18 | + |
| 19 | + /** |
| 20 | + * Calls the Activate method (https://app.cryptolens.io/docs/api/v3/Activate). |
| 21 | + * @param token |
| 22 | + * @param RSAPubKey |
| 23 | + * @param model |
| 24 | + * @return |
| 25 | + */ |
| 26 | + public static LicenseKey Activate (String token, String RSAPubKey, ActivateModel model) { |
| 27 | + |
| 28 | + Map<String,String> extraParams = new HashMap<>(); |
| 29 | + |
| 30 | + // force sign and the new protocol (only in activate) |
| 31 | + extraParams.put("Sign", "true"); |
| 32 | + extraParams.put("SignMethod", "1"); |
| 33 | + extraParams.put("token", token); |
| 34 | + |
| 35 | + ActivateResult result = HelperMethods.SendRequestToWebAPI("key/activate", model, extraParams, ActivateResult.class); |
| 36 | + |
| 37 | + if(result.result == 1) { |
| 38 | + System.err.println("The server returned an error: " + result.message); |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + Base64.Decoder decoder = Base64.getDecoder(); |
| 44 | + |
| 45 | + byte[] licenseKey = decoder.decode(result.licenseKey); |
| 46 | + byte[] signature = decoder.decode(result.signature); |
| 47 | + |
| 48 | + // setting up the signatures |
| 49 | + JavaSecuritySignatureVerifier signatureVerifier = new JavaSecuritySignatureVerifier(); |
| 50 | + int mstart = RSAPubKey.indexOf("<Modulus>"); |
| 51 | + int mend = RSAPubKey.indexOf("</Modulus>"); |
| 52 | + int estart = RSAPubKey.indexOf("<Exponent>"); |
| 53 | + int eend = RSAPubKey.indexOf("</Exponent>"); |
| 54 | + signatureVerifier.setModulusBase64(RSAPubKey.substring(mstart + 9, mend)); |
| 55 | + signatureVerifier.setExponentBase64(RSAPubKey.substring(estart + 10, eend)); |
| 56 | + |
| 57 | + if (!signatureVerifier.verify(licenseKey, signature)) { |
| 58 | + System.err.println("Signature check failed"); |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + String s = new String(licenseKey, StandardCharsets.UTF_8); |
| 63 | + LicenseKey license = new Gson().fromJson(s, LicenseKey.class); |
| 64 | + license.RawResponse = result.RawResponse; |
| 65 | + |
| 66 | + return license; |
| 67 | + } catch (Exception ex) { |
| 68 | + System.err.println(ex.getStackTrace()); |
| 69 | + } |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments