增加BCUtil.decodeECPrivateKey方法(issue#3829@Github)

This commit is contained in:
Looly 2025-01-01 18:39:50 +08:00
parent 46fa4a1295
commit 12b56a3841
2 changed files with 25 additions and 1 deletions

View File

@ -2,9 +2,10 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.36(2024-12-25)
# 5.8.36(2025-01-01)
### 🐣新特性
* 【crypto 】 增加BCUtil.decodeECPrivateKey方法issue#3829@Github
### 🐞Bug修复
-------------------------------------------------------------------------------------------------------------

View File

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