@Override public BigInteger getPrivateKey() { return this.key.getPrivKey(); }
/** * Returns a 32 byte array containing the private key. * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key bytes are missing/encrypted. */ public byte[] getPrivKeyBytes() { return Utils.bigIntegerToBytes(getPrivKey(), 32); }
/** * Returns a 32 byte array containing the private key. * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key bytes are missing/encrypted. */ public byte[] getPrivKeyBytes() { return Utils.bigIntegerToBytes(getPrivKey(), 32); }
/** * Returns a 32 byte array containing the private key. * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key bytes are missing/encrypted. */ public byte[] getPrivKeyBytes() { return Utils.bigIntegerToBytes(getPrivKey(), 32); }
/** * Returns a 32 byte array containing the private key. * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key bytes are missing/encrypted. */ public byte[] getPrivKeyBytes() { return Utils.bigIntegerToBytes(getPrivKey(), 32); }
public String encryptFor(String xpub, String payload) throws UnsupportedEncodingException, InvalidCipherTextException { ECKey myKey = getNode(); DeterministicKey otherKey = DeterministicKey.deserializeB58(null, xpub, PersistentUrls.getInstance().getCurrentNetworkParams()); byte[] sharedSecret = otherKey.getPubKeyPoint().multiply(myKey.getPrivKey()).getEncoded(); byte[] sharedKey = Sha256Hash.hash(sharedSecret); return new String(AESUtil.encryptWithKey(sharedKey, payload)); }
public String decryptFrom(String xpub, String payload) throws UnsupportedEncodingException, InvalidCipherTextException { ECKey myKey = getNode(); DeterministicKey otherKey = DeterministicKey.deserializeB58(null, xpub, PersistentUrls.getInstance().getCurrentNetworkParams()); byte[] sharedSecret = otherKey.getPubKeyPoint().multiply(myKey.getPrivKey()).getEncoded(); byte[] sharedKey = Sha256Hash.hash(sharedSecret); return AESUtil.decryptWithKey(sharedKey, payload); }
public static ECDHKeySet getSharedSecret (ECKey keyServer, ECKey keyClient) { try { ECPrivateKeySpec specPrivate = new ECPrivateKeySpec(keyServer.getPrivKey(), ecParameters); ECPublicKeySpec specPublic = new ECPublicKeySpec(new ECPoint(keyClient.getPubKeyPoint().getXCoord().toBigInteger(), keyClient.getPubKeyPoint() .getYCoord().toBigInteger()), ecParameters); ECPrivateKey privateKey = (ECPrivateKey) kf.generatePrivate(specPrivate); ECPublicKey publicKey = (ECPublicKey) kf.generatePublic(specPublic); JCEECPrivateKey ecPrivKey = new JCEECPrivateKey(privateKey); JCEECPublicKey ecPubKey = new JCEECPublicKey(publicKey); KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH"); aKeyAgree.init(ecPrivKey); aKeyAgree.doPhase(ecPubKey, true); return new ECDHKeySet(aKeyAgree.generateSecret(), keyServer.getPubKey(), keyClient.getPubKey()); } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void testKeyPairRoundtrip() throws Exception { byte[] privkeyASN1 = HEX.decode( "3082011302010104205c0b98e524ad188ddef35dc6abba13c34a351a05409e5d285403718b93336a4aa081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a144034200042af7a2aafe8dafd7dc7f9cfb58ce09bda7dce28653ab229b98d1d3d759660c672dd0db18c8c2d76aa470448e876fc2089ab1354c01a6e72cefc50915f4a963ee"); ECKey decodedKey = ECKey.fromASN1(privkeyASN1); // Now re-encode and decode the ASN.1 to see if it is equivalent (it does not produce the exact same byte // sequence, some integers are padded now). ECKey roundtripKey = ECKey.fromPrivateAndPrecalculatedPublic(decodedKey.getPrivKey(), decodedKey.getPubKeyPoint()); for (ECKey key : new ECKey[] {decodedKey, roundtripKey}) { byte[] message = reverseBytes(HEX.decode( "11da3761e86431e4a54c176789e41f1651b324d240d599a7067bee23d328ec2a")); byte[] output = key.sign(Sha256Hash.wrap(message)).encodeToDER(); assertTrue(key.verify(message, output)); output = HEX.decode( "304502206faa2ebc614bf4a0b31f0ce4ed9012eb193302ec2bcaccc7ae8bb40577f47549022100c73a1a1acc209f3f860bf9b9f5e13e9433db6f8b7bd527a088a0e0cd0a4c83e9"); assertTrue(key.verify(message, output)); } // Try to sign with one key and verify with the other. byte[] message = reverseBytes(HEX.decode( "11da3761e86431e4a54c176789e41f1651b324d240d599a7067bee23d328ec2a")); assertTrue(roundtripKey.verify(message, decodedKey.sign(Sha256Hash.wrap(message)).encodeToDER())); assertTrue(decodedKey.verify(message, roundtripKey.sign(Sha256Hash.wrap(message)).encodeToDER())); // Verify bytewise equivalence of public keys (i.e. compression state is preserved) ECKey key = new ECKey(); ECKey key2 = ECKey.fromASN1(key.toASN1()); assertArrayEquals(key.getPubKey(), key2.getPubKey()); }