Java bouncycastle 包实现椭圆曲线计算

Java bouncycastle 包实现椭圆曲线计算

  • 说明
  • 示例代码

说明

最近项目需要使用椭圆曲线实现ECDHE秘钥协商算法,然后遇到了一些坑,在这里做个记录

示例代码

引入依赖

<dependency>
    <groupId>org.bouncycastlegroupId>
    <artifactId>bcpkix-jdk15onartifactId>
    <version>1.62version>
    <scope>providedscope>
dependency>

其实计算的过程就是参考公司钥对生成的过程。
公私钥对的生成代码如下:

@Test
public void case6() throws Exception{

    // 曲线参数
    X9ECParameters ecp = SECNamedCurves.getByName("secp256r1");
    ECDomainParameters domainParameters = new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed());

    // 生成公私钥对
    AsymmetricCipherKeyPair keyPair;
    ECKeyGenerationParameters keyGenerationParameters = new ECKeyGenerationParameters(domainParameters, new SecureRandom());
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keyGenerationParameters);
    keyPair = generator.generateKeyPair();

    ECPublicKeyParameters publicKeyParameters = (ECPublicKeyParameters) keyPair.getPublic();
    ECPrivateKeyParameters privateKeyParameters = (ECPrivateKeyParameters) keyPair.getPrivate();

}

然后,就是去源码中看,随机生成私钥,然后 私钥 与 基点相乘

@Test
public void case5() throws Exception{

    // 曲线参数
    X9ECParameters ecp = SECNamedCurves.getByName("secp256r1");
    ECDomainParameters domainParameters = new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed());

    // 生成公私钥对
    AsymmetricCipherKeyPair keyPair;
    ECKeyGenerationParameters keyGenerationParameters = new ECKeyGenerationParameters(domainParameters, new SecureRandom());
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keyGenerationParameters);
    keyPair = generator.generateKeyPair();

    ECPublicKeyParameters publicKeyParameters = (ECPublicKeyParameters) keyPair.getPublic();
    ECPrivateKeyParameters privateKeyParameters = (ECPrivateKeyParameters) keyPair.getPrivate();

    BigInteger privateKey = privateKeyParameters.getD();

    System.out.println("privateKey: "+privateKey.toString(16));
    System.out.println("publicKey x: "+publicKeyParameters.getQ().getXCoord().toBigInteger().toString(16));
    System.out.println("publicKey y: "+publicKeyParameters.getQ().getYCoord().toBigInteger().toString(16));

    ECPoint Q = keyGenerationParameters.getDomainParameters().getG().multiply(privateKey);
    // 好像在 1.60 版本之前不需要这一步, 这个搞了我好久
    Q = ECAlgorithms.importPoint(Q.getCurve(), Q).normalize();

    System.out.println(" multiply x: " + Q.getXCoord().toBigInteger().toString(16));
    System.out.println(" multiply y: " + Q.getYCoord().toBigInteger().toString(16));

}

输出结果

privateKey: 8e9c71edc91829c302b55e16ca39d63f0c76b946908e4a8dd49b1d17d30d8fcc
publicKey x: a731d1b4ba06232941f35f2c5ebb3c4c4343b94cff3433f5efd0eba0541ff666
publicKey y: 9f60476948ce2718a3a92ad32a5185cdf43f9903ce028e37b95142cc4c53def0
 multiply Q: a731d1b4ba06232941f35f2c5ebb3c4c4343b94cff3433f5efd0eba0541ff666
 multiply Q: 9f60476948ce2718a3a92ad32a5185cdf43f9903ce028e37b95142cc4c53def0

你可能感兴趣的:(Java bouncycastle 包实现椭圆曲线计算)