diff --git a/core/src/main/java/org/bitcoinj/crypto/KeyCrypterException.java b/core/src/main/java/org/bitcoinj/crypto/KeyCrypterException.java index 5e8ae9af..c38b5f29 100644 --- a/core/src/main/java/org/bitcoinj/crypto/KeyCrypterException.java +++ b/core/src/main/java/org/bitcoinj/crypto/KeyCrypterException.java @@ -49,4 +49,18 @@ public class KeyCrypterException extends RuntimeException { super(message, throwable); } } + + /** + * This exception is thrown when a private key or seed is decrypted, the decrypted message is damaged + * (e.g. the padding is damaged). This likely means the wrong decryption key has been used. + */ + public static class InvalidCipherText extends KeyCrypterException { + public InvalidCipherText(String message) { + super(message); + } + + public InvalidCipherText(String message, Throwable throwable) { + super(message, throwable); + } + } } diff --git a/core/src/main/java/org/bitcoinj/crypto/KeyCrypterScrypt.java b/core/src/main/java/org/bitcoinj/crypto/KeyCrypterScrypt.java index 3d4f328c..cff7e038 100644 --- a/core/src/main/java/org/bitcoinj/crypto/KeyCrypterScrypt.java +++ b/core/src/main/java/org/bitcoinj/crypto/KeyCrypterScrypt.java @@ -29,6 +29,7 @@ import org.bouncycastle.crypto.engines.AESEngine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.bouncycastle.crypto.BufferedBlockCipher; +import org.bouncycastle.crypto.InvalidCipherTextException; import org.bouncycastle.crypto.modes.CBCBlockCipher; import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; @@ -224,7 +225,9 @@ public class KeyCrypterScrypt implements KeyCrypter { final int length2 = cipher.doFinal(decryptedBytes, length1); return Arrays.copyOf(decryptedBytes, length1 + length2); - } catch (Exception e) { + } catch (InvalidCipherTextException e) { + throw new KeyCrypterException.InvalidCipherText("Could not decrypt bytes", e); + } catch (RuntimeException e) { throw new KeyCrypterException("Could not decrypt bytes", e); } }