@Override
 public DeterministicKeyChain toDecrypted(CharSequence password) {
   checkNotNull(password);
   checkArgument(password.length() > 0);
   KeyCrypter crypter = getKeyCrypter();
   checkState(crypter != null, "Chain not encrypted");
   KeyParameter derivedKey = crypter.deriveKey(password);
   return toDecrypted(derivedKey);
 }
 @Override
 public DeterministicKeyChain toEncrypted(CharSequence password) {
   checkNotNull(password);
   checkArgument(password.length() > 0);
   checkState(seed != null, "Attempt to encrypt a watching chain.");
   checkState(!seed.isEncrypted());
   KeyCrypter scrypt = new KeyCrypterScrypt();
   KeyParameter derivedKey = scrypt.deriveKey(password);
   return toEncrypted(scrypt, derivedKey);
 }
 // 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.
 private BigInteger findOrDeriveEncryptedPrivateKey(KeyCrypter keyCrypter, KeyParameter aesKey) {
   if (encryptedPrivateKey != null)
     return new BigInteger(1, keyCrypter.decrypt(encryptedPrivateKey, aesKey));
   // 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.
   DeterministicKey cursor = parent;
   while (cursor != null) {
     if (cursor.encryptedPrivateKey != null) break;
     cursor = cursor.parent;
   }
   if (cursor == null)
     throw new KeyCrypterException(
         "Neither this key nor its parents have an encrypted private key");
   byte[] parentalPrivateKeyBytes = keyCrypter.decrypt(cursor.encryptedPrivateKey, aesKey);
   return derivePrivateKeyDownwards(cursor, parentalPrivateKeyBytes);
 }
 public DeterministicKey encrypt(
     KeyCrypter keyCrypter, KeyParameter aesKey, @Nullable DeterministicKey newParent)
     throws KeyCrypterException {
   // Same as the parent code, except we construct a DeterministicKey instead of an ECKey.
   checkNotNull(keyCrypter);
   if (newParent != null) checkArgument(newParent.isEncrypted());
   final byte[] privKeyBytes = getPrivKeyBytes();
   checkState(privKeyBytes != null, "Private key is not available");
   EncryptedData encryptedPrivateKey = keyCrypter.encrypt(privKeyBytes, aesKey);
   DeterministicKey key =
       new DeterministicKey(
           childNumberPath, chainCode, keyCrypter, pub, encryptedPrivateKey, newParent);
   if (newParent == null) key.setCreationTimeSeconds(getCreationTimeSeconds());
   return key;
 }