KeyCrypterException: Introduce dedicated InvalidCipherText exception for when a private key or seed is decrypted, the decrypted message is damaged.

This commit is contained in:
Andreas Schildbach
2019-03-31 12:32:52 +02:00
parent 38323538b4
commit ddcaf228e9
2 changed files with 18 additions and 1 deletions

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}