private PrivateKey readEcPrivateKey ( String fileName ) throws InvalidKeySpecException , NoSuchAlgorithmException , IOException , NoSuchProviderException { StringBuilder sb = new StringBuilder ( ) ; BufferedReader br = new BufferedReader ( new FileReader ( fileName ) ) ; try { char [ ] cbuf = new char [ 1024 ] ; for ( int i = 0 ; i <= 10 * 1024 ; i ++ ) { if ( ! br . ready ( ) ) break ; int len = br . read ( cbuf , i * 1024 , 1024 ) ; sb . append ( cbuf , 0 , len ) ; } if ( br . ready ( ) ) { throw new IndexOutOfBoundsException ( "JWK file larger than 10MB" ) ; } Type mapType = new TypeToken < Map < String , String > > ( ) { } . getType ( ) ; Map < String , String > son = new Gson ( ) . fromJson ( sb . toString ( ) , mapType ) ; try { @ SuppressWarnings ( { "unchecked" , "rawtypes" } ) EllipticCurveJsonWebKey jwk = new EllipticCurveJsonWebKey ( ( Map < String , Object > ) ( Map ) son ) ; Base64Encoder enc = new Base64Encoder ( ) ; ByteArrayOutputStream os = new ByteArrayOutputStream ( ) ; enc . encode ( jwk . getPrivateKey ( ) . getEncoded ( ) , 0 , jwk . getPrivateKey ( ) . getEncoded ( ) . length , os ) ; return jwk . getPrivateKey ( ) ; } catch ( JoseException e1 ) { e1 . printStackTrace ( ) ; } return null ; } finally { br . close ( ) ; } }
Reads JWK into PrivateKEy instance