如果知道曲线名称&amp ;,该如何构造PrivateKey?原始私钥/点? [英] How to construct PrivateKey if you know the curve name & raw private key/point?

查看:70
本文介绍了如果知道曲线名称&amp ;,该如何构造PrivateKey?原始私钥/点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求:对于给定的命名曲线,请发送尽可能少的数据,以便接收方可以构造EC私钥.

Requirement: For a given named-curve, send as little data as you can, so that receiver can construct EC PrivateKey.

我目前在Android上使用BouncyCastle/SpongyCastle.这是我到目前为止所了解的.

I am currently using BouncyCastle/SpongyCastle on Android. This is what I have understood till now.

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp112r2");
keyGen.initialize(ecGenParameterSpec, new SecureRandom());
KeyPair keyPair = keyGen.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();

// privateKey.getEncoded() -> gives DER encoded private key. I can construct PrivateKey from that easily
byte[] derEncodedPrivateKeyBytes = privateKey.getEncoded(); // byteArray length-> 80
KeyFactory kf = KeyFactory.getInstance("EC");
PrivateKey key = kf.generatePrivate(new PKCS8EncodedKeySpec(derEncodedPrivateKeyBytes));

现在,使用Bouncy/Spongy城堡,我可以获得专用密钥的实际点,而derEncoding中没有任何其他信息.

Now, using Bouncy/Spongy castle, I get the actual point for the Private Key, without any other information which is present in derEncoding.

ECPrivateKeyParameters param = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(privateKey);
int lenghtOfKey = param.getD().toByteArray().length; // length - 14

问题:如何仅通过使用点 D ( privateKeyParam.getD())和曲线名称来重建PrivateKey对象?使用曲线名称,我可以获得ECCurveParameters.

Question: How can I reconstruct PrivateKey object, just by using the point D (privateKeyParam.getD()) and curve name? Using curve name, I can get ECCurveParameters.

我可以使用私钥点( praram.getD())构造 ECPrivateKeyParameters ,但仍然不知道如何从中生成PrivateKey ECPrivateKeyParameters .

I am able to construct ECPrivateKeyParameters using private key point (praram.getD()) but still couldn't figure out how to can I generate PrivateKey from ECPrivateKeyParameters.

X9ECParameters ecCurve = ECNamedCurveTable.getByName(curveName);
ECDomainParameters ecDomainParam = new ECDomainParameters(ecCurve.getCurve(),
        ecCurve.getG(), ecCurve.getN(), ecCurve.getH(), ecCurve.getSeed());
ECPrivateKeyParameters generatedECPrivateKeyParams = new ECPrivateKeyParameters(param.getD(), ecDomainParam);

推荐答案

我能够弄清楚如何从BigInteger D&曲线名称.

I was able to figure out how to re-construct EC PrivateKey from BigInteger D & curve name.

 public static PrivateKey getPrivateKeyFromECBigIntAndCurve(BigInteger s, String curveName) {
    X9ECParameters ecCurve = ECNamedCurveTable.getByName(curveName);
    ECParameterSpec ecParameterSpec = new ECNamedCurveSpec(curveName, ecCurve.getCurve(), ecCurve.getG(), ecCurve.getN(), ecCurve.getH(), ecCurve.getSeed());
    ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, ecParameterSpec);
    try {
        KeyFactory keyFactory = KeyFactory.getInstance("EC");
        return keyFactory.generatePrivate(privateKeySpec);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        e.printStackTrace();
        return null;
    }
}

这篇关于如果知道曲线名称&amp ;,该如何构造PrivateKey?原始私钥/点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