public static ECPublicKey PrivateKeytoECPublicKey(byte[] privateKey) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException { KeyPairGenerator kpg = null; kpg = KeyPairGenerator.getInstance("EC"); ECGenParameterSpec gps = new ECGenParameterSpec("secp256k1"); // NIST P-256 kpg.initialize(gps); KeyPair apair = kpg.generateKeyPair(); ECPublicKey apub = (ECPublicKey) apair.getPublic(); ECParameterSpec aspec = apub.getParams(); byte[] publicKeyb = Address.privateKeyToPublicKey(Utils.toHex(privateKey), false); byte[] publicKeybx = new byte[32]; byte[] publicKeyby = new byte[32]; System.arraycopy(publicKeyb, 1, publicKeybx, 0, 32); System.arraycopy(publicKeyb, 33, publicKeyby, 0, 32); BigInteger x = new BigInteger(1, publicKeybx); BigInteger y = new BigInteger(1, publicKeyby); java.security.spec.ECPoint cpoint = new java.security.spec.ECPoint(x, y); ECPublicKeySpec cpubs = new ECPublicKeySpec(cpoint, aspec); ECPublicKey cpub = null; KeyFactory kfa = null; kfa = KeyFactory.getInstance("EC"); return cpub = (ECPublicKey) kfa.generatePublic(cpubs); }
public static ECPrivateKey WIFtoECPrivateKey(String wif) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException { // byte [] privateKey = // Address.wifToPrivateKey("5Kb6aGpijtrb8X28GzmWtbcGZCG8jHQWFJcWugqo3MwKRvC8zyu"); byte[] privateKey = Address.wifToPrivateKey(wif); return PrivateKeytoECPrivateKey(privateKey); }
public static ECPublicKey WIFtoECPublicKey(String wif) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException { byte[] privateKey = Address.wifToPrivateKey(wif); return PrivateKeytoECPublicKey(privateKey); }