package org.example;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
public class jiemi {
private static String byte2hex (byte[] data) {
StringBuilder hexString = new StringBuilder ();
for (byte b : data) {
String hex = Integer.toHexString (0xFF & b);
if (hex.length () == 1) {
hexString.append ( }
hexString.append (hex);
}
return hexString.toString ().toLowerCase ();
}
public static String encrypt (String input, String key) throws Exception {
if (key.length () > 16) {
key = key.substring (0, 16);
}
SecretKeySpec secretKeySpec = new SecretKeySpec (key.getBytes (StandardCharsets.US_ASCII), "AES");
Cipher cipher = Cipher.getInstance ("AES");
cipher.init (Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encrypted = cipher.doFinal (input.getBytes ());
return byte2hex (encrypted);
}
public static String calculatePassword (int userId, int addValue, boolean needTime, long days, long hours, long minutes, long seconds) {
long totalMilliseconds = (days * 24 * 60 * 60 + hours * 60 * 60 + minutes * 60 + seconds) * 1000;
String hexString = Integer.toHexString ( (userId / 2) + addValue);
if (needTime) {
hexString += "z" + totalMilliseconds;
}
try {
return encrypt (hexString, "Format2044153997");
} catch (Exception e) {
e.printStackTrace ();
return null;
}
}
public static void main (String[] args) {
int userId = 2; // 用户ID
int addValue = 8; // KEY
boolean needTime = false; // 是否要时间
long days = 1, hours = 2, minutes = 30, seconds = 45; // 示例时间
String calculatedPassword = calculatePassword (userId, addValue, needTime, days, hours, minutes, seconds);
System.out.println ("Calculated Password: " + calculatedPassword);
}
}