Class PemCodec
java.lang.Object
cloud.opencode.base.crypto.codec.PemCodec
PEM format encoding and decoding utility - Handle PEM encoded keys and certificates
PEM 格式编解码工具类 - 处理 PEM 编码的密钥和证书
Features | 主要功能:
- PEM format encoding and decoding - PEM 格式编码和解码
- Certificate and key format support - 证书和密钥格式支持
Usage Examples | 使用示例:
String pem = PemCodec.encode("CERTIFICATE", derBytes);
byte[] der = PemCodec.decode(pem);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-crypto V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]Decode PEM formatted string to bytes 将 PEM 格式字符串解码为字节static byte[]decodeBase64OrPem(String input) Decode a string that is either PEM-with-markers or bare Base64.static byte[]decodeCertificate(String pem) Decode PEM formatted certificate to bytes 将 PEM 格式的证书解码为字节static byte[]decodePrivateKey(String pem) Decode PEM formatted private key to bytes 将 PEM 格式的私钥解码为字节static byte[]decodePublicKey(String pem) Decode PEM formatted public key to bytes 将 PEM 格式的公钥解码为字节static StringEncode data to PEM format with custom type 使用自定义类型将数据编码为 PEM 格式static StringencodeCertificate(byte[] cert) Encode certificate bytes to PEM format 将证书字节编码为 PEM 格式static StringencodePrivateKey(byte[] key) Encode private key bytes to PEM format 将私钥字节编码为 PEM 格式static StringencodePublicKey(byte[] key) Encode public key bytes to PEM format 将公钥字节编码为 PEM 格式static StringGet PEM type identifier from PEM string 从 PEM 字符串获取类型标识符static booleanCheck whether the input string appears to be PEM-formatted (starts with-----BEGIN).
-
Method Details
-
encodePublicKey
Encode public key bytes to PEM format 将公钥字节编码为 PEM 格式- Parameters:
key- public key bytes- Returns:
- PEM formatted string
- Throws:
NullPointerException- if key is null
-
decodePublicKey
Decode PEM formatted public key to bytes 将 PEM 格式的公钥解码为字节- Parameters:
pem- PEM formatted public key- Returns:
- decoded key bytes
- Throws:
NullPointerException- if pem is nullIllegalArgumentException- if pem is not valid format
-
encodePrivateKey
Encode private key bytes to PEM format 将私钥字节编码为 PEM 格式- Parameters:
key- private key bytes- Returns:
- PEM formatted string
- Throws:
NullPointerException- if key is null
-
decodePrivateKey
Decode PEM formatted private key to bytes 将 PEM 格式的私钥解码为字节- Parameters:
pem- PEM formatted private key- Returns:
- decoded key bytes
- Throws:
NullPointerException- if pem is nullIllegalArgumentException- if pem is not valid format
-
encodeCertificate
Encode certificate bytes to PEM format 将证书字节编码为 PEM 格式- Parameters:
cert- certificate bytes- Returns:
- PEM formatted string
- Throws:
NullPointerException- if cert is null
-
decodeCertificate
Decode PEM formatted certificate to bytes 将 PEM 格式的证书解码为字节- Parameters:
pem- PEM formatted certificate- Returns:
- decoded certificate bytes
- Throws:
NullPointerException- if pem is nullIllegalArgumentException- if pem is not valid format
-
encode
Encode data to PEM format with custom type 使用自定义类型将数据编码为 PEM 格式- Parameters:
type- PEM type identifier (e.g., "RSA PRIVATE KEY")data- data bytes to encode- Returns:
- PEM formatted string
- Throws:
NullPointerException- if type or data is nullIllegalArgumentException- if type is empty
-
decode
Decode PEM formatted string to bytes 将 PEM 格式字符串解码为字节- Parameters:
pem- PEM formatted string- Returns:
- decoded bytes
- Throws:
NullPointerException- if pem is nullIllegalArgumentException- if pem is not valid format
-
getType
Get PEM type identifier from PEM string 从 PEM 字符串获取类型标识符- Parameters:
pem- PEM formatted string- Returns:
- PEM type identifier
- Throws:
NullPointerException- if pem is nullIllegalArgumentException- if pem is not valid format
-
decodeBase64OrPem
Decode a string that is either PEM-with-markers or bare Base64. 解码 PEM 格式或裸 Base64 字符串。If the input starts with
-----BEGIN, it is treated as standard PEM and decoded viadecode(String). Otherwise it is treated as bare Base64 (whitespace is stripped before decoding).若输入以
-----BEGIN开头则按标准 PEM 解码; 否则按裸 Base64 解码(先去除空白)。- Parameters:
input- PEM-with-markers or bare Base64 string | PEM 格式或裸 Base64 字符串- Returns:
- decoded bytes | 解码后的字节
- Throws:
NullPointerException- if input is nullIllegalArgumentException- if input is neither valid PEM nor valid Base64- Since:
- V1.0.4
-
isPem
Check whether the input string appears to be PEM-formatted (starts with-----BEGIN). 检查输入字符串是否为 PEM 格式(以-----BEGIN开头)。- Parameters:
input- the input string | 输入字符串- Returns:
- true if PEM-formatted | 如果是 PEM 格式返回 true
- Since:
- V1.0.4
-