Looly 6 years ago
parent
commit
417702f9fc

+ 37 - 7
hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java

@@ -595,20 +595,50 @@ public class ArrayUtil {
 
 		int length = 0;
 		for (T[] array : arrays) {
-			if (array == null) {
-				continue;
+			if (null != array) {
+				length += array.length;
 			}
-			length += array.length;
 		}
 		T[] result = newArray(arrays.getClass().getComponentType().getComponentType(), length);
 
 		length = 0;
 		for (T[] array : arrays) {
-			if (array == null) {
-				continue;
+			if (null != array) {
+				System.arraycopy(array, 0, result, length, array.length);
+				length += array.length;
+			}
+		}
+		return result;
+	}
+
+	/**
+	 * 将多个数组合并在一起<br>
+	 * 忽略null的数组
+	 *
+	 * @param arrays 数组集合
+	 * @return 合并后的数组
+	 * @since 4.6.9
+	 */
+	public static byte[] addAll(byte[]... arrays) {
+		if (arrays.length == 1) {
+			return arrays[0];
+		}
+
+		// 计算总长度
+		int length = 0;
+		for (byte[] array : arrays) {
+			if (null != array) {
+				length += array.length;
+			}
+		}
+
+		final byte[] result = new byte[length];
+		length = 0;
+		for (byte[] array : arrays) {
+			if (null != array) {
+				System.arraycopy(array, 0, result, length, array.length);
+				length += array.length;
 			}
-			System.arraycopy(array, 0, result, length, array.length);
-			length += array.length;
 		}
 		return result;
 	}

+ 47 - 14
hutool-crypto/src/test/java/cn/hutool/crypto/test/RSATest.java

@@ -1,23 +1,30 @@
 package cn.hutool.crypto.test;
 
-import java.security.KeyPair;
-
+import java.nio.charset.StandardCharsets;
+import java.security.*;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.X509EncodedKeySpec;
+
+import cn.hutool.core.codec.Base64;
+import cn.hutool.core.lang.Console;
+import cn.hutool.core.util.*;
 import org.junit.Assert;
 import org.junit.Test;
 
-import cn.hutool.core.util.CharsetUtil;
-import cn.hutool.core.util.HexUtil;
-import cn.hutool.core.util.StrUtil;
 import cn.hutool.crypto.KeyUtil;
 import cn.hutool.crypto.SecureUtil;
 import cn.hutool.crypto.asymmetric.KeyType;
 import cn.hutool.crypto.asymmetric.RSA;
 
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+
 /**
  * RSA算法单元测试
- * 
- * @author Looly
  *
+ * @author Looly
  */
 public class RSATest {
 
@@ -59,7 +66,7 @@ public class RSATest {
 
 		// 公钥加密,私钥解密
 		byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
-		
+
 		byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
 		Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
 
@@ -111,22 +118,22 @@ public class RSATest {
 	@Test
 	public void rsaBase64Test() {
 		String textBase = "我是一段特别长的测试";
-		String text = "";
+		StringBuilder text = new StringBuilder();
 		for (int i = 0; i < 10; i++) {
-			text += textBase;
+			text.append(textBase);
 		}
 
 		final RSA rsa = new RSA();
 
 		// 公钥加密,私钥解密
-		String encryptStr = rsa.encryptBase64(text, KeyType.PublicKey);
+		String encryptStr = rsa.encryptBase64(text.toString(), KeyType.PublicKey);
 		String decryptStr = StrUtil.utf8Str(rsa.decrypt(encryptStr, KeyType.PrivateKey));
-		Assert.assertEquals(text, decryptStr);
+		Assert.assertEquals(text.toString(), decryptStr);
 
 		// 私钥加密,公钥解密
-		String encrypt2 = rsa.encryptBase64(text, KeyType.PrivateKey);
+		String encrypt2 = rsa.encryptBase64(text.toString(), KeyType.PrivateKey);
 		String decrypt2 = StrUtil.utf8Str(rsa.decrypt(encrypt2, KeyType.PublicKey));
-		Assert.assertEquals(text, decrypt2);
+		Assert.assertEquals(text.toString(), decrypt2);
 	}
 
 	@Test
@@ -153,4 +160,30 @@ public class RSATest {
 
 		Assert.assertEquals("虎头闯杭州,多抬头看天,切勿只管种地", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
 	}
+
+	@Test
+	public void rsaTest2() throws Exception {
+		String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6s" +
+				"XqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9S" +
+				"dB1Ua44oncaTWz7OBGLbCiK45wIDAQAB";
+
+		byte[] keyBytes = Base64.decode(publicKeyStr);
+		PublicKey publicKey = KeyUtil.generateRSAPublicKey(keyBytes);
+
+		byte[] data = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 16).getBytes();
+		//长度不满足128补0
+		byte[] finalData = ArrayUtil.resize(data, 128);
+
+		//jdk原生加密
+		Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
+		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
+		String result1 = HexUtil.encodeHexStr(cipher.doFinal(finalData));
+
+		//hutool加密
+		RSA rsa = new RSA("RSA/ECB/NoPadding", null, publicKeyStr);
+		rsa.setEncryptBlockSize(128);
+		String result2 = rsa.encryptHex(finalData, KeyType.PublicKey);
+
+		Assert.assertEquals(result1, result2);
+	}
 }