Skip to content

Commit ee161b5

Browse files
Copilotbinarywang
andauthored
修复代码审查问题:hexToBytes 增加输入验证,改进测试代码质量
Agent-Logs-Url: https://github.com/binarywang/WxJava/sessions/f3aba758-8b4a-479f-96bd-88ce00a9c176 Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
1 parent 96eaeb2 commit ee161b5

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/crypt/WxMaCryptUtils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,24 @@ public static String encryptWithEncryptKey(String encryptKey, String hexIv, Stri
145145

146146
/**
147147
* 将 Hex 字符串转换为字节数组.
148+
*
149+
* @param hex Hex 字符串(长度必须为偶数,只包含 0-9 和 a-f/A-F 字符)
150+
* @return 字节数组
151+
* @throws IllegalArgumentException 如果输入不是合法的 Hex 字符串
148152
*/
149153
private static byte[] hexToBytes(String hex) {
154+
if (hex == null || hex.length() % 2 != 0) {
155+
throw new IllegalArgumentException("无效的十六进制字符串格式:长度必须为偶数");
156+
}
150157
int len = hex.length();
151158
byte[] data = new byte[len / 2];
152159
for (int i = 0; i < len; i += 2) {
153-
data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
154-
+ Character.digit(hex.charAt(i + 1), 16));
160+
int high = Character.digit(hex.charAt(i), 16);
161+
int low = Character.digit(hex.charAt(i + 1), 16);
162+
if (high == -1 || low == -1) {
163+
throw new IllegalArgumentException("无效的十六进制字符串格式:包含非法字符 '" + hex.charAt(high == -1 ? i : i + 1) + "'");
164+
}
165+
data[i / 2] = (byte) ((high << 4) + low);
155166
}
156167
return data;
157168
}

weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/util/crypt/WxMaCryptUtilsTest.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1515
*/
1616
public class WxMaCryptUtilsTest {
17+
// 模拟来自 getUserEncryptKey 接口返回的 encrypt_key(Base64)和 iv(Hex,32位即16字节)
18+
private static final String ENCRYPT_KEY = "VI6BpyrK9XH4i4AIGe86tg==";
19+
private static final String HEX_IV = "6003f73ec441c3866003f73ec441c386";
20+
1721
@Test
1822
public void testDecrypt() {
1923
String sessionKey = "7MG7jbTToVVRWRXVA885rg==";
@@ -39,30 +43,24 @@ public void testDecryptAnotherWay() {
3943
*/
4044
@Test
4145
public void testEncryptAndDecryptWithEncryptKey() {
42-
// 模拟来自 getUserEncryptKey 接口的 encrypt_key(Base64)和 iv(Hex)
43-
String encryptKey = "VI6BpyrK9XH4i4AIGe86tg==";
44-
String hexIv = "6003f73ec441c3866003f73ec441c386";
4546
String plainText = "{\"userId\":\"12345\",\"amount\":100}";
4647

47-
String encrypted = WxMaCryptUtils.encryptWithEncryptKey(encryptKey, hexIv, plainText);
48+
String encrypted = WxMaCryptUtils.encryptWithEncryptKey(ENCRYPT_KEY, HEX_IV, plainText);
4849
assertThat(encrypted).isNotNull().isNotEmpty();
4950

50-
String decrypted = WxMaCryptUtils.decryptWithEncryptKey(encryptKey, hexIv, encrypted);
51+
String decrypted = WxMaCryptUtils.decryptWithEncryptKey(ENCRYPT_KEY, HEX_IV, encrypted);
5152
assertThat(decrypted).isEqualTo(plainText);
5253
}
5354

5455
/**
55-
* 测试使用已知密文验证解密结果(加密网络通道).
56+
* 测试加密网络通道的加解密对称性(不同明文).
5657
*/
5758
@Test
58-
public void testDecryptWithEncryptKey() {
59-
String encryptKey = "VI6BpyrK9XH4i4AIGe86tg==";
60-
String hexIv = "6003f73ec441c3866003f73ec441c386";
59+
public void testEncryptDecryptSymmetryWithEncryptKey() {
6160
String plainText = "hello miniprogram";
6261

63-
// 先加密再解密,验证对称性
64-
String encrypted = WxMaCryptUtils.encryptWithEncryptKey(encryptKey, hexIv, plainText);
65-
String decrypted = WxMaCryptUtils.decryptWithEncryptKey(encryptKey, hexIv, encrypted);
62+
String encrypted = WxMaCryptUtils.encryptWithEncryptKey(ENCRYPT_KEY, HEX_IV, plainText);
63+
String decrypted = WxMaCryptUtils.decryptWithEncryptKey(ENCRYPT_KEY, HEX_IV, encrypted);
6664
assertThat(decrypted).isEqualTo(plainText);
6765
}
6866
}

0 commit comments

Comments
 (0)