mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
remove EC
This commit is contained in:
parent
7c4c6c7af1
commit
44fe8904a2
@ -21,6 +21,7 @@
|
|||||||
* 【script 】 ScriptUtil增加evalInvocable和invoke方法(issue#I1HHCP@Gitee)
|
* 【script 】 ScriptUtil增加evalInvocable和invoke方法(issue#I1HHCP@Gitee)
|
||||||
* 【core 】 ImgUtil增加去除背景色的方法(pr#124@Gitee)
|
* 【core 】 ImgUtil增加去除背景色的方法(pr#124@Gitee)
|
||||||
* 【system 】 OshiUtil增加获取CPU使用率的方法(pr#124@Gitee)
|
* 【system 】 OshiUtil增加获取CPU使用率的方法(pr#124@Gitee)
|
||||||
|
* 【crypto 】 AsymmetricAlgorithm去除EC(issue#887@Github)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【core 】 修复SimpleCache死锁问题(issue#I1HOKB@Gitee)
|
* 【core 】 修复SimpleCache死锁问题(issue#I1HOKB@Gitee)
|
||||||
|
@ -1,7 +1,26 @@
|
|||||||
package cn.hutool.crypto;
|
package cn.hutool.crypto;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.io.IoUtil;
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
import cn.hutool.core.util.CharUtil;
|
||||||
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
||||||
|
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
|
||||||
|
|
||||||
|
import javax.crypto.KeyGenerator;
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import javax.crypto.SecretKeyFactory;
|
||||||
|
import javax.crypto.spec.DESKeySpec;
|
||||||
|
import javax.crypto.spec.DESedeKeySpec;
|
||||||
|
import javax.crypto.spec.PBEKeySpec;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.security.InvalidAlgorithmParameterException;
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.KeyFactory;
|
import java.security.KeyFactory;
|
||||||
@ -17,32 +36,15 @@ import java.security.SecureRandom;
|
|||||||
import java.security.cert.Certificate;
|
import java.security.cert.Certificate;
|
||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.security.cert.CertificateFactory;
|
import java.security.cert.CertificateFactory;
|
||||||
|
import java.security.interfaces.RSAPrivateCrtKey;
|
||||||
import java.security.spec.AlgorithmParameterSpec;
|
import java.security.spec.AlgorithmParameterSpec;
|
||||||
import java.security.spec.ECGenParameterSpec;
|
import java.security.spec.ECGenParameterSpec;
|
||||||
import java.security.spec.InvalidKeySpecException;
|
import java.security.spec.InvalidKeySpecException;
|
||||||
import java.security.spec.KeySpec;
|
import java.security.spec.KeySpec;
|
||||||
import java.security.spec.PKCS8EncodedKeySpec;
|
import java.security.spec.PKCS8EncodedKeySpec;
|
||||||
|
import java.security.spec.RSAPublicKeySpec;
|
||||||
import java.security.spec.X509EncodedKeySpec;
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
|
|
||||||
import javax.crypto.KeyGenerator;
|
|
||||||
import javax.crypto.SecretKey;
|
|
||||||
import javax.crypto.SecretKeyFactory;
|
|
||||||
import javax.crypto.spec.DESKeySpec;
|
|
||||||
import javax.crypto.spec.DESedeKeySpec;
|
|
||||||
import javax.crypto.spec.PBEKeySpec;
|
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
|
||||||
import cn.hutool.core.io.IoUtil;
|
|
||||||
import cn.hutool.core.lang.Assert;
|
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
|
||||||
import cn.hutool.core.util.CharUtil;
|
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
|
||||||
import cn.hutool.core.util.RandomUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
|
||||||
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 密钥工具类
|
* 密钥工具类
|
||||||
*
|
*
|
||||||
@ -906,4 +908,49 @@ public class KeyUtil {
|
|||||||
public static PublicKey decodeECPoint(byte[] encodeByte, String curveName) {
|
public static PublicKey decodeECPoint(byte[] encodeByte, String curveName) {
|
||||||
return BCUtil.decodeECPoint(encodeByte, curveName);
|
return BCUtil.decodeECPoint(encodeByte, curveName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过RSA私钥生成RSA公钥
|
||||||
|
*
|
||||||
|
* @param privateKey RSA私钥
|
||||||
|
* @return RSA公钥,null表示私钥不被支持
|
||||||
|
* @since 5.3.6
|
||||||
|
*/
|
||||||
|
public static PublicKey getRSAPublicKey(PrivateKey privateKey){
|
||||||
|
if(privateKey instanceof RSAPrivateCrtKey){
|
||||||
|
final RSAPrivateCrtKey privk = (RSAPrivateCrtKey)privateKey;
|
||||||
|
return getRSAPublicKey(privk.getModulus(), privk.getPublicExponent());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得RSA公钥对象
|
||||||
|
*
|
||||||
|
* @param modulus Modulus
|
||||||
|
* @param publicExponent Public Exponent
|
||||||
|
* @return 公钥
|
||||||
|
* @since 5.3.6
|
||||||
|
*/
|
||||||
|
public static PublicKey getRSAPublicKey(String modulus, String publicExponent){
|
||||||
|
return getRSAPublicKey(
|
||||||
|
new BigInteger(modulus, 16), new BigInteger(publicExponent, 16));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得RSA公钥对象
|
||||||
|
*
|
||||||
|
* @param modulus Modulus
|
||||||
|
* @param publicExponent Public Exponent
|
||||||
|
* @return 公钥
|
||||||
|
* @since 5.3.6
|
||||||
|
*/
|
||||||
|
public static PublicKey getRSAPublicKey(BigInteger modulus, BigInteger publicExponent){
|
||||||
|
final RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);
|
||||||
|
try {
|
||||||
|
return getKeyFactory("RSA").generatePublic(publicKeySpec);
|
||||||
|
} catch (InvalidKeySpecException e) {
|
||||||
|
throw new CryptoException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,7 @@ public enum AsymmetricAlgorithm {
|
|||||||
/** RSA算法,此算法用了默认补位方式为RSA/ECB/PKCS1Padding */
|
/** RSA算法,此算法用了默认补位方式为RSA/ECB/PKCS1Padding */
|
||||||
RSA_ECB_PKCS1("RSA/ECB/PKCS1Padding"),
|
RSA_ECB_PKCS1("RSA/ECB/PKCS1Padding"),
|
||||||
/** RSA算法,此算法用了RSA/None/NoPadding */
|
/** RSA算法,此算法用了RSA/None/NoPadding */
|
||||||
RSA_None("RSA/None/NoPadding"),
|
RSA_None("RSA/None/NoPadding");
|
||||||
/** EC(Elliptic Curve)算法 */
|
|
||||||
EC("EC");
|
|
||||||
|
|
||||||
private final String value;
|
private final String value;
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import cn.hutool.core.lang.Assert;
|
|||||||
import cn.hutool.core.util.HexUtil;
|
import cn.hutool.core.util.HexUtil;
|
||||||
import cn.hutool.crypto.BCUtil;
|
import cn.hutool.crypto.BCUtil;
|
||||||
import cn.hutool.crypto.CryptoException;
|
import cn.hutool.crypto.CryptoException;
|
||||||
|
import cn.hutool.crypto.KeyUtil;
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
import org.bouncycastle.crypto.CipherParameters;
|
import org.bouncycastle.crypto.CipherParameters;
|
||||||
import org.bouncycastle.crypto.Digest;
|
import org.bouncycastle.crypto.Digest;
|
||||||
@ -78,8 +79,8 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
|||||||
*/
|
*/
|
||||||
public SM2(byte[] privateKey, byte[] publicKey) {
|
public SM2(byte[] privateKey, byte[] publicKey) {
|
||||||
this(//
|
this(//
|
||||||
SecureUtil.generatePrivateKey(ALGORITHM_SM2, privateKey), //
|
KeyUtil.generatePrivateKey(ALGORITHM_SM2, privateKey), //
|
||||||
SecureUtil.generatePublicKey(ALGORITHM_SM2, publicKey)//
|
KeyUtil.generatePublicKey(ALGORITHM_SM2, publicKey)//
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
package cn.hutool.crypto.test;
|
package cn.hutool.crypto.test;
|
||||||
|
|
||||||
import java.security.KeyPair;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import cn.hutool.crypto.CryptoException;
|
import cn.hutool.crypto.CryptoException;
|
||||||
import cn.hutool.crypto.GlobalBouncyCastleProvider;
|
import cn.hutool.crypto.GlobalBouncyCastleProvider;
|
||||||
import cn.hutool.crypto.KeyUtil;
|
import cn.hutool.crypto.KeyUtil;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
|
||||||
public class KeyUtilTest {
|
public class KeyUtilTest {
|
||||||
|
|
||||||
@ -23,4 +24,11 @@ public class KeyUtilTest {
|
|||||||
Assert.assertNotNull(pair);
|
Assert.assertNotNull(pair);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getRSAPublicKeyTest(){
|
||||||
|
final KeyPair keyPair = KeyUtil.generateKeyPair("RSA");
|
||||||
|
final PrivateKey aPrivate = keyPair.getPrivate();
|
||||||
|
final PublicKey rsaPublicKey = KeyUtil.getRSAPublicKey(aPrivate);
|
||||||
|
Assert.assertEquals(rsaPublicKey, keyPair.getPublic());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user