mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-07-30 19:41:24 +00:00
DeterministicKey: Decrypted deterministic keys must always be 32 bytes long, otherwise likely the encryption key was wrong.
This commit is contained in:
@@ -391,8 +391,13 @@ public class DeterministicKey extends ECKey {
|
|||||||
// For when a key is encrypted, either decrypt our encrypted private key bytes, or work up the tree asking parents
|
// For when a key is encrypted, either decrypt our encrypted private key bytes, or work up the tree asking parents
|
||||||
// to decrypt and re-derive.
|
// to decrypt and re-derive.
|
||||||
private BigInteger findOrDeriveEncryptedPrivateKey(KeyCrypter keyCrypter, KeyParameter aesKey) {
|
private BigInteger findOrDeriveEncryptedPrivateKey(KeyCrypter keyCrypter, KeyParameter aesKey) {
|
||||||
if (encryptedPrivateKey != null)
|
if (encryptedPrivateKey != null) {
|
||||||
return new BigInteger(1, keyCrypter.decrypt(encryptedPrivateKey, aesKey));
|
byte[] decryptedKey = keyCrypter.decrypt(encryptedPrivateKey, aesKey);
|
||||||
|
if (decryptedKey.length != 32)
|
||||||
|
throw new KeyCrypterException.InvalidCipherText(
|
||||||
|
"Decrypted key must be 32 bytes long, but is " + decryptedKey.length);
|
||||||
|
return new BigInteger(1, decryptedKey);
|
||||||
|
}
|
||||||
// Otherwise we don't have it, but maybe we can figure it out from our parents. Walk up the tree looking for
|
// Otherwise we don't have it, but maybe we can figure it out from our parents. Walk up the tree looking for
|
||||||
// the first key that has some encrypted private key data.
|
// the first key that has some encrypted private key data.
|
||||||
DeterministicKey cursor = parent;
|
DeterministicKey cursor = parent;
|
||||||
@@ -403,6 +408,9 @@ public class DeterministicKey extends ECKey {
|
|||||||
if (cursor == null)
|
if (cursor == null)
|
||||||
throw new KeyCrypterException("Neither this key nor its parents have an encrypted private key");
|
throw new KeyCrypterException("Neither this key nor its parents have an encrypted private key");
|
||||||
byte[] parentalPrivateKeyBytes = keyCrypter.decrypt(cursor.encryptedPrivateKey, aesKey);
|
byte[] parentalPrivateKeyBytes = keyCrypter.decrypt(cursor.encryptedPrivateKey, aesKey);
|
||||||
|
if (parentalPrivateKeyBytes.length != 32)
|
||||||
|
throw new KeyCrypterException.InvalidCipherText(
|
||||||
|
"Decrypted key must be 32 bytes long, but is " + parentalPrivateKeyBytes.length);
|
||||||
return derivePrivateKeyDownwards(cursor, parentalPrivateKeyBytes);
|
return derivePrivateKeyDownwards(cursor, parentalPrivateKeyBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user