Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to generate activation codes and keys

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use java to generate activation codes and keys". In daily operation, I believe many people have doubts about how to use java to generate activation codes and keys. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to use java to generate activation codes and keys". Next, please follow the editor to study!

Design idea of decryption and encryption

Encryption:

Using AES symmetrical encryption and decryption

7 digits: 32-bit sequence (4 bits) + key category (2 bits) + valid duration (1 bit)

After encryption, the key is 11 bits

4 digits: the first three digits, first get a random number (0 to 2500), then multiply it by 11, then convert it to a three-digit 32-digit number, and then the last digit is (machine version number)

The last 3 digits + 1 bits generate 4 digits

Envision a 15-bit key

11 bits + 4 bits

Then the keys are scrambled and confused.

Obfuscation strategy: first obtain the odd and even bits of the activation code, and then splice the odd and even bits to get the confused activation code

Odd bit + even bit

Decryption:

(1) remove confusion (reassemble and restore the confused activation code)

(2) four digits after the verification key; the verification succeeds and continues to the next step. The verification failure key is invalid.

(3) the first 11-bit key can be decrypted only if the verification is successful; the verification failure key is invalid.

(4) if the decryption is successful, it is a valid key, obtain the key information, and perform corresponding operations on the client according to the information. If the decryption fails, the key is invalid.

(5) whether the decryption is successful or not, send a request to the server, notify the server, and then carry out the corresponding operation and record.

The key category (2 digits) can be used to indicate which devices or platforms the activation code is used to activate (for example, 01 represents a platform, 02 represents an app), and the duration (1 bit) is used to indicate the valid duration of the activation code (for example, 0 for permanent, 1 for 7 days, 2 for 30 days, etc.)

Note: the first 7 digits are encrypted to 11 digits, indicating the number of activation codes that can be generated; the last 4 digits are random numbers * 11 to 32 digits and the obfuscation strategy is to activate the encryption of the code and to verify whether the activation code is valid.

Therefore, the encryption of the activation code is mainly reflected in three places:

Confusion strategy

Can 32 be divisible by 11 after it is forbidden to convert to 18?

AES symmetric encryption and decryption

Decryption and encryption tool class

CDKeyUtil.java

