Code example for InvalidKeyException

0
 
        if (key == null) {
            throw new InvalidKeyException("key == null");
        } 
        if (!(key instanceof PublicKey)) {
            throw new InvalidKeyException("Not a public key: " + key.getClass());
        } 
        OpenSSLKey openSslPublicKey = translateKeyToEcOpenSSLKey(key);
 
        byte[] buffer = new byte[mExpectedResultLength];
        int actualResultLength = NativeCrypto.ECDH_compute_key(
                buffer,
                0, 
                openSslPublicKey.getPkeyContext(),
                mOpenSslPrivateKey.getPkeyContext());
        byte[] result;
        if (actualResultLength == -1) {
            throw new RuntimeException("Engine returned " + actualResultLength);
        } else if (actualResultLength == mExpectedResultLength) {
            // The output is as long as expected -- use the whole buffer