Java Code Examples for org.bitcoinj.core.ECKey#encrypt()

The following examples show how to use org.bitcoinj.core.ECKey#encrypt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicKeyChain.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Encrypt the wallet using the KeyCrypter and the AES key. A good default KeyCrypter to use is
 * {@link KeyCrypterScrypt}.
 *
 * @param keyCrypter The KeyCrypter that specifies how to encrypt/ decrypt a key
 * @param aesKey     AES key to use (normally created using KeyCrypter#deriveKey and cached as it is time consuming
 *                   to create from a password)
 * @throws KeyCrypterException Thrown if the wallet encryption fails. If so, the wallet state is unchanged.
 */
@Override
public BasicKeyChain toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey) {
    lock.lock();
    try {
        checkNotNull(keyCrypter);
        checkState(this.keyCrypter == null, "Key chain is already encrypted");
        BasicKeyChain encrypted = new BasicKeyChain(keyCrypter);
        for (ECKey key : hashToKeys.values()) {
            ECKey encryptedKey = key.encrypt(keyCrypter, aesKey);
            // Check that the encrypted key can be successfully decrypted.
            // This is done as it is a critical failure if the private key cannot be decrypted successfully
            // (all bitcoin controlled by that private key is lost forever).
            // For a correctly constructed keyCrypter the encryption should always be reversible so it is just
            // being as cautious as possible.
            if (!ECKey.encryptionIsReversible(key, encryptedKey, keyCrypter, aesKey))
                throw new KeyCrypterException("The key " + key.toString() + " cannot be successfully decrypted after encryption so aborting wallet encryption.");
            encrypted.importKeyLocked(encryptedKey);
        }
        return encrypted;
    } finally {
        lock.unlock();
    }
}
 
Example 2
Source File: BasicKeyChain.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Encrypt the wallet using the KeyCrypter and the AES key. A good default KeyCrypter to use is
 * {@link org.bitcoinj.crypto.KeyCrypterScrypt}.
 *
 * @param keyCrypter The KeyCrypter that specifies how to encrypt/ decrypt a key
 * @param aesKey AES key to use (normally created using KeyCrypter#deriveKey and cached as it is time consuming
 *               to create from a password)
 * @throws KeyCrypterException Thrown if the wallet encryption fails. If so, the wallet state is unchanged.
 */
@Override
public BasicKeyChain toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey) {
    lock.lock();
    try {
        checkNotNull(keyCrypter);
        checkState(this.keyCrypter == null, "Key chain is already encrypted");
        BasicKeyChain encrypted = new BasicKeyChain(keyCrypter);
        for (ECKey key : hashToKeys.values()) {
            ECKey encryptedKey = key.encrypt(keyCrypter, aesKey);
            // Check that the encrypted key can be successfully decrypted.
            // This is done as it is a critical failure if the private key cannot be decrypted successfully
            // (all bitcoin controlled by that private key is lost forever).
            // For a correctly constructed keyCrypter the encryption should always be reversible so it is just
            // being as cautious as possible.
            if (!ECKey.encryptionIsReversible(key, encryptedKey, keyCrypter, aesKey))
                throw new KeyCrypterException("The key " + key.toString() + " cannot be successfully decrypted after encryption so aborting wallet encryption.");
            encrypted.importKeyLocked(encryptedKey);
        }
        return encrypted;
    } finally {
        lock.unlock();
    }
}
 
Example 3
Source File: BasicKeyChain.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Encrypt the wallet using the KeyCrypter and the AES key. A good default KeyCrypter to use is
 * {@link org.bitcoinj.crypto.KeyCrypterScrypt}.
 *
 * @param keyCrypter The KeyCrypter that specifies how to encrypt/ decrypt a key
 * @param aesKey AES key to use (normally created using KeyCrypter#deriveKey and cached as it is time consuming
 *               to create from a password)
 * @throws KeyCrypterException Thrown if the wallet encryption fails. If so, the wallet state is unchanged.
 */
@Override
public BasicKeyChain toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey) {
    lock.lock();
    try {
        checkNotNull(keyCrypter);
        checkState(this.keyCrypter == null, "Key chain is already encrypted");
        BasicKeyChain encrypted = new BasicKeyChain(keyCrypter);
        for (ECKey key : hashToKeys.values()) {
            ECKey encryptedKey = key.encrypt(keyCrypter, aesKey);
            // Check that the encrypted key can be successfully decrypted.
            // This is done as it is a critical failure if the private key cannot be decrypted successfully
            // (all bitcoin controlled by that private key is lost forever).
            // For a correctly constructed keyCrypter the encryption should always be reversible so it is just
            // being as cautious as possible.
            if (!ECKey.encryptionIsReversible(key, encryptedKey, keyCrypter, aesKey))
                throw new KeyCrypterException("The key " + key.toString() + " cannot be successfully decrypted after encryption so aborting wallet encryption.");
            encrypted.importKeyLocked(encryptedKey);
        }
        return encrypted;
    } finally {
        lock.unlock();
    }
}
 
Example 4
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void addEncryptedKeyToUnencryptedWallet() throws Exception {
    Wallet encryptedWallet = new Wallet(UNITTEST);
    encryptedWallet.encrypt(PASSWORD1);
    KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();

    ECKey key1 = new ECKey();
    key1 = key1.encrypt(keyCrypter, keyCrypter.deriveKey("PASSWORD!"));
    wallet.importKey(key1);
}
 
Example 5
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void addEncryptedKeyToUnencryptedWallet() throws Exception {
    Wallet encryptedWallet = new Wallet(PARAMS);
    encryptedWallet.encrypt(PASSWORD1);
    KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();

    ECKey key1 = new ECKey();
    key1 = key1.encrypt(keyCrypter, keyCrypter.deriveKey("PASSWORD!"));
    wallet.importKey(key1);
}
 
Example 6
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void addEncryptedKeyToUnencryptedWallet() throws Exception {
    Wallet encryptedWallet = new Wallet(PARAMS);
    encryptedWallet.encrypt(PASSWORD1);
    KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();

    ECKey key1 = new ECKey();
    key1 = key1.encrypt(keyCrypter, keyCrypter.deriveKey("PASSWORD!"));
    wallet.importKey(key1);
}