From 4273dacc00e2ab37c693ded132a372da10bc540c Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 29 Mar 2013 17:05:39 +0000 Subject: [PATCH] Check that the private key decrypted correctly in ECKey.sign(). Resolves issue 359. --- core/src/main/java/com/google/bitcoin/core/ECKey.java | 3 +++ .../test/java/com/google/bitcoin/core/WalletTest.java | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/bitcoin/core/ECKey.java b/core/src/main/java/com/google/bitcoin/core/ECKey.java index 1fdf2873..cee53a1e 100644 --- a/core/src/main/java/com/google/bitcoin/core/ECKey.java +++ b/core/src/main/java/com/google/bitcoin/core/ECKey.java @@ -375,6 +375,9 @@ public class ECKey implements Serializable { } privateKeyForSigning = new BigInteger(1, keyCrypter.decrypt(encryptedPrivateKey, aesKey)); + // Check encryption was correct. + if (!Arrays.equals(pub, publicKeyFromPrivate(privateKeyForSigning, isCompressed()))) + throw new KeyCrypterException("Could not decrypt bytes"); } else { // No decryption of private key required. if (priv == null) { diff --git a/core/src/test/java/com/google/bitcoin/core/WalletTest.java b/core/src/test/java/com/google/bitcoin/core/WalletTest.java index 280d5766..fb705c77 100644 --- a/core/src/test/java/com/google/bitcoin/core/WalletTest.java +++ b/core/src/test/java/com/google/bitcoin/core/WalletTest.java @@ -168,7 +168,14 @@ public class WalletTest { @Test public void basicSpendingWithEncryptedHetergeneousWallet() throws Exception { - basicSpendingCommon(encryptedHetergeneousWallet, myEncryptedAddress2, true); + for (int i = 0; i < 100; i++) { + encryptedHetergeneousWallet = new Wallet(params, keyCrypter); + myKey2 = new ECKey(); + encryptedHetergeneousWallet.addKey(myKey2); + myEncryptedKey2 = encryptedHetergeneousWallet.addNewEncryptedKey(keyCrypter, aesKey); + myEncryptedAddress2 = myEncryptedKey2.toAddress(params); + basicSpendingCommon(encryptedHetergeneousWallet, myEncryptedAddress2, true); + } } private void basicSpendingCommon(Wallet wallet, Address toAddress, boolean testEncryption) throws Exception {