Interface AsymmetricCipher

All Known Implementing Classes:
EccCipher, RsaCipher, RsaOaepCipher, Sm2Cipher

public interface AsymmetricCipher
Interface for asymmetric encryption operations - Provides fluent API for public/private key cryptography 非对称加密操作接口 - 提供公钥/私钥加密的流式 API

Features | 主要功能:

  • Public key encryption and private key decryption - 公钥加密和私钥解密
  • Key pair generation and management - 密钥对生成和管理

Usage Examples | 使用示例:

AsymmetricCipher cipher = RsaOaepCipher.sha256();
cipher.setPublicKey(publicKey);
byte[] encrypted = cipher.encrypt(data);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Partial - 空值安全: 部分

Performance | 性能特性:

  • Time complexity: O(k^2) to O(k^3) depending on algorithm - 时间复杂度: O(k^2)~O(k^3),取决于算法
  • Space complexity: O(k) - 空间复杂度: O(k)
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    decrypt(byte[] ciphertext)
    Decrypt data using private key 使用私钥解密数据
    byte[]
    decryptBase64(String base64Ciphertext)
    Decrypt Base64 encoded ciphertext 解密 Base64 编码的密文
    byte[]
    decryptHex(String hexCiphertext)
    Decrypt hexadecimal encoded ciphertext 解密十六进制编码的密文
    decryptToString(byte[] ciphertext)
    Decrypt data and return as string 解密数据并返回字符串
    byte[]
    encrypt(byte[] plaintext)
    Encrypt data using public key 使用公钥加密数据
    byte[]
    encrypt(String plaintext)
    Encrypt string using public key 使用公钥加密字符串
    encryptBase64(byte[] plaintext)
    Encrypt data and return Base64 encoded result 加密数据并返回 Base64 编码结果
    encryptHex(byte[] plaintext)
    Encrypt data and return hexadecimal encoded result 加密数据并返回十六进制编码结果
    Generate a new key pair for this cipher 为此加密器生成新的密钥对
    Get the algorithm name 获取算法名称
    int
    Get maximum size of data that can be encrypted in a single operation 获取单次操作可加密的最大数据大小
    Set both public and private keys from key pair 从密钥对设置公钥和私钥
    setPrivateKey(byte[] encodedKey)
    Set private key from encoded byte array 从编码的字节数组设置私钥
    Set private key for decryption operations 设置用于解密操作的私钥
    Set private key from PEM formatted string 从 PEM 格式字符串设置私钥
    setPublicKey(byte[] encodedKey)
    Set public key from encoded byte array 从编码的字节数组设置公钥
    Set public key for encryption operations 设置用于加密操作的公钥
    Set public key from PEM formatted string 从 PEM 格式字符串设置公钥
  • Method Details

    • setPublicKey

      AsymmetricCipher setPublicKey(PublicKey publicKey)
      Set public key for encryption operations 设置用于加密操作的公钥
      Parameters:
      publicKey - the public key
      Returns:
      this cipher instance for method chaining
      Throws:
      NullPointerException - if publicKey is null
    • setPublicKey

      AsymmetricCipher setPublicKey(byte[] encodedKey)
      Set public key from encoded byte array 从编码的字节数组设置公钥
      Parameters:
      encodedKey - the encoded public key bytes
      Returns:
      this cipher instance for method chaining
      Throws:
      NullPointerException - if encodedKey is null
      OpenKeyException - if key format is invalid
    • setPublicKeyPem

      AsymmetricCipher setPublicKeyPem(String pem)
      Set public key from PEM formatted string 从 PEM 格式字符串设置公钥
      Parameters:
      pem - the PEM formatted public key
      Returns:
      this cipher instance for method chaining
      Throws:
      NullPointerException - if pem is null
      OpenKeyException - if PEM format is invalid
    • setPrivateKey

      AsymmetricCipher setPrivateKey(PrivateKey privateKey)
      Set private key for decryption operations 设置用于解密操作的私钥
      Parameters:
      privateKey - the private key
      Returns:
      this cipher instance for method chaining
      Throws:
      NullPointerException - if privateKey is null
    • setPrivateKey

      AsymmetricCipher setPrivateKey(byte[] encodedKey)
      Set private key from encoded byte array 从编码的字节数组设置私钥
      Parameters:
      encodedKey - the encoded private key bytes
      Returns:
      this cipher instance for method chaining
      Throws:
      NullPointerException - if encodedKey is null
      OpenKeyException - if key format is invalid
    • setPrivateKeyPem

      AsymmetricCipher setPrivateKeyPem(String pem)
      Set private key from PEM formatted string 从 PEM 格式字符串设置私钥
      Parameters:
      pem - the PEM formatted private key
      Returns:
      this cipher instance for method chaining
      Throws:
      NullPointerException - if pem is null
      OpenKeyException - if PEM format is invalid
    • setKeyPair

      AsymmetricCipher setKeyPair(KeyPair keyPair)
      Set both public and private keys from key pair 从密钥对设置公钥和私钥
      Parameters:
      keyPair - the key pair containing public and private keys
      Returns:
      this cipher instance for method chaining
      Throws:
      NullPointerException - if keyPair is null
    • encrypt

      byte[] encrypt(byte[] plaintext)
      Encrypt data using public key 使用公钥加密数据
      Parameters:
      plaintext - the data to encrypt
      Returns:
      encrypted bytes
      Throws:
      NullPointerException - if plaintext is null
      IllegalStateException - if public key is not set
      OpenCryptoException - if encryption fails
    • encrypt

      byte[] encrypt(String plaintext)
      Encrypt string using public key 使用公钥加密字符串
      Parameters:
      plaintext - the string to encrypt
      Returns:
      encrypted bytes
      Throws:
      NullPointerException - if plaintext is null
      IllegalStateException - if public key is not set
      OpenCryptoException - if encryption fails
    • encryptBase64

      String encryptBase64(byte[] plaintext)
      Encrypt data and return Base64 encoded result 加密数据并返回 Base64 编码结果
      Parameters:
      plaintext - the data to encrypt
      Returns:
      Base64 encoded ciphertext
      Throws:
      NullPointerException - if plaintext is null
      IllegalStateException - if public key is not set
      OpenCryptoException - if encryption fails
    • encryptHex

      String encryptHex(byte[] plaintext)
      Encrypt data and return hexadecimal encoded result 加密数据并返回十六进制编码结果
      Parameters:
      plaintext - the data to encrypt
      Returns:
      hexadecimal encoded ciphertext
      Throws:
      NullPointerException - if plaintext is null
      IllegalStateException - if public key is not set
      OpenCryptoException - if encryption fails
    • decrypt

      byte[] decrypt(byte[] ciphertext)
      Decrypt data using private key 使用私钥解密数据
      Parameters:
      ciphertext - the encrypted data
      Returns:
      decrypted bytes
      Throws:
      NullPointerException - if ciphertext is null
      IllegalStateException - if private key is not set
      OpenCryptoException - if decryption fails
    • decryptToString

      String decryptToString(byte[] ciphertext)
      Decrypt data and return as string 解密数据并返回字符串
      Parameters:
      ciphertext - the encrypted data
      Returns:
      decrypted string
      Throws:
      NullPointerException - if ciphertext is null
      IllegalStateException - if private key is not set
      OpenCryptoException - if decryption fails
    • decryptBase64

      byte[] decryptBase64(String base64Ciphertext)
      Decrypt Base64 encoded ciphertext 解密 Base64 编码的密文
      Parameters:
      base64Ciphertext - the Base64 encoded ciphertext
      Returns:
      decrypted bytes
      Throws:
      NullPointerException - if base64Ciphertext is null
      IllegalStateException - if private key is not set
      OpenCryptoException - if decryption fails
    • decryptHex

      byte[] decryptHex(String hexCiphertext)
      Decrypt hexadecimal encoded ciphertext 解密十六进制编码的密文
      Parameters:
      hexCiphertext - the hexadecimal encoded ciphertext
      Returns:
      decrypted bytes
      Throws:
      NullPointerException - if hexCiphertext is null
      IllegalStateException - if private key is not set
      OpenCryptoException - if decryption fails
    • getAlgorithm

      String getAlgorithm()
      Get the algorithm name 获取算法名称
      Returns:
      the algorithm name
    • getMaxEncryptSize

      int getMaxEncryptSize()
      Get maximum size of data that can be encrypted in a single operation 获取单次操作可加密的最大数据大小
      Returns:
      maximum encrypt size in bytes, or -1 if not applicable
    • generateKeyPair

      KeyPair generateKeyPair()
      Generate a new key pair for this cipher 为此加密器生成新的密钥对
      Returns:
      generated key pair
      Throws:
      OpenKeyException - if key generation fails