[Java] 纯文本查看 复制代码
@Test
void regCode() {
// 机器码
String pcCode = "09602-11802-62920-20730";
// 有效时长 2029-12-28 超过这个的 不一定能注册成哈
String regDate = "20291228";
String text = pcCode.replace("-", "");
if (text.length() > 10) {
text = text.substring(0, 10) + text.substring(text.length() - 5);
}
String regCode = m4(SecureUtil.md5(m12(text)).toUpperCase());
// 年份yyyy 后2位对应注册码 5-6位
// 6,7,B=>1 对应1几年 8,9,C=>2 对应2几年
String[] year3 = { "8", "9", "C" };
// 年份第四位对照关系
String[] year41 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
String[] year42 = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K" };
// 月份MM 对应注册码7-8位
// 月份第一位, 6,7,B=>1 对应1 4,5,A=>0 对应0 月份第二位对照关系同年份第四位对照关系
String[] month10 = { "4", "5", "A" };
String[] month11 = { "6", "7", "B" };
// 日dd对应注册码13,14位
// 日dd第一位A,B,C=> 0,1,2 第二位0-8不用对应直接用
Random rd = new Random();
int i = rd.nextInt(3); // Random value between 0 and 2 (inclusive)
// 注册码
String y3 = regDate.substring(2, 3); // 年第三位
String y4 = regDate.substring(3, 4); // 年第四位
String m1 = regDate.substring(4, 5); // 月第一位
String m2 = regDate.substring(5, 6); // 月第二位
String d1 = regDate.substring(6, 7); // 日第一位
String d2 = regDate.substring(7, 8); // 日第二位
String[] month = month10;
if ("1".equals(m1)) month = month11;
// 注册码拼接
String zccode = regCode.substring(0, 4) + year3[i] + year42[findIndex(year41, y4)]
+ month[i] + year42[findIndex(year41, m2)] + regCode.substring(4, 8)
+ year42[findIndex(year41, d1)] + d2 + "2"; // 固定1或2均可
log.info("res req code --> {}", zccode);
}
int findIndex(String[] array, String value) {
for (int index = 0; index < array.length; index++) {
if (array[index].equals(value)) {
return index;
}
}
throw new IllegalArgumentException("Value not found in array: " + value);
}
String m12(String string_7) {
int length = string_7.length();
long num = 0L;
if (length > 0) {
for (int i = 0; i < length; i++) {
int num3 = (int) string_7.charAt(i);
int num4 = num3 + 7;
int num5 = num3 * 5;
num += smethod_8(num3, num4, num5);
num += smethod_9(num3, num4, num5);
num += smethod_10(num3, num4, num5);
num *= smethod_11(num3, num4, num5) + 12L;
if (num > 100000000L) {
num = Long.parseLong(Long.toString(num).substring(Math.max(0, Long.toString(num).length() - 7)));
}
}
String text = Long.toString(num);
if (text.length() >= 6) {
return text.substring(text.length() - 6);
} else {
return Long.toString(num + 523109L).substring(Math.max(0, Long.toString(num + 523109L).length() - 6));
}
}
return "";
}
String m4(String string_7) {
String text2 = "";
String text3 = "";
// 构建 text2
text2 = string_7.substring(10, 12) +
string_7.substring(0, 2) +
string_7.substring(14, 16) +
string_7.substring(21, 23) +
string_7.substring(6, 8) +
string_7.substring(4, 6);
// 处理 text2
for (int num = 1; num <= 12; num++) {
String charAtIndex = text2.substring(num - 1, num);
if (isNumeric(charAtIndex)) {
int numValue = Integer.parseInt(charAtIndex);
text3 += Integer.toString(numValue ^ 5);
} else {
char charValue = (char) (charAtIndex.charAt(0) + 20 - num);
String text4 = Character.toString(charValue);
if (text4.equals("O")) {
text3 += "0";
} else {
text3 += text4;
}
}
}
// 替换 "0" 为 "7"
text3 = text3.replace("0", "7");
// 重新排列 text3
String result = text3.substring(8, 10) +
text3.substring(4, 6) +
text3.substring(10, 12) +
text3.substring(6, 8);
return result;
}
boolean isNumeric(String str) {
return str.matches("\\d");
}
int smethod_8(int int_0, int int_1, int int_2) {
return (int_0 << 2) | (int_1 & int_2);
}
int smethod_9(int int_0, int int_1, int int_2) {
return int_0 & int_2 & ((int_1 << 3) | int_2);
}
int smethod_10(int int_0, int int_1, int int_2) {
return (int_0 & (int_2 << 2)) | ((int_1 << 3) & int_2);
}
int smethod_11(int int_0, int int_1, int int_2) {
return (int_0 << 4) & (int_2 << 2) & ((int_1 << 2) | int_2);
}