public static String readPemFileAsBase64(final File file) throws IOException
	{
		final byte[] keyBytes = Files.readAllBytes(file.toPath());
		final String privateKeyPem = new String(keyBytes);
		String privateKeyAsBase64String = null;
		if (privateKeyPem.indexOf(BEGIN_PRIVATE_KEY_PREFIX) != -1)
		{
			// PKCS#8 format
			privateKeyAsBase64String = new String(keyBytes).replace(BEGIN_PRIVATE_KEY_PREFIX, "")
				.replace(END_PRIVATE_KEY_SUFFIX, "").trim();
		}
		if (privateKeyPem.indexOf(BEGIN_RSA_PRIVATE_KEY_PREFIX) != -1)
		{
			// PKCS#1 format
			privateKeyAsBase64String = new String(keyBytes)
				.replace(BEGIN_RSA_PRIVATE_KEY_PREFIX, "").replace(END_RSA_PRIVATE_KEY_SUFFIX, "")
				.trim();
		}
		return privateKeyAsBase64String;
	}
public static String readPemFileAsBase64 ( final File file ) throws IOException { final byte [ ] keyBytes = Files . readAllBytes ( file . toPath ( ) ) ; final String privateKeyPem = new String ( keyBytes ) ; String privateKeyAsBase64String = null ; if ( privateKeyPem . indexOf ( BEGIN_PRIVATE_KEY_PREFIX ) != - 1 ) { privateKeyAsBase64String = new String ( keyBytes ) . replace ( BEGIN_PRIVATE_KEY_PREFIX , "" ) . replace ( END_PRIVATE_KEY_SUFFIX , "" ) . trim ( ) ; } if ( privateKeyPem . indexOf ( BEGIN_RSA_PRIVATE_KEY_PREFIX ) != - 1 ) { privateKeyAsBase64String = new String ( keyBytes ) . replace ( BEGIN_RSA_PRIVATE_KEY_PREFIX , "" ) . replace ( END_RSA_PRIVATE_KEY_SUFFIX , "" ) . trim ( ) ; } return privateKeyAsBase64String ; }
Read the private key from a pem file as base64 encoded {@link String} value.

@param file
the file( in *.pem format) that contains the private key
@return the base64 encoded {@link String} value.
@throws IOException
Signals that an I/O exception has occurred.