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 {