Import java.util.Random;/** * Created by tao. * Date: 16:43 on 2021-6-28 * description: * / public class CDKeyUtil {/ / machine version number / * Activation code generation method * @ param category key category (fixed two digits) * @ param deadline life (fixed one character) * @ activation code returned by return * / public static String createCDkey (String category, String deadline) String machineVersion) throws Exception {String CDKey = "" / / 1. Get the first four digits String sequence = getSequence (); / / 2. Generate the first seven bits String plaintext = sequence + category + deadline; / / 3. Encrypt plaintext CDKey = CDKeyEncryptUtils.AESencrypt (plaintext) .substring (0,11); / / 4. Get the last four digits String rulesSequence = CDKeyUtil.getRulesSequence (machineVersion); / / 5. Confusion operation CDKey = CDKey + rulesSequence; CDKey = confusion (CDKey); / / 6. Get the activation code return CDKey;} / * Activation code decoding method * * @ param CDKey activation code * @ return return the activation code plaintext * / public static String deCDkey (String CDKey, String machineVersion) throws Exception {/ / 1. Remove confusion String deConfusion = deConfusion (CDKey); / / 2. Extract the last four-bit sequence (the first version number, and the last three check its rules) String sequence = deConfusion.substring (deConfusion.length ()-4); / / 3. Get the last three-bit sequence and convert it to 10, and the version number String randomInt = sequence.substring (1); String version = sequence.substring (0,1); int to10 = Integer.parseInt (change32To10 (randomInt)); / / 4. Check whether the activation code is correct according to the established rules if (to10% 11 = = 0 & & version.equals (machineVersion)) {/ / 1. If the last four sequences are verified correctly, the activation code is decrypted by String secretKey = deConfusion.substring (0,11); String code = ""; try {code = CDKeyEncryptUtils.AESdecrypt (secretKey);} catch (Exception e) {e.printStackTrace (); return "activation code error" } return code;} else {return "activation code error";}} / * * get the first four bit sequence of activation code * * @ return returns the first four bit sequence of activation code * / public static String getSequence () {String sequence = "; / / 1. Get the random number int randomInt = getRandomInt (); / / 2. Convert 32-ary String to32 = change10To32 (randomInt + ""); / / 3. Complete four digits int len = to32.length (); if (len < 4) {for (int I = 0; I < 4-len; iTunes +) {to32 = "0" + to32;}} sequence = to32; return sequence } / * the four-digit rule sequence method after obtaining the activation code * * @ param machineVersion machine version number * @ return returns the four-digit rule sequence after the activation code * / public static String getRulesSequence (String machineVersion) {String rulesSequence; / / 1. According to the rule, get the first three bits / * int randomInt = new Random (). NextInt (8); String randomStr = randomInt + "" + (randomInt + 1) + (randomInt + 2); * / / 1. According to the rule, get the first three digits int randomInt = new Random (). NextInt (2500); String randomStr = (randomInt * 11) + "; / / 2. Transfer to 32-ary String to32 = change10To32 (randomStr); / / 3. Complete the three digits int len = to32.length (); if (len < 3) {for (int I = 0; I < 3-len; iTunes +) {to32 = "0" + to32;}} / / 4. Concatenate the fourth bit rulesSequence = machineVersion + to32; return rulesSequence;} / * Activation Code obfuscation method * Odd digits + even digits * * @ return returns the sequence * / public static String confusion (String CDKey) {String deCDKey = ""; / / 1. Get the odd string String odd = "; for (int I = 0; I < CDKey.length (); I = I + 2) {odd = odd + CDKey.charAt (I);} / / 2. Get the even-digit string String even = "; for (int I = 1; I < CDKey.length (); I = I + 2) {even = even + CDKey.charAt (I);} / / 3. Concatenate deCDKey = odd + even; return deCDKey;} / * * Activation Code unobfuscation method * * @ return returns the sequence * / public static String deConfusion (String deCDKey) {String CDKey = ""; / / 1. Split int oddCount = (deCDKey.length () / 2) + (deCDKey.length ()% 2); String odd = deCDKey.substring (0, oddCount); String even = deCDKey.substring (oddCount); / / 2. Recovery activation code if (odd.length () = = even.length ()) {for (int I = 0; I < odd.length ()) {CDKey = CDKey + odd.charAt (I) + even.charAt (I);}} else {for (int I = 0; I < even.length () CDKey +) {CDKey = CDKey + odd.charAt (I) + even.charAt (I);} CDKey = CDKey + odd.charAt (odd.length ()-1);} return CDKey } / * method of converting 10 to 32 system * num the number to be converted the number of from source number the base to to convert to * * @ param num 10 (string) * @ return the 32-ary string of the conversion result * / public static String change10To32 (String num) {int from = 10; int to = 32 Return new java.math.BigInteger (num, from) .toString (to);} / * * 32-to-decimal method * num the number to be converted from the from source number to the decimal to to be converted to the decimal string * / public static String change32To10 (String num) {int f = 32 of the result of the return conversion Int t = 10; return new java.math.BigInteger (num, f) .toString (t) } / * * generate random integers between [min, max] * min minimum integers (fixed 0) * max maximum integers (fixed 1000000) * * @ return returns random numbers * @ author tao * / public static int getRandomInt () {int min = 0; int max = 1000000 between min---max Return new Random () .nextInt (max)% (max-min + 1) + min;} / * * enumerate the date and return the number of days * / public static int duetimeEnum (String code) {switch (code) {case "0": return 36500; case "1": return 7 Case "2": return 30; case "3": return 90; case "4": return 180; case "5": return 365; default: return 30;}

AES encryption and decryption are used: CDKeyEncryptUtils.java

Import org.apache.commons.codec.binary.Base64;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;/** * Created by tao. * Date: 16:37 on 2021-6-28 * description: * / public class CDKeyEncryptUtils {/ /-AES- private static final String KEY = "12055296"; / / key, must be 16-bit private static final String OFFSET = "12055296"; / / offset private static final String ENCODING = "UTF-8" / / Encoding private static final String ALGORITHM = "DES"; / / algorithm private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding" / / default encryption algorithm, CBC mode public static String AESencrypt (String data) throws Exception {/ / specify algorithm, get Cipher object (DES/CBC/PKCS5Padding: algorithm, working mode, fill mode) Cipher cipher = Cipher.getInstance (CIPHER_ALGORITHM) / / initialize the key specification SecretKeySpec skeySpec = new SecretKeySpec (KEY.getBytes ("ASCII"), ALGORITHM) according to the custom encryption key and algorithm mode; / / CBC mode offset IV IvParameterSpec iv = new IvParameterSpec (OFFSET.getBytes ()) / / initialize the encryption mode cipher.init (Cipher.ENCRYPT_MODE, skeySpec, iv); / / reset Cipher byte [] encrypted = cipher.doFinal (data.getBytes (ENCODING)) when the single part encryption ends; / / use BASE64 to transcode return new Base64 () .encodeToString (encrypted) after encryption } / * AES decryption * * @ param data * @ return String * @ author tao * @ date 2021-6-15 16:46:07 * / public static String AESdecrypt (String data) throws Exception {/ / specify algorithm, Get the Cipher object (DES/CBC/PKCS5Padding: the algorithm is Working mode, filling mode) Cipher cipher = Cipher.getInstance (CIPHER_ALGORITHM) / / initialize the key specification SecretKeySpec skeySpec = new SecretKeySpec (KEY.getBytes ("ASCII"), ALGORITHM) according to the custom encryption key and algorithm mode; / / CBC mode offset IV IvParameterSpec iv = new IvParameterSpec (OFFSET.getBytes ()) / / initialize decryption mode cipher.init (Cipher.DECRYPT_MODE, skeySpec, iv); / / decode byte [] buffer = new Base64 () .decode (data) with base64 first; / / end single-part encryption and reset Cipher byte [] encrypted = cipher.doFinal (buffer) Return new String (encrypted, ENCODING);}}

Where the key of AES is 12055296 and is set to 8 bits, the secret ciphertext is 11 bits, and the encryption algorithm is "DES"

Activation code generation test public static void main (String [] args) throws Exception {for (int I = 0; I < 10; iTunes +) {String CDKey = CDKeyUtil.createCDkey ("01", "0", "1"); System.out.println ("activation code:" + CDKey); String deCDkey = CDKeyUtil.deCDkey (CDKey, "1") System.out.println ("activation code decryption:" + deCDkey);}}

Execution result:

At this point, the study on "how to use java to generate activation codes and keys" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report