This commit is contained in:
Looly 2019-10-09 11:34:09 +08:00
parent 8e5bbdf170
commit 417702f9fc
2 changed files with 83 additions and 20 deletions

View File

@ -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;
}

View File

@ -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);
}
}