private void initFromKey(Key key) throws InvalidKeyException { // BEGIN android-removed // if (agreement instanceof ECMQVBasicAgreement) // { // if (!(key instanceof MQVPrivateKey)) // { // throw new InvalidKeyException(kaAlgorithm + " key agreement requires " // + getSimpleName(MQVPrivateKey.class) + " for initialisation"); // } // // MQVPrivateKey mqvPrivKey = (MQVPrivateKey)key; // ECPrivateKeyParameters staticPrivKey = (ECPrivateKeyParameters) // ECUtil.generatePrivateKeyParameter(mqvPrivKey.getStaticPrivateKey()); // ECPrivateKeyParameters ephemPrivKey = (ECPrivateKeyParameters) // ECUtil.generatePrivateKeyParameter(mqvPrivKey.getEphemeralPrivateKey()); // // ECPublicKeyParameters ephemPubKey = null; // if (mqvPrivKey.getEphemeralPublicKey() != null) // { // ephemPubKey = (ECPublicKeyParameters) // ECUtil.generatePublicKeyParameter(mqvPrivKey.getEphemeralPublicKey()); // } // // MQVPrivateParameters localParams = new MQVPrivateParameters(staticPrivKey, ephemPrivKey, // ephemPubKey); // this.parameters = staticPrivKey.getParameters(); // // // TODO Validate that all the keys are using the same parameters? // // agreement.init(localParams); // } // else // END android-removed { if (!(key instanceof PrivateKey)) { throw new InvalidKeyException( kaAlgorithm + " key agreement requires " + getSimpleName(ECPrivateKey.class) + " for initialisation"); } ECPrivateKeyParameters privKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key); this.parameters = privKey.getParameters(); agreement.init(privKey); } }
public static byte[] privateKeyToPublicKey(String private_key, boolean compressed) { byte[] privateKeyBytes = null; try { privateKeyBytes = org.apache.commons.codec.binary.Hex.decodeHex(private_key.toCharArray()); } catch (DecoderException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } X9ECParameters ecp = SECNamedCurves.getByName("secp256k1"); ECDomainParameters domainParams = new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); ECPrivateKeyParameters privateKey = new ECPrivateKeyParameters(new BigInteger(1, privateKeyBytes), domainParams); System.out.println(privateKey.getD()); byte[] publicKeyBIBytes = privateKey.getD().toByteArray(); ECPoint Q = domainParams.getG().multiply(new BigInteger(publicKeyBIBytes)); byte[] publicKeyBytes = Q.getEncoded(compressed); return publicKeyBytes; }