public static void saveEcKey(ECKey ecKey) throws IOException { byte[] bytes = ecKey.toASN1(); File file; if (KeyUtils.privateKey == null) { file = new File(PRIV_KEY_FILENAME); } else { file = new File(KeyUtils.privateKey); } FileOutputStream output = new FileOutputStream(file); output.write(bytes); output.close(); }
@Test public void testEncryptionKey() throws Exception { System.out.println("\nBegin Test encryptionKey:"); EncryptionKey encryptionKey1 = new EncryptionKeyImpl(testKeys.getPublic()); // // PublicKey publicKey = BitcoinCrypto.loadPublicKey(Base64.getEncoder().encodeToString(ecKey.getPubKey())); System.out.println("ecKey: " + ecKey.toString()); System.out.println("ecKey priv: " + ecKey.getPrivateKeyAsHex()); System.out.println("secureRandom: " + secureRandom.nextInt()); System.out.println("decryptionKey: " + decryptionKey.toString()); System.out.println("ASN.1 " + Arrays.toString(ecKey.toASN1())); EncryptionKeyImpl encTest = new EncryptionKeyImpl(publicTestKey); System.out.println("\nencTest: " + encTest); System.out.println("encryptionKey: " + encryptionKey1); System.out.println("EncKey.toString from ECKeys Pub: " + encTest.toString()); System.out.println("EncKey from DecKey to string: " + decryptionKey.EncryptionKey().toString()); encryptionKey = new EncryptionKeyImpl(publicTestKey); assertEquals(encryptionKey.toString(), decryptionKey.EncryptionKey().toString()); }
@Test public void testASN1Roundtrip() 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.fromASN1(decodedKey.toASN1()); assertArrayEquals(decodedKey.getPrivKeyBytes(), roundtripKey.getPrivKeyBytes()); 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())); }
@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()); }