APIdia Launch Partner
Top Description Inners Fields Constructors Methods
sun.security.util

public Class Pem

extends Object
Class Inheritance
Imports
sun.security.pkcs.PKCS8Key, sun.security.x509.AlgorithmId, javax.crypto.EncryptedPrivateKeyInfo, .SecretKey, .SecretKeyFactory, javax.crypto.spec.PBEKeySpec, java.io.*, java.nio.charset.StandardCharsets, java.security.*, java.security.spec.InvalidKeySpecException, .PKCS8EncodedKeySpec, .X509EncodedKeySpec, java.util.Arrays, .Base64, .HexFormat, .Objects, java.util.regex.Pattern

A utility class for PEM format encoding.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Pem.ClearableBufferStream

Clearable ByteArrayOutputStream for temporary data.

Field Summary

Modifier and TypeField and Description
private static volatile Base64.Encoder
private static final byte[]
private static final byte[]
public static final String
public static final String
private static final byte[]
private static final byte[]
public static final String
public static final String
private static final byte[]
private static final Pattern
private static final Pattern
private static volatile ObjectIdentifier
public static final String
public static final String
public static final String
private static final Pattern
public static final String
public static final String
public static final String

Constructor Summary

AccessConstructor and Description
public
Pem()

Method Summary

Modifier and TypeMethod and Description
public static byte[]

Returns:

the decoded bytes
decode
(String
the input string, according to RFC 1421, can only contain characters in the base-64 alphabet and whitespaces.
input
)

Decodes a PEM-encoded block.

public static byte[]
decryptEncoding(EncryptedPrivateKeyInfo ekpi, PBEKeySpec keySpec)

Decrypt the EncryptedPrivateKeyInfo with the given keySpec and return the PKCS#8 byte array

public static ObjectIdentifier
getPBEID(String algorithm)

Return the OID for a given PBE algorithm.

private static boolean
matchesAt(byte[] source, int offset, byte[] match)

public static byte[]
pemEncoded(String type, byte[] base64)

Return a PEM encoding with the given type and base64 byte array.

public static byte[]
pemEncodedFromDER(String type, byte[] der)

public static PEM

Returns:

a PEM instance
readPEM
(InputStream
an InputStream
is
,
boolean
if true, the hyphen length is 4 because the first hyphen is assumed to have been read. This is needed for the CertificateFactory X509 implementation.
shortHeader
)

Read the PEM text and return it in it's three components: header, base64, and footer.

public static PEM
public static BinaryEncodable
toPKCS8Encodable(byte[]
PKCS8 encoding
encoded
,
Provider
KeyFactory provider
provider
)

With a given PKCS8 encoding, construct a PrivateKey or KeyPair.

private static String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

b64Encoderback to summary
private static volatile Base64.Encoder b64Encoder
BEGIN_Bback to summary
private static final byte[] BEGIN_B
BEGIN_PREFIXback to summary
private static final byte[] BEGIN_PREFIX
CERTIFICATEback to summary
public static final String CERTIFICATE
CRLback to summary
public static final String CRL
CRLFback to summary
private static final byte[] CRLF
DASHback to summary
private static final byte[] DASH
DEFAULT_ALGOback to summary
public static final String DEFAULT_ALGO
ENCRYPTED_PRIVATE_KEYback to summary
public static final String ENCRYPTED_PRIVATE_KEY
END_PREFIXback to summary
private static final byte[] END_PREFIX
LINE_WRAP_64_PATTERNback to summary
private static final Pattern LINE_WRAP_64_PATTERN
PBE_PATTERNback to summary
private static final Pattern PBE_PATTERN
PBES2OIDback to summary
private static volatile ObjectIdentifier PBES2OID
PRIVATE_KEYback to summary
public static final String PRIVATE_KEY
PUBLIC_KEYback to summary
public static final String PUBLIC_KEY
RSA_PRIVATE_KEYback to summary
public static final String RSA_PRIVATE_KEY
STRIP_WHITESPACE_PATTERNback to summary
private static final Pattern STRIP_WHITESPACE_PATTERN
X509_CERTIFICATEback to summary
public static final String X509_CERTIFICATE
X509_CRLback to summary
public static final String X509_CRL
X_509_CERTIFICATEback to summary
public static final String X_509_CERTIFICATE

Constructor Detail

Pemback to summary
public Pem()

Method Detail

decodeback to summary
public static byte[] decode(String input)

Decodes a PEM-encoded block.

Parameters
input:String

the input string, according to RFC 1421, can only contain characters in the base-64 alphabet and whitespaces.

Returns:byte[]

the decoded bytes

decryptEncodingback to summary
public static byte[] decryptEncoding(EncryptedPrivateKeyInfo ekpi, PBEKeySpec keySpec) throws NoSuchAlgorithmException, InvalidKeyException

Decrypt the EncryptedPrivateKeyInfo with the given keySpec and return the PKCS#8 byte array

getPBEIDback to summary
public static ObjectIdentifier getPBEID(String algorithm)

Return the OID for a given PBE algorithm. PBES1 has an OID for each algorithm, while PBES2 has one OID for everything that complies with the formatting. Therefore, if the algorithm is not PBES1, it will return PBES2. Cipher will determine if this is a valid PBE algorithm. PBES2 specifies AES as the cipher algorithm, but any block cipher could be supported.

matchesAtback to summary
private static boolean matchesAt(byte[] source, int offset, byte[] match)
pemEncodedback to summary
public static byte[] pemEncoded(String type, byte[] base64)

Return a PEM encoding with the given type and base64 byte array.

pemEncodedFromDERback to summary
public static byte[] pemEncodedFromDER(String type, byte[] der)
readPEMback to summary
public static PEM readPEM(InputStream is, boolean shortHeader) throws IOException

Read the PEM text and return it in it's three components: header, base64, and footer. The header begins processing when "-----B" is read. At that point exceptions will be thrown for syntax errors. The method will leave the stream after reading the end of line of the footer or end of file

Parameters
is:InputStream

an InputStream

shortHeader:boolean

if true, the hyphen length is 4 because the first hyphen is assumed to have been read. This is needed for the CertificateFactory X509 implementation.

Returns:PEM

a PEM instance

Exceptions
IOException:
on IO errors or PEM syntax errors that leave the read position not at the end of a PEM block
EOFException:
when at the unexpected end of the stream
readPEMback to summary
public static PEM readPEM(InputStream is) throws IOException
toPKCS8Encodableback to summary
public static BinaryEncodable toPKCS8Encodable(byte[] encoded, Provider provider) throws InvalidKeyException

With a given PKCS8 encoding, construct a PrivateKey or KeyPair. A KeyPair is returned if requested and the encoding has a public key; otherwise, a PrivateKey is returned.

Parameters
encoded:byte[]

PKCS8 encoding

provider:Provider

KeyFactory provider

typeConverterback to summary
private static String typeConverter(String type)
sun.security.util back to summary

private final Class Pem.ClearableBufferStream

extends ByteArrayOutputStream
Class Inheritance

Clearable ByteArrayOutputStream for temporary data. Access to the internal buffer is allowed to limit data copying. Handle with care.

Field Summary

Inherited from java.io.ByteArrayOutputStream:
bufcount

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv void
pack-priv byte[]
pack-priv int
Inherited from java.io.ByteArrayOutputStream:
closeresetsizetoByteArraytoStringtoStringtoStringtoStringwritewritewriteByteswriteTo

Constructor Detail

ClearableBufferStreamback to summary
pack-priv ClearableBufferStream(int len)

Method Detail

clearback to summary
pack-priv void clear()
getBufferback to summary
pack-priv byte[] getBuffer()
lengthback to summary
pack-priv int length()