fix bug and add crypto

This commit is contained in:
Looly 2020-07-23 12:10:58 +08:00
parent 1e22cce3a3
commit b47125c9f1
7 changed files with 140 additions and 5 deletions

View File

@ -12,10 +12,12 @@
* 【core 】 增加CRC16算法若干pr#963@Github
* 【core 】 LocalDateTimeUtil增加format等方法pr#140@Gitee
* 【http 】 UserAgentUtil增加Android原生浏览器识别pr#975@Github
* 【crypto 】 增加ECIES算法类issue#979@Github
### Bug修复
* 【core 】 修复ZipUtil中finish位于循环内的问题issue#961@Github
* 【core 】 修复CollUtil.page未越界检查的问题issue#I1O2LR@Gitee
* 【core 】 修复StrUtil.removeAny的bugissue#977@Github
-------------------------------------------------------------------------------------------------------------
## 5.3.9 (2020-07-12)

View File

@ -978,7 +978,7 @@ public class StrUtil {
String result = str(str);
if (isNotEmpty(str)) {
for (CharSequence strToRemove : strsToRemove) {
result = removeAll(str, strToRemove);
result = removeAll(result, strToRemove);
}
}
return result;

View File

@ -342,7 +342,12 @@ public class KeyUtil {
* @return {@link KeyPair}
*/
public static KeyPair generateKeyPair(String algorithm) {
return generateKeyPair(algorithm, DEFAULT_KEY_SIZE);
int keySize = DEFAULT_KEY_SIZE;
if("ECIES".equalsIgnoreCase(algorithm)){
// ECIES算法对KEY的长度有要求此处默认256
keySize = 256;
}
return generateKeyPair(algorithm, keySize);
}
/**

View File

@ -3,7 +3,7 @@ package cn.hutool.crypto.asymmetric;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.CryptoException;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.KeyUtil;
import java.security.Key;
import java.security.KeyPair;
@ -89,7 +89,7 @@ public class BaseAsymmetric<T extends BaseAsymmetric<T>> {
*/
@SuppressWarnings("unchecked")
public T initKeys() {
KeyPair keyPair = SecureUtil.generateKeyPair(this.algorithm);
KeyPair keyPair = KeyUtil.generateKeyPair(this.algorithm);
this.publicKey = keyPair.getPublic();
this.privateKey = keyPair.getPrivate();
return (T) this;

View File

@ -0,0 +1,103 @@
package cn.hutool.crypto.asymmetric;
import java.security.PrivateKey;
import java.security.PublicKey;
/**
* ECIES集成加密方案elliptic curve integrate encrypt scheme
*
* <p>
* 详细介绍见https://blog.csdn.net/baidu_26954729/article/details/90437344
* 此算法必须引入Bouncy Castle库
*
* @author loolly
* @since 5.3.10
*/
public class ECIES extends AsymmetricCrypto{
/** 默认的ECIES算法 */
private static final String ALGORITHM_ECIES = "ECIES";
// ------------------------------------------------------------------ Constructor start
/**
* 构造生成新的私钥公钥对
*/
public ECIES() {
super(ALGORITHM_ECIES);
}
/**
* 构造生成新的私钥公钥对
*
* @param eciesAlgorithm 自定义ECIES算法例如ECIESwithDESede/NONE/PKCS7Padding
*/
public ECIES(String eciesAlgorithm) {
super(eciesAlgorithm);
}
/**
* 构造<br>
* 私钥和公钥同时为空时生成一对新的私钥和公钥<br>
* 私钥和公钥可以单独传入一个如此则只能使用此钥匙来做加密或者解密
*
* @param privateKeyStr 私钥Hex或Base64表示
* @param publicKeyStr 公钥Hex或Base64表示
*/
public ECIES(String privateKeyStr, String publicKeyStr) {
super(ALGORITHM_ECIES, privateKeyStr, publicKeyStr);
}
/**
* 构造<br>
* 私钥和公钥同时为空时生成一对新的私钥和公钥<br>
* 私钥和公钥可以单独传入一个如此则只能使用此钥匙来做加密或者解密
*
* @param eciesAlgorithm 自定义ECIES算法例如ECIESwithDESede/NONE/PKCS7Padding
* @param privateKeyStr 私钥Hex或Base64表示
* @param publicKeyStr 公钥Hex或Base64表示
* @since 4.5.8
*/
public ECIES(String eciesAlgorithm, String privateKeyStr, String publicKeyStr) {
super(eciesAlgorithm, privateKeyStr, publicKeyStr);
}
/**
* 构造 <br>
* 私钥和公钥同时为空时生成一对新的私钥和公钥<br>
* 私钥和公钥可以单独传入一个如此则只能使用此钥匙来做加密或者解密
*
* @param privateKey 私钥
* @param publicKey 公钥
*/
public ECIES(byte[] privateKey, byte[] publicKey) {
super(ALGORITHM_ECIES, privateKey, publicKey);
}
/**
* 构造 <br>
* 私钥和公钥同时为空时生成一对新的私钥和公钥<br>
* 私钥和公钥可以单独传入一个如此则只能使用此钥匙来做加密或者解密
*
* @param privateKey 私钥
* @param publicKey 公钥
* @since 3.1.1
*/
public ECIES(PrivateKey privateKey, PublicKey publicKey) {
super(ALGORITHM_ECIES, privateKey, publicKey);
}
/**
* 构造 <br>
* 私钥和公钥同时为空时生成一对新的私钥和公钥<br>
* 私钥和公钥可以单独传入一个如此则只能使用此钥匙来做加密或者解密
*
* @param eciesAlgorithm 自定义ECIES算法例如ECIESwithDESede/NONE/PKCS7Padding
* @param privateKey 私钥
* @param publicKey 公钥
* @since 4.5.8
*/
public ECIES(String eciesAlgorithm, PrivateKey privateKey, PublicKey publicKey) {
super(eciesAlgorithm, privateKey, publicKey);
}
// ------------------------------------------------------------------ Constructor end
}

View File

@ -0,0 +1,26 @@
package cn.hutool.crypto.test;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.ECIES;
import cn.hutool.crypto.asymmetric.KeyType;
import org.junit.Assert;
import org.junit.Test;
public class ECIESTest {
@Test
public void eciesTest(){
final ECIES ecies = new ECIES();
String textBase = "我是一段特别长的测试";
StringBuilder text = new StringBuilder();
for (int i = 0; i < 10; i++) {
text.append(textBase);
}
// 公钥加密私钥解密
String encryptStr = ecies.encryptBase64(text.toString(), KeyType.PublicKey);
String decryptStr = StrUtil.utf8Str(ecies.decrypt(encryptStr, KeyType.PrivateKey));
Assert.assertEquals(text.toString(), decryptStr);
}
}

View File

@ -196,5 +196,4 @@ public class RSATest {
final String encryptBase64 = rsa.encryptBase64("测试内容", KeyType.PublicKey);
Assert.assertNotNull(encryptBase64);
}
}