Decryption and encryption design ideas
Encryption:
Using AES symmetric encryption and decryption
7 digits: 32-digit sequence (4 digits) Key category ( 2 digits) Validity duration (1 digit)
The encrypted key is 11 digits
4 digits: for the first three digits, first obtain a random number (0 to 2500), then multiply by 11, and then convert to A three-digit hexadecimal number, and then the last digit is (machine version number),
The last 3 digits and 1 digit generate a 4-digit number
Expected 15-digit key
11 digits and 4 digits
Then Keys are scrambled and confused
Confusion strategy: First obtain the odd and even digits of the activation code respectively, and then splice the odd and even digits to obtain the obfuscated activation code
Odd digits and even digits
Decryption:
(1) Deobfuscate (reorganize and restore the obfuscated activation code)
(2) Verify the last four digits of the key; if the verification is successful, continue One-step operation, the key is invalid if the verification fails
(3) The first eleven keys can be decrypted only if the verification is successful; the key is invalid if the verification fails
(4) The decryption is successful, indicating that it is a valid key , obtain the key information, and perform corresponding operations on the client based on the information; decryption fails, indicating that the key is invalid
(5) Regardless of whether the decryption is successful or not, send a request to the server, notify the server, and then perform corresponding operations and records
Among them: the key category (2 digits) can be used to indicate which devices or platforms the activation code is used to activate (such as 01 represents a certain platform, 02 represents a certain app), duration (1 digit) Used to indicate the validity period of the activation code (such as 0 means permanent, 1 means 7 days, 2 means 30 days, etc.)
Note: The first 7 digits are encrypted to 11 digits, indicating that the activation code can be generated number; the last 4 digits are random numbers * 11 to 32 decimal and the obfuscation strategy is for the encryption of the activation code and is used to verify whether the activation code is valid
Therefore, the activation The encryption of the code is mainly reflected in three places:
Confusion strategy
32 It is forbidden to convert to hexadecimal and whether it can be divisible by 11
AES symmetric encryption, decryption
Decryption and encryption tool class
CDKeyUtil.java
import java.util.Random; /** * Created by tao. * Date: 2021/6/28 16:43 * 描述: */ public class CDKeyUtil { //机器版本号 /** * 激活码生成方法 * * @param category 密钥类别(固定两位数字) * @param deadline 使用期限(固定一位字符) * @return 返回的激活码 */ public static String createCDkey(String category, String deadline, String machineVersion) throws Exception { String CDKey = ""; //1. 获取前四位 String sequence = getSequence(); //2. 生成前七位 String plaintext = sequence + category + deadline; //3.对明文进行加密 CDKey = CDKeyEncryptUtils.AESencrypt(plaintext).substring(0, 11); //4.获取后四位 String rulesSequence = CDKeyUtil.getRulesSequence(machineVersion); //5.混淆操作 CDKey = CDKey + rulesSequence; CDKey = confusion(CDKey); //6.得到激活码 return CDKey; } /** * 激活码解码方法 * * @param CDKey 激活码 * @return 返回激活码明文 */ public static String deCDkey(String CDKey, String machineVersion) throws Exception { //1. 解除混淆 String deConfusion = deConfusion(CDKey); //2. 提取后四位序列(第1位版本号,后三位校验其规则) String sequence = deConfusion.substring(deConfusion.length() - 4); //3. 获取后三位序列并且转为10进制,和版本号 String randomInt = sequence.substring(1); String version = sequence.substring(0, 1); int to10 = Integer.parseInt(change32To10(randomInt)); //4. 根据既定规则校验激活码是否正确 if (to10 % 11 == 0 && version.equals(machineVersion)) { //1. 如果后四位序列校验正确,则对激活码进行解密操作 String secretKey = deConfusion.substring(0, 11); String code = ""; try { code = CDKeyEncryptUtils.AESdecrypt(secretKey); } catch (Exception e) { e.printStackTrace(); return "激活码错误"; } return code; } else { return "激活码错误"; } } /** * 获得激活码前四位序列方法 * * @return 返回激活码前四位序列 */ public static String getSequence() { String sequence = ""; //1. 获取随机数 int randomInt = getRandomInt(); //2. 转32进制 String to32 = change10To32(randomInt + ""); //3. 补全四位 int len = to32.length(); if (len < 4) { for (int i = 0; i < 4 - len; i++) { to32 = "0" + to32; } } sequence = to32; return sequence; } /** * 获得激活码后四位规则序列方法 * * @param machineVersion 机器版本号 * @return 返回激活码后四位规则序列 */ public static String getRulesSequence(String machineVersion) { String rulesSequence; //1. 按照规则获取前三位 /*int randomInt = new Random().nextInt(8); String randomStr = randomInt + "" + (randomInt + 1) + (randomInt + 2);*/ //1. 按照规则获取前三位 int randomInt = new Random().nextInt(2500); String randomStr = (randomInt * 11) + ""; //2. 转32进制 String to32 = change10To32(randomStr); //3. 补全三位 int len = to32.length(); if (len < 3) { for (int i = 0; i < 3 - len; i++) { to32 = "0" + to32; } } //4.拼接第四位 rulesSequence = machineVersion + to32; return rulesSequence; } /** * 激活码混淆方法 * 奇数位+偶数位 * * @return 返回激活码混淆后的序列 */ public static String confusion(String CDKey) { String deCDKey = ""; //1.获取奇数位字串 String odd = ""; for (int i = 0; i < CDKey.length(); i = i + 2) { odd = odd + CDKey.charAt(i); } //2.获取偶数位字串 String even = ""; for (int i = 1; i < CDKey.length(); i = i + 2) { even = even + CDKey.charAt(i); } //3.拼接 deCDKey = odd + even; return deCDKey; } /** * 激活码解除混淆方法 * * @return 返回激活码解除混淆后的序列 */ public static String deConfusion(String deCDKey) { String CDKey = ""; //1. 拆分 int oddCount = (deCDKey.length() / 2) + (deCDKey.length() % 2); String odd = deCDKey.substring(0, oddCount); String even = deCDKey.substring(oddCount); //2. 复原激活码 if (odd.length() == even.length()) { for (int i = 0; i < odd.length(); i++) { CDKey = CDKey + odd.charAt(i) + even.charAt(i); } } else { for (int i = 0; i < even.length(); i++) { CDKey = CDKey + odd.charAt(i) + even.charAt(i); } CDKey = CDKey + odd.charAt(odd.length() - 1); } return CDKey; } /** * 10进制转32进制的方法 * num 要转换的数 from源数的进制 to要转换成的进制 * * @param num 10进制(字符串) * @return 转换结果的32进制字符串 */ public static String change10To32(String num) { int from = 10; int to = 32; return new java.math.BigInteger(num, from).toString(to); } /** * 32进制转10进制的方法 * num 要转换的数 from源数的进制 to要转换成的进制 * * @param num 10进制(字符串) * @return 转换结果的10进制字符串 */ public static String change32To10(String num) { int f = 32; int t = 10; return new java.math.BigInteger(num, f).toString(t); } /** * 生成[min, max]之间的随机整数 * min 最小整数(固定0) * max 最大整数(固定1000000) * * @return 返回min———max之间的随机数 * @author tao */ public static int getRandomInt() { int min = 0; int max = 1000000; return new Random().nextInt(max) % (max - min + 1) + min; } /* * 枚举日期,返回天数 */ 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; } } }
used To AES encryption and decryption: 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: 2021/6/28 16:37 * 描述: */ public class CDKeyEncryptUtils { //--------------AES--------------- private static final String KEY = "12055296"; // 密匙,必须16位 private static final String OFFSET = "12055296"; // 偏移量 private static final String ENCODING = "UTF-8"; // 编码 private static final String ALGORITHM = "DES"; //算法 private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding"; // 默认的加密算法,CBC模式 public static String AESencrypt(String data) throws Exception { //指定算法、获取Cipher对象(DES/CBC/PKCS5Padding:算法为,工作模式,填充模式) Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); //根据自定义的加密密匙和算法模式初始化密钥规范 SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("ASCII"), ALGORITHM); //CBC模式偏移量IV IvParameterSpec iv = new IvParameterSpec(OFFSET.getBytes()); //初始化加密模式 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); //单部分加密结束,重置Cipher byte[] encrypted = cipher.doFinal(data.getBytes(ENCODING)); //加密后再使用BASE64做转码 return new Base64().encodeToString(encrypted); } /** * AES解密 * * @param data * @return String * @author tao * @date 2021-6-15 16:46:07 */ public static String AESdecrypt(String data) throws Exception { //指定算法、获取Cipher对象(DES/CBC/PKCS5Padding:算法为,工作模式,填充模式) Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); //根据自定义的加密密匙和算法模式初始化密钥规范 SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("ASCII"), ALGORITHM); //CBC模式偏移量IV IvParameterSpec iv = new IvParameterSpec(OFFSET.getBytes()); //初始化解密模式 cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); //先用base64解码 byte[] buffer = new Base64().decode(data); //单部分加密结束,重置Cipher byte[] encrypted = cipher.doFinal(buffer); return new String(encrypted, ENCODING); } }
The AES key is 12055296, set to 8 bits, then 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; i++) { String CDKey = CDKeyUtil.createCDkey("01", "0", "1"); System.out.println("激活码:" + CDKey); String deCDkey = CDKeyUtil.deCDkey(CDKey, "1"); System.out.println("激活码解密:" + deCDkey); } }
Execution result:
The above is the detailed content of How to generate activation codes and keys using Java.. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool