/** * Convenience wrapper around {@link #toEncrypted(org.bitcoinj.crypto.KeyCrypter, * org.spongycastle.crypto.params.KeyParameter)} which uses the default Scrypt key derivation algorithm and * parameters, derives a key from the given password and returns the created key. */ @Override public BasicKeyChain toEncrypted(CharSequence password) { checkNotNull(password); checkArgument(password.length() > 0); KeyCrypter scrypt = new KeyCrypterScrypt(); KeyParameter derivedKey = scrypt.deriveKey(password); return toEncrypted(scrypt, derivedKey); }
/** * Convenience wrapper around {@link #toEncrypted(org.bitcoinj.crypto.KeyCrypter, * org.spongycastle.crypto.params.KeyParameter)} which uses the default Scrypt key derivation algorithm and * parameters, derives a key from the given password and returns the created key. */ @Override public BasicKeyChain toEncrypted(CharSequence password) { checkNotNull(password); checkArgument(password.length() > 0); KeyCrypter scrypt = new KeyCrypterScrypt(); KeyParameter derivedKey = scrypt.deriveKey(password); return toEncrypted(scrypt, derivedKey); }
/** * Convenience wrapper around {@link #toEncrypted(org.bitcoinj.crypto.KeyCrypter, * org.spongycastle.crypto.params.KeyParameter)} which uses the default Scrypt key derivation algorithm and * parameters, derives a key from the given password and returns the created key. */ @Override public BasicKeyChain toEncrypted(CharSequence password) { checkNotNull(password); checkArgument(password.length() > 0); KeyCrypter scrypt = new KeyCrypterScrypt(); KeyParameter derivedKey = scrypt.deriveKey(password); return toEncrypted(scrypt, derivedKey); }
/** * Convenience wrapper around {@link #toEncrypted(org.bitcoinj.crypto.KeyCrypter, * org.spongycastle.crypto.params.KeyParameter)} which uses the default Scrypt key derivation algorithm and * parameters, derives a key from the given password and returns the created key. */ @Override public BasicKeyChain toEncrypted(CharSequence password) { checkNotNull(password); checkArgument(password.length() > 0); KeyCrypter scrypt = new KeyCrypterScrypt(); KeyParameter derivedKey = scrypt.deriveKey(password); return toEncrypted(scrypt, derivedKey); }
/** * Encrypt the keys in the group using the KeyCrypter and the AES key. A good default KeyCrypter to use is * {@link org.bitcoinj.crypto.KeyCrypterScrypt}. * * @throws org.bitcoinj.crypto.KeyCrypterException Thrown if the wallet encryption fails for some reason, * leaving the group unchanged. * @throws DeterministicUpgradeRequiredException Thrown if there are random keys but no HD chain. */ public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { checkNotNull(keyCrypter); checkNotNull(aesKey); // This code must be exception safe. BasicKeyChain newBasic = basic.toEncrypted(keyCrypter, aesKey); List<DeterministicKeyChain> newChains = new ArrayList<>(chains.size()); if (chains.isEmpty() && basic.numKeys() == 0) { // No HD chains and no random keys: encrypting an entirely empty keychain group. But we can't do that, we // must have something to encrypt: so instantiate a new HD chain here. createAndActivateNewHDChain(); } for (DeterministicKeyChain chain : chains) newChains.add(chain.toEncrypted(keyCrypter, aesKey)); this.keyCrypter = keyCrypter; basic = newBasic; chains.clear(); chains.addAll(newChains); }
/** * Encrypt the keys in the group using the KeyCrypter and the AES key. A good default KeyCrypter to use is * {@link org.bitcoinj.crypto.KeyCrypterScrypt}. * * @throws org.bitcoinj.crypto.KeyCrypterException Thrown if the wallet encryption fails for some reason, * leaving the group unchanged. * @throws DeterministicUpgradeRequiredException Thrown if there are random keys but no HD chain. */ public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { checkNotNull(keyCrypter); checkNotNull(aesKey); // This code must be exception safe. BasicKeyChain newBasic = basic.toEncrypted(keyCrypter, aesKey); List<DeterministicKeyChain> newChains = new ArrayList<DeterministicKeyChain>(chains.size()); if (chains.isEmpty() && basic.numKeys() == 0) { // No HD chains and no random keys: encrypting an entirely empty keychain group. But we can't do that, we // must have something to encrypt: so instantiate a new HD chain here. createAndActivateNewHDChain(); } for (DeterministicKeyChain chain : chains) newChains.add(chain.toEncrypted(keyCrypter, aesKey)); this.keyCrypter = keyCrypter; basic = newBasic; chains.clear(); chains.addAll(newChains); }
/** * Encrypt the keys in the group using the KeyCrypter and the AES key. A good default KeyCrypter to use is * {@link org.bitcoinj.crypto.KeyCrypterScrypt}. * * @throws org.bitcoinj.crypto.KeyCrypterException Thrown if the wallet encryption fails for some reason, * leaving the group unchanged. * @throws DeterministicUpgradeRequiredException Thrown if there are random keys but no HD chain. */ public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { checkNotNull(keyCrypter); checkNotNull(aesKey); // This code must be exception safe. BasicKeyChain newBasic = basic.toEncrypted(keyCrypter, aesKey); List<DeterministicKeyChain> newChains = new ArrayList<>(chains.size()); if (chains.isEmpty() && basic.numKeys() == 0) { // No HD chains and no random keys: encrypting an entirely empty keychain group. But we can't do that, we // must have something to encrypt: so instantiate a new HD chain here. createAndActivateNewHDChain(); } for (DeterministicKeyChain chain : chains) newChains.add(chain.toEncrypted(keyCrypter, aesKey)); this.keyCrypter = keyCrypter; basic = newBasic; chains.clear(); chains.addAll(newChains); }
/** * Encrypt the keys in the group using the KeyCrypter and the AES key. A good default KeyCrypter to use is * {@link org.bitcoinj.crypto.KeyCrypterScrypt}. * * @throws org.bitcoinj.crypto.KeyCrypterException Thrown if the wallet encryption fails for some reason, * leaving the group unchanged. * @throws DeterministicUpgradeRequiredException Thrown if there are random keys but no HD chain. */ public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { checkNotNull(keyCrypter); checkNotNull(aesKey); // This code must be exception safe. BasicKeyChain newBasic = basic.toEncrypted(keyCrypter, aesKey); List<DeterministicKeyChain> newChains = new ArrayList<DeterministicKeyChain>(chains.size()); if (chains.isEmpty() && basic.numKeys() == 0) { // No HD chains and no random keys: encrypting an entirely empty keychain group. But we can't do that, we // must have something to encrypt: so instantiate a new HD chain here. createAndActivateNewHDChain(); } for (DeterministicKeyChain chain : chains) newChains.add(chain.toEncrypted(keyCrypter, aesKey)); this.keyCrypter = keyCrypter; basic = newBasic; chains.clear(); chains.addAll(newChains); }
@Test(expected = IllegalStateException.class) public void doubleEncryptFails() { final ArrayList<ECKey> keys = Lists.newArrayList(new ECKey(), new ECKey()); chain.importKeys(keys); chain = chain.toEncrypted("foo"); chain.toEncrypted("foo"); }
@Test(expected = KeyCrypterException.class) public void cannotImportEncryptedKey() { final ECKey key1 = new ECKey(); chain.importKeys(ImmutableList.of(key1)); chain = chain.toEncrypted("foobar"); ECKey encryptedKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); assertTrue(encryptedKey.isEncrypted()); BasicKeyChain chain2 = new BasicKeyChain(); chain2.importKeys(ImmutableList.of(encryptedKey)); }
@Test public void serializationEncrypted() throws UnreadableWalletException { ECKey key1 = new ECKey(); chain.importKeys(key1); chain = chain.toEncrypted("foo bar"); key1 = chain.getKeys().get(0); List<Protos.Key> keys = chain.serializeToProtobuf(); assertEquals(1, keys.size()); assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray()); assertFalse(keys.get(0).hasSecretBytes()); assertTrue(keys.get(0).hasEncryptedData()); chain = BasicKeyChain.fromProtobufEncrypted(keys, checkNotNull(chain.getKeyCrypter())); assertEquals(key1.getEncryptedPrivateKey(), chain.getKeys().get(0).getEncryptedPrivateKey()); assertTrue(chain.checkPassword("foo bar")); }
@Test(expected = KeyCrypterException.class) public void cannotMixParams() throws Exception { chain = chain.toEncrypted("foobar"); KeyCrypterScrypt scrypter = new KeyCrypterScrypt(2); // Some bogus params. ECKey key1 = new ECKey().encrypt(scrypter, scrypter.deriveKey("other stuff")); chain.importKeys(key1); }
chain.importKeys(key1, new ECKey()); final String PASSWORD = "foobar"; chain = chain.toEncrypted(PASSWORD); final KeyCrypter keyCrypter = chain.getKeyCrypter(); assertNotNull(keyCrypter);