skyouc
2025-05-26 83bebf33f2a141a13a7adef66d09b43145fc29e9
rsf-server/src/main/Test/ChineseMD5Util.java
@@ -1,70 +1,38 @@
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
public class ChineseMD5Util {
    /**
     * 对汉字字符串进行MD5加密
     * @param input 要加密的汉字字符串
     * @return 32位小写MD5哈希值
     * @throws NoSuchAlgorithmException
     */
    public static String md5Chinese(String input) throws NoSuchAlgorithmException {
        if (input == null) {
            return null;
    public static List<Integer> getSelectedIndices(int[] arr) {
        List<Integer> indices = new ArrayList<>();
        int index = 0;
        int step = 3;
        while (index < arr.length) {
            indices.add(index);
            index += step;
            step = (step == 3) ? 1 : 3;
        }
        // 统一使用UTF-8编码
        byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest(inputBytes);
        // 将字节数组转换为16进制字符串
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
        return indices;
    }
    /**
     * 验证汉字字符串与MD5哈希是否匹配
     * @param input 要验证的汉字字符串
     * @param md5Hash 存储的MD5哈希值
     * @return 如果匹配返回true,否则false
     */
    public static boolean verifyChinese(String input, String md5Hash) {
        try {
            String computedHash = md5Chinese(input);
            return computedHash.equals(md5Hash.toLowerCase());
        } catch (NoSuchAlgorithmException e) {
            // 理论上不会发生,因为MD5是Java标准库支持的
            return false;
    public static Integer getNextSelectedValue(int[] arr, int inputIndex) {
        List<Integer> selectedIndices = getSelectedIndices(arr);
        int currentPos = selectedIndices.indexOf(inputIndex);
        if (currentPos != -1 && currentPos + 1 < selectedIndices.size()) {
            return arr[selectedIndices.get(currentPos + 1)];
        }
        return null; // 或按需处理
    }
    public static void main(String[] args) {
        try {
            String chineseText = "你好,世界!";
        int[] arr = {2, 5, 8, 10, 15, 20, 25, 30, 35, 40, 45, 50, 15, 40, 32, 48};
        System.out.println("输入 0,输出: " + getNextSelectedValue(arr, 0)); // 10
        System.out.println("输入 3,输出: " + getNextSelectedValue(arr, 3)); // 15
        System.out.println("输入 4,输出: " + getNextSelectedValue(arr, 4)); // 30
        System.out.println("输入 4,输出: " + getNextSelectedValue(arr, 5)); // 30
            // 加密
            String md5Hash = md5Chinese(chineseText);
            System.out.println("原文: " + chineseText);
            System.out.println("MD5哈希: " + md5Hash);
            // 验证
            boolean isValid = verifyChinese(chineseText, md5Hash);
            System.out.println("验证结果: " + (isValid ? "匹配" : "不匹配"));
            // 错误验证示例
            boolean isWrongValid = verifyChinese("你好,世界!", md5Hash);
            System.out.println("错误文本验证: " + (isWrongValid ? "匹配" : "不匹配"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}