From 94774d917f3442635118f52282e57b189a7e940b Mon Sep 17 00:00:00 2001 From: redkale Date: Fri, 23 Jun 2023 20:22:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Utility.aes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/redkale/util/Utility.java | 46 +++++++++++++++++++ src/main/java/org/redkale/util/XmlReader.java | 6 +-- .../org/redkale/test/util/UntilTestMain.java | 34 ++++++++++++-- 3 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/redkale/util/Utility.java b/src/main/java/org/redkale/util/Utility.java index 60c8502a8..c8a8a964c 100644 --- a/src/main/java/org/redkale/util/Utility.java +++ b/src/main/java/org/redkale/util/Utility.java @@ -506,6 +506,29 @@ public final class Utility { return str == null || str.isEmpty() || str.isBlank(); } + /** + * 是否为空白 + * + * @param str 字符串 + * @param fromIndex 起始位置 + * @param toIndex 结束位置 + * + * @return 是否为空白 + * + */ + public static boolean isBlank(String str, int fromIndex, int toIndex) { + if (str == null || str.isEmpty()) { + return true; + } + for (int i = fromIndex; i < toIndex; i++) { + char ch = str.charAt(i); + if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) { + return false; + } + } + return true; + } + /** * 是否不为空白 * @@ -518,6 +541,29 @@ public final class Utility { return str != null && !str.isEmpty() && !str.isBlank(); } + /** + * 是否不为空白 + * + * @param str 字符串 + * @param fromIndex 起始位置 + * @param toIndex 结束位置 + * + * @return 是否为空白 + * + */ + public static boolean isNotBlank(String str, int fromIndex, int toIndex) { + if (str == null || str.isEmpty()) { + return false; + } + for (int i = fromIndex; i < toIndex; i++) { + char ch = str.charAt(i); + if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) { + return true; + } + } + return false; + } + /** * 是否为空 * diff --git a/src/main/java/org/redkale/util/XmlReader.java b/src/main/java/org/redkale/util/XmlReader.java index b423536ee..1439d1e82 100644 --- a/src/main/java/org/redkale/util/XmlReader.java +++ b/src/main/java/org/redkale/util/XmlReader.java @@ -413,12 +413,12 @@ public class XmlReader { .replace(">", ">").replace("&", "&"); } - protected RuntimeException newException(String msg) { + protected RedkaleException newException(String msg) { return newException(msg, (Throwable) null); } - protected RuntimeException newException(String msg, Throwable chain) { - return new RuntimeException((msg == null ? "" : (msg + " ")) + protected RedkaleException newException(String msg, Throwable chain) { + return new RedkaleException((msg == null ? "" : (msg + " ")) + "(line: " + this.lineNumber + ", column: " + this.columnNumber + ", position:" + this.position + ") " + (chain == null ? "" : ("caused by: " + chain))); } diff --git a/src/test/java/org/redkale/test/util/UntilTestMain.java b/src/test/java/org/redkale/test/util/UntilTestMain.java index f5faf6fe1..0078dd3a6 100644 --- a/src/test/java/org/redkale/test/util/UntilTestMain.java +++ b/src/test/java/org/redkale/test/util/UntilTestMain.java @@ -5,8 +5,11 @@ */ package org.redkale.test.util; -import org.redkale.util.Reproduce; -import org.redkale.util.Attribute; +import java.nio.charset.StandardCharsets; +import java.security.Key; +import javax.crypto.Cipher; +import javax.crypto.spec.*; +import org.redkale.util.*; /** * @@ -17,6 +20,30 @@ public class UntilTestMain { public static void main(String[] args) throws Throwable { reproduce(args); attribute(args); + aes(args); + } + + public static void aes(String[] args) throws Throwable { + String mingwen = "我是z前面的"; + String secret = "after z content."; + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + Key secureKey = new SecretKeySpec(secret.getBytes(), "AES"); + IvParameterSpec iv = new IvParameterSpec(secret.getBytes(StandardCharsets.UTF_8)); + cipher.init(Cipher.ENCRYPT_MODE, secureKey, iv); + byte[] bs = cipher.doFinal(mingwen.getBytes()); + System.out.println("加密后: " + Utility.binToHexString(bs)); + + System.out.println(new String(Utility.hexToBin("6166746572207a20636f6e74656e742e"))); + String miwen = "2a2c3ff3b51205eb80a02e43e0eb0751"; + secret = "after z content."; + byte[] ms = Utility.hexToBin(miwen); + cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + secureKey = new SecretKeySpec(secret.substring(0, 16).getBytes(), "AES"); + iv = new IvParameterSpec(secret.substring(0, 16).getBytes(StandardCharsets.UTF_8)); + cipher.init(Cipher.DECRYPT_MODE, secureKey, iv); + bs = cipher.doFinal(ms); + System.out.println("解密后: " + new String(bs)); + System.out.println(Utility.binToHexString(secret.getBytes())); } public static void reproduce(String[] args) throws Throwable { @@ -98,7 +125,8 @@ public class UntilTestMain { } e = System.nanoTime() - s; System.out.println("动态Attribute耗时: " + e); - System.out.println(); System.out.println("TestBean.map: " + Attribute.create(TestBean.class.getDeclaredField("map")).genericType()); + System.out.println(); + System.out.println(); } }