<p>Attempting to implement the interface <code>KeyCrypter</code> is a worthy exercise but it seems to require more than a few simple lines of code. <code>bitcoinj</code> offers one implementation with the class <code>KeyCrypterScrypt</code> and you may wish to look at the source file for this class. However, because this implementation exists, you do not need to provide your own implementation of <code>KeyCrypter</code> if all you want to do is have the ability to encrypt and decrypt keys. This can be done as follows:</p>

<pre><code>import org.bitcoinj.core.ECKey;
import org.bitcoinj.crypto.KeyCrypter;
import org.bitcoinj.crypto.KeyCrypterScrypt;
import org.spongycastle.crypto.params.KeyParameter;

public class Test {

  public static void main(String[] args){

  ECKey k1 = new ECKey(); // some random key

  // encrypting a key
  KeyCrypter crypter1 = new KeyCrypterScrypt();
  KeyParameter aesKey1 = crypter1.deriveKey("some arbitrary passphrase");
  ECKey k2 = k1.encrypt(crypter1, aesKey1);
  System.out.println(k2.isEncrypted()); // true

  // decrypting a key
  KeyCrypter crypter2 = k2.getKeyCrypter();
  KeyParameter aesKey2 = crypter2.deriveKey("some arbitrary passphrase");
  ECKey k3 = k2.decrypt(aesKey2);
  System.out.println(k1.equals(k3));  // true
  }
}
</code></pre>

CC BY-SA 3.0
2016-08-23T15:56:01.003Z
2016-08-23T15:56:01.003Z
Sven Williamson
<p>I am trying to decrypt my private key bytes using the <code>KeyCrypter</code> interface. But i do not know how to implement and call the <code>decrypt()</code> function. Initially i encrypt the bytes of my Private Key of my <code>ECKey</code>.</p>

<p>Here is my code:</p>

<pre><code>private ECKey myKey;
private CharSequence password = "myPassword";


public KeyParameter deriveKey(CharSequence password)
            throws KeyCrypterException {

        String s = password.toString();
        KeyParameter keyParameter = new KeyParameter(s.getBytes());

        return keyParameter;
    }

    public EncryptedData encrypt(byte[] plainBytes,
            org.spongycastle.crypto.params.KeyParameter aesKey)
            throws KeyCrypterException {

        plainBytes = myKey.getPrivKeyBytes();

        EncryptedData myData = new EncryptedData(plainBytes, aesKey.getKey());
        System.out.println(myData);

        return myData;
    }

    public byte[] decrypt(EncryptedData encryptedBytesToDecode,
            KeyParameter aesKey) throws KeyCrypterException {
        // TODO Auto-generated method stub

        return null;
    }
</code></pre>

<p>which is the byte [] result=...returning from my <code>decrypt()</code> function?</p>

<private-key><bitcoinj><encryption>
Decrypt my private key using KeyCrypter
gtopal
# Decrypt my private key using KeyCrypter

(Asked by: username_0 on 2016-01-11)

I am trying to decrypt my private key bytes using the `KeyCrypter` interface. But i do not know how to implement and call the `decrypt()` function. Initially i encrypt the bytes of my Private Key of my `ECKey`.

Here is my code:

```
private ECKey myKey;
private CharSequence password = "myPassword";

public KeyParameter deriveKey(CharSequence password)
            throws KeyCrypterException {

        String s = password.toString();
        KeyParameter keyParameter = new KeyParameter(s.getBytes());

        return keyParameter;
    }

    public EncryptedData encrypt(byte[] plainBytes,
            org.spongycastle.crypto.params.KeyParameter aesKey)
            throws KeyCrypterException {

        plainBytes = myKey.getPrivKeyBytes();

        EncryptedData myData = new EncryptedData(plainBytes, aesKey.getKey());
        System.out.println(myData);

        return myData;
    }

    public byte[] decrypt(EncryptedData encryptedBytesToDecode,
            KeyParameter aesKey) throws KeyCrypterException {
        // TODO Auto-generated method stub

        return null;
    }

```

which is the byte [] result=...returning from my `decrypt()` function?

**Tags:** private-key, bitcoinj, encryption


---

## 1 answers

### Answer by username_1 on 2016-08-23. Score: 2 | Accepted answer

Attempting to implement the interface `KeyCrypter` is a worthy exercise but it seems to require more than a few simple lines of code. `bitcoinj` offers one implementation with the class `KeyCrypterScrypt` and you may wish to look at the source file for this class. However, because this implementation exists, you do not need to provide your own implementation of `KeyCrypter` if all you want to do is have the ability to encrypt and decrypt keys. This can be done as follows:

```
import org.bitcoinj.core.ECKey;
import org.bitcoinj.crypto.KeyCrypter;
import org.bitcoinj.crypto.KeyCrypterScrypt;
import org.spongycastle.crypto.params.KeyParameter;

public class Test {

  public static void main(String[] args){

  ECKey k1 = new ECKey(); // some random key

  // encrypting a key
  KeyCrypter crypter1 = new KeyCrypterScrypt();
  KeyParameter aesKey1 = crypter1.deriveKey("some arbitrary passphrase");
  ECKey k2 = k1.encrypt(crypter1, aesKey1);
  System.out.println(k2.isEncrypted()); // true

  // decrypting a key
  KeyCrypter crypter2 = k2.getKeyCrypter();
  KeyParameter aesKey2 = crypter2.deriveKey("some arbitrary passphrase");
  ECKey k3 = k2.decrypt(aesKey2);
  System.out.println(k1.equals(k3));  // true
  }
}

```


---

