0

I'm trying to get PrivateKey Object from 32-byte raw private key that I got by useing getS() method. I used ECDSA secp256k1 algorithm at bouncy castle library to generate a key pair.

Below is the way that I got 32-byte private key in byte array, and I would like to reverse this process to get PrivateKey object from the 32-byte private key.

Can anyone please help me out with this? I'd really appreciate it.

PrivateKey prvKey;

public byte[] getPrivateKey() {
        ECPrivateKey ecPrv = (ECPrivateKey) this.prvKey;
        byte[] prv = ecPrv.getS().toByteArray();
        if (prv[0] == 0) {
            byte[] rslt = new byte[prv.length - 1];
            System.arraycopy(prv, 1, rslt, 0, prv.length - 1);
            return rslt;
        } else {
            return prv;
        }
    }
Minji Cho
  • 1
  • 1

1 Answers1

0

I found the answer for my question. I used encoded Private key as a parameter for below method. Just in case anyone is still looking for the answer...

private PrivateKey generatePrivateKey(byte[] encodedPrivateKey)
            throws NoSuchAlgorithmException, InvalidKeySpecException {
        Security.addProvider(new BouncyCastleProvider());
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
        return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedPrivateKey));
    }
Minji Cho
  • 1
  • 1