Class PemCodec

java.lang.Object
cloud.opencode.base.crypto.codec.PemCodec

public final class PemCodec extends Object
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 Type
    Method
    Description
    static byte[]
    Decode PEM formatted string to bytes 将 PEM 格式字符串解码为字节
    static byte[]
    Decode a string that is either PEM-with-markers or bare Base64.
    static byte[]
    Decode PEM formatted certificate to bytes 将 PEM 格式的证书解码为字节
    static byte[]
    Decode PEM formatted private key to bytes 将 PEM 格式的私钥解码为字节
    static byte[]
    Decode PEM formatted public key to bytes 将 PEM 格式的公钥解码为字节
    static String
    encode(String type, byte[] data)
    Encode data to PEM format with custom type 使用自定义类型将数据编码为 PEM 格式
    static String
    encodeCertificate(byte[] cert)
    Encode certificate bytes to PEM format 将证书字节编码为 PEM 格式
    static String
    encodePrivateKey(byte[] key)
    Encode private key bytes to PEM format 将私钥字节编码为 PEM 格式
    static String
    encodePublicKey(byte[] key)
    Encode public key bytes to PEM format 将公钥字节编码为 PEM 格式
    static String
    Get PEM type identifier from PEM string 从 PEM 字符串获取类型标识符
    static boolean
    isPem(String input)
    Check whether the input string appears to be PEM-formatted (starts with -----BEGIN).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • encodePublicKey

      public static String encodePublicKey(byte[] key)
      Encode public key bytes to PEM format 将公钥字节编码为 PEM 格式
      Parameters:
      key - public key bytes
      Returns:
      PEM formatted string
      Throws:
      NullPointerException - if key is null
    • decodePublicKey

      public static byte[] decodePublicKey(String pem)
      Decode PEM formatted public key to bytes 将 PEM 格式的公钥解码为字节
      Parameters:
      pem - PEM formatted public key
      Returns:
      decoded key bytes
      Throws:
      NullPointerException - if pem is null
      IllegalArgumentException - if pem is not valid format
    • encodePrivateKey

      public static String encodePrivateKey(byte[] key)
      Encode private key bytes to PEM format 将私钥字节编码为 PEM 格式
      Parameters:
      key - private key bytes
      Returns:
      PEM formatted string
      Throws:
      NullPointerException - if key is null
    • decodePrivateKey

      public static byte[] decodePrivateKey(String pem)
      Decode PEM formatted private key to bytes 将 PEM 格式的私钥解码为字节
      Parameters:
      pem - PEM formatted private key
      Returns:
      decoded key bytes
      Throws:
      NullPointerException - if pem is null
      IllegalArgumentException - if pem is not valid format
    • encodeCertificate

      public static String encodeCertificate(byte[] cert)
      Encode certificate bytes to PEM format 将证书字节编码为 PEM 格式
      Parameters:
      cert - certificate bytes
      Returns:
      PEM formatted string
      Throws:
      NullPointerException - if cert is null
    • decodeCertificate

      public static byte[] decodeCertificate(String pem)
      Decode PEM formatted certificate to bytes 将 PEM 格式的证书解码为字节
      Parameters:
      pem - PEM formatted certificate
      Returns:
      decoded certificate bytes
      Throws:
      NullPointerException - if pem is null
      IllegalArgumentException - if pem is not valid format
    • encode

      public static String encode(String type, byte[] data)
      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 null
      IllegalArgumentException - if type is empty
    • decode

      public static byte[] decode(String pem)
      Decode PEM formatted string to bytes 将 PEM 格式字符串解码为字节
      Parameters:
      pem - PEM formatted string
      Returns:
      decoded bytes
      Throws:
      NullPointerException - if pem is null
      IllegalArgumentException - if pem is not valid format
    • getType

      public static String getType(String pem)
      Get PEM type identifier from PEM string 从 PEM 字符串获取类型标识符
      Parameters:
      pem - PEM formatted string
      Returns:
      PEM type identifier
      Throws:
      NullPointerException - if pem is null
      IllegalArgumentException - if pem is not valid format
    • decodeBase64OrPem

      public static byte[] decodeBase64OrPem(String input)
      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 via decode(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 null
      IllegalArgumentException - if input is neither valid PEM nor valid Base64
      Since:
      V1.0.4
    • isPem

      public static boolean isPem(String input)
      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