Class HybridCrypto

java.lang.Object
cloud.opencode.base.crypto.envelope.HybridCrypto

public final class HybridCrypto extends Object
Hybrid encryption implementation - Simplified envelope encryption with transparent format 混合加密实现 - 简化的信封加密,格式透明

Features | 主要功能:

  • Hybrid encryption combining asymmetric and symmetric - 结合非对称和对称的混合加密
  • Automatic key negotiation - 自动密钥协商

Usage Examples | 使用示例:

HybridCrypto hybrid = HybridCrypto.rsaAes();
byte[] encrypted = hybrid.encrypt(data, publicKey);
byte[] decrypted = hybrid.decrypt(encrypted, privateKey);

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 Details

    • rsaAes

      public static HybridCrypto rsaAes()
      Create hybrid crypto with RSA and AES (Recommended) 创建使用 RSA 和 AES 的混合加密(推荐)
      Returns:
      new HybridCrypto instance
    • ecdhAes

      public static HybridCrypto ecdhAes()
      Create hybrid crypto with ECDH and AES 创建使用 ECDH 和 AES 的混合加密
      Returns:
      new HybridCrypto instance
    • x25519ChaCha20

      public static HybridCrypto x25519ChaCha20()
      Create hybrid crypto with X25519 and ChaCha20 创建使用 X25519 和 ChaCha20 的混合加密
      Returns:
      new HybridCrypto instance
    • builder

      public static HybridCrypto.Builder builder()
      Create a new builder for custom configuration 创建用于自定义配置的构建器
      Returns:
      new Builder instance
    • setRecipientPublicKey

      public HybridCrypto setRecipientPublicKey(PublicKey publicKey)
      Set recipient public key for encryption 设置接收者公钥用于加密
      Parameters:
      publicKey - recipient's public key
      Returns:
      this instance for method chaining
      Throws:
      NullPointerException - if publicKey is null
    • setRecipientPrivateKey

      public HybridCrypto setRecipientPrivateKey(PrivateKey privateKey)
      Set recipient private key for decryption 设置接收者私钥用于解密
      Parameters:
      privateKey - recipient's private key
      Returns:
      this instance for method chaining
      Throws:
      NullPointerException - if privateKey is null
    • encrypt

      public byte[] encrypt(byte[] plaintext)
      Encrypt plaintext using hybrid encryption 使用混合加密加密明文

      The result is serialized in a compact binary format

      Parameters:
      plaintext - data to encrypt
      Returns:
      encrypted bytes (serialized envelope)
      Throws:
      NullPointerException - if plaintext is null
      IllegalStateException - if public key is not set
      OpenCryptoException - if encryption fails
    • encryptBase64

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

      public byte[] decrypt(byte[] ciphertext)
      Decrypt ciphertext using hybrid encryption 使用混合加密解密密文
      Parameters:
      ciphertext - encrypted bytes (serialized envelope)
      Returns:
      decrypted plaintext
      Throws:
      NullPointerException - if ciphertext is null
      IllegalStateException - if private key is not set
      OpenCryptoException - if decryption fails
    • decryptBase64

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

      public AsymmetricAlgorithm getAsymmetricAlgorithm()
      Get the asymmetric algorithm 获取非对称算法
      Returns:
      asymmetric algorithm
    • getSymmetricAlgorithm

      public SymmetricAlgorithm getSymmetricAlgorithm()
      Get the symmetric algorithm 获取对称算法
      Returns:
      symmetric algorithm