mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 08:37:26 +08:00
增加BCUtil.decodeECPrivateKey方法(issue#3829@Github)
This commit is contained in:
parent
46fa4a1295
commit
12b56a3841
@ -2,9 +2,10 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.36(2024-12-25)
|
||||
# 5.8.36(2025-01-01)
|
||||
|
||||
### 🐣新特性
|
||||
* 【crypto 】 增加BCUtil.decodeECPrivateKey方法(issue#3829@Github)
|
||||
### 🐞Bug修复
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
@ -14,7 +14,9 @@ import org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util;
|
||||
import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
|
||||
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
|
||||
import org.bouncycastle.jce.spec.ECParameterSpec;
|
||||
import org.bouncycastle.jce.spec.ECPrivateKeySpec;
|
||||
import org.bouncycastle.math.ec.ECCurve;
|
||||
import org.bouncycastle.util.BigIntegers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -44,6 +46,27 @@ public class BCUtil {
|
||||
return ((BCECPrivateKey) privateKey).getD().toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码恢复EC私钥,支持Base64和Hex编码,(基于BouncyCastle)
|
||||
*
|
||||
* @param d 私钥d值
|
||||
* @param curveName EC曲线名
|
||||
* @return 私钥
|
||||
* @since 5.8.36
|
||||
*/
|
||||
public static PrivateKey decodeECPrivateKey(final byte[] d, final String curveName) {
|
||||
final X9ECParameters x9ECParameters = ECUtil.getNamedCurveByName(curveName);
|
||||
final ECParameterSpec ecSpec = new ECParameterSpec(
|
||||
x9ECParameters.getCurve(),
|
||||
x9ECParameters.getG(),
|
||||
x9ECParameters.getN(),
|
||||
x9ECParameters.getH()
|
||||
);
|
||||
|
||||
final ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(BigIntegers.fromUnsignedByteArray(d), ecSpec);
|
||||
return KeyUtil.generatePrivateKey("EC", privateKeySpec);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码压缩EC公钥(基于BouncyCastle),即Q值<br>
|
||||
* 见:https://www.cnblogs.com/xinzhao/p/8963724.html
|
||||
|
Loading…
Reference in New Issue
Block a user