|  |  | 
 |  |  | import java.nio.charset.StandardCharsets; | 
 |  |  | import java.security.MessageDigest; | 
 |  |  | import java.security.NoSuchAlgorithmException; | 
 |  |  | import java.time.LocalDate; | 
 |  |  | import java.time.YearMonth; | 
 |  |  | import java.time.format.DateTimeFormatter; | 
 |  |  | import java.time.temporal.ChronoUnit; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  | import java.util.stream.IntStream; | 
 |  |  |  | 
 |  |  | 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; | 
 |  |  | //        } | 
 |  |  | //        return indices; | 
 |  |  | //    } | 
 |  |  | // | 
 |  |  | //    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) { | 
 |  |  | //        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 | 
 |  |  | // | 
 |  |  | //    } | 
 |  |  |  | 
 |  |  |         // 统一使用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(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 验证汉字字符串与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 List<String> getFormattedDatesOfPreviousMonth(String pattern) { | 
 |  |  | //        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); | 
 |  |  | //        YearMonth previousMonth = YearMonth.now().minusMonths(1); | 
 |  |  | // | 
 |  |  | //        return IntStream.rangeClosed(1, previousMonth.lengthOfMonth()) | 
 |  |  | //                .mapToObj(day -> previousMonth.atDay(day).format(formatter)) | 
 |  |  | //                .collect(Collectors.toList()); | 
 |  |  |         LocalDate today = LocalDate.now().minusDays(1); | 
 |  |  |         LocalDate oneMonthAgo = today.minusMonths(1); | 
 |  |  |         DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); | 
 |  |  |         return IntStream.iterate(0, i -> i + 1) | 
 |  |  |                 .limit(ChronoUnit.DAYS.between(oneMonthAgo, today) + 1) | 
 |  |  |                 .mapToObj(oneMonthAgo::plusDays) | 
 |  |  |                 .map(date -> date.format(formatter)) | 
 |  |  |                 .collect(Collectors.toList()); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static void main(String[] args) { | 
 |  |  |         try { | 
 |  |  |             String chineseText = "你好,世界!"; | 
 |  |  |  | 
 |  |  |             // 加密 | 
 |  |  |             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(); | 
 |  |  |         } | 
 |  |  |         List<String> formattedDates = getFormattedDatesOfPreviousMonth("yyyy-MM-dd"); | 
 |  |  |         formattedDates.forEach(System.out::println); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |