Code example for PublicKey

Methods: getEncodedgetFormat

        KeyHolder keyHolder = KeyHolder.getInstance();
        KeyPair keyPair = keyHolder.generateRandomKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        // Get the bytes of the key 
        byte[] keyInBytes = publicKey.getEncoded();
        Log.d(TAG, "Encoding format: " + publicKey.getFormat());
        // THE FOLLOWING DOES NOT MATCH! The reason for this is, that the key is not encoded in raw format but in X.509 
        // format, which adds some length to the key. 
        // Check if the key has the correct length 
        // int expectedKeyLength = KeyHolder.RSA_KEY_LENGTH / 8; 
        // int keyLength = keyInBytes.length; 
        // assertEquals("The key does not have the expected length of " + expectedKeyLength + " bytes! Key length: " 
        // + keyLength, expectedKeyLength, keyLength); 
        // Generate a new key from the bytes 
        PublicKey publicKey2 = keyHolder.generatePublicKeyFromBytes(keyInBytes);
Connect your IDE to all the code out there  Get Codota for Java