public static String getPEMFromRSAPublicKey ( RSAPublicKey publicKey ) { StringBuilder builder = new StringBuilder ( ) ; builder . append ( "-----BEGIN PUBLIC KEY-----\n" ) ; String encoded = new String ( BASE_64 . encode ( publicKey . getEncoded ( ) ) ) ; int start = 0 ; int end = 64 ; try { //noinspection InfiniteLoopStatement while ( true ) { builder . append ( encoded . substring ( start , end ) ) ; builder . append ( "\n" ) ; start = end ; end = start + 64 ; } } catch ( IndexOutOfBoundsException e ) { if ( start != encoded . length ( ) ) { builder . append ( encoded . substring ( start , encoded . length ( ) ) ) ; builder . append ( "\n" ) ; } } builder . append ( "-----END PUBLIC KEY-----\n" ) ; return builder . toString ( ) ; }
Get a PEM formatted string from the provided public key