Example #1
0
  /**
   * Return a PKCS8 representation of the key. The sequence returned represents a full
   * PrivateKeyInfo object.
   *
   * @return a PKCS8 representation of the key.
   */
  public byte[] getEncoded() {
    X962Parameters params;

    if (ecSpec instanceof ECNamedCurveSpec) {
      DERObjectIdentifier curveOid = ECUtil.getNamedCurveOid(((ECNamedCurveSpec) ecSpec).getName());
      if (curveOid == null) // guess it's the OID
      {
        curveOid = new DERObjectIdentifier(((ECNamedCurveSpec) ecSpec).getName());
      }
      params = new X962Parameters(curveOid);
    } else if (ecSpec == null) {
      params = new X962Parameters(DERNull.INSTANCE);
    } else {
      ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());

      X9ECParameters ecP =
          new X9ECParameters(
              curve,
              EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression),
              ecSpec.getOrder(),
              BigInteger.valueOf(ecSpec.getCofactor()),
              ecSpec.getCurve().getSeed());

      params = new X962Parameters(ecP);
    }

    PrivateKeyInfo info;
    ECPrivateKeyStructure keyStructure;

    if (publicKey != null) {
      keyStructure = new ECPrivateKeyStructure(this.getS(), publicKey, params);
    } else {
      keyStructure = new ECPrivateKeyStructure(this.getS(), params);
    }

    if (algorithm.equals("ECGOST3410")) {
      info =
          new PrivateKeyInfo(
              new AlgorithmIdentifier(
                  CryptoProObjectIdentifiers.gostR3410_2001, params.getDERObject()),
              keyStructure.getDERObject());
    } else {

      info =
          new PrivateKeyInfo(
              new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params.getDERObject()),
              keyStructure.getDERObject());
    }

    return info.getDEREncoded();
  }