Class OpenAsymmetric

java.lang.Object
cloud.opencode.base.crypto.OpenAsymmetric

public final class OpenAsymmetric extends Object
Asymmetric encryption facade for encrypt/decrypt operations - Provides convenient API for various asymmetric algorithms 非对称加密门面类 - 为各种非对称加密算法提供便捷的 API

Features | 主要功能:

  • RSA-OAEP encryption (recommended) - RSA-OAEP 加密(推荐)
  • RSA-PKCS1 encryption - RSA-PKCS1 加密
  • SM2 encryption (Chinese national standard) - SM2 加密(中国国密标准)
  • Hex and Base64 output encoding - 十六进制和 Base64 输出编码
  • Key pair generation - 密钥对生成

Usage Examples | 使用示例:

OpenAsymmetric rsa = OpenAsymmetric.rsaOaep();
KeyPair keyPair = rsa.generateKeyPair();
rsa.setKeyPair(keyPair);
String encrypted = rsa.encryptBase64("secret");
String decrypted = rsa.decryptBase64ToString(encrypted);

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • rsaOaep

      public static OpenAsymmetric rsaOaep()
      Create RSA-OAEP cipher (recommended) 创建 RSA-OAEP 加密(推荐)
      Returns:
      OpenAsymmetric instance
    • rsa

      public static OpenAsymmetric rsa()
      Create RSA-PKCS1 cipher 创建 RSA-PKCS1 加密
      Returns:
      OpenAsymmetric instance
    • ecc

      public static OpenAsymmetric ecc()
      Create ECC cipher (ECIES) 创建 ECC 加密(ECIES)
      Returns:
      OpenAsymmetric instance
    • sm2

      public static OpenAsymmetric sm2()
      Create SM2 cipher (requires Bouncy Castle) 创建 SM2 加密(需要 Bouncy Castle)
      Returns:
      OpenAsymmetric instance
    • of

      public static OpenAsymmetric of(AsymmetricAlgorithm algorithm)
      Create cipher by algorithm enum 根据算法枚举创建加密器
      Parameters:
      algorithm - asymmetric algorithm
      Returns:
      OpenAsymmetric instance
    • setPrivateKey

      public OpenAsymmetric setPrivateKey(PrivateKey privateKey)
      Set private key for decryption 设置解密私钥
      Parameters:
      privateKey - private key
      Returns:
      this instance for chaining
    • setPublicKey

      public OpenAsymmetric setPublicKey(PublicKey publicKey)
      Set public key for encryption 设置加密公钥
      Parameters:
      publicKey - public key
      Returns:
      this instance for chaining
    • setKeyPair

      public OpenAsymmetric setKeyPair(KeyPair keyPair)
      Set key pair 设置密钥对
      Parameters:
      keyPair - key pair
      Returns:
      this instance for chaining
    • encrypt

      public byte[] encrypt(byte[] plaintext)
      Encrypt data with public key 使用公钥加密数据
      Parameters:
      plaintext - plaintext bytes
      Returns:
      ciphertext bytes
    • encrypt

      public byte[] encrypt(String plaintext)
      Encrypt string (UTF-8) 加密字符串(UTF-8)
      Parameters:
      plaintext - plaintext string
      Returns:
      ciphertext bytes
    • encryptHex

      public String encryptHex(byte[] plaintext)
      Encrypt and return as hex string 加密并返回十六进制字符串
      Parameters:
      plaintext - plaintext bytes
      Returns:
      hex ciphertext
    • encryptHex

      public String encryptHex(String plaintext)
      Encrypt string and return as hex string 加密字符串并返回十六进制字符串
      Parameters:
      plaintext - plaintext string
      Returns:
      hex ciphertext
    • encryptBase64

      public String encryptBase64(byte[] plaintext)
      Encrypt and return as Base64 string 加密并返回 Base64 字符串
      Parameters:
      plaintext - plaintext bytes
      Returns:
      Base64 ciphertext
    • encryptBase64

      public String encryptBase64(String plaintext)
      Encrypt string and return as Base64 string 加密字符串并返回 Base64 字符串
      Parameters:
      plaintext - plaintext string
      Returns:
      Base64 ciphertext
    • decrypt

      public byte[] decrypt(byte[] ciphertext)
      Decrypt data with private key 使用私钥解密数据
      Parameters:
      ciphertext - ciphertext bytes
      Returns:
      plaintext bytes
    • decryptToString

      public String decryptToString(byte[] ciphertext)
      Decrypt and return as string (UTF-8) 解密并返回字符串(UTF-8)
      Parameters:
      ciphertext - ciphertext bytes
      Returns:
      plaintext string
    • decryptHex

      public byte[] decryptHex(String hexCiphertext)
      Decrypt hex-encoded ciphertext 解密十六进制编码的密文
      Parameters:
      hexCiphertext - hex-encoded ciphertext
      Returns:
      plaintext bytes
    • decryptHexToString

      public String decryptHexToString(String hexCiphertext)
      Decrypt hex-encoded ciphertext to string 解密十六进制编码的密文为字符串
      Parameters:
      hexCiphertext - hex-encoded ciphertext
      Returns:
      plaintext string
    • decryptBase64

      public byte[] decryptBase64(String base64Ciphertext)
      Decrypt Base64-encoded ciphertext 解密 Base64 编码的密文
      Parameters:
      base64Ciphertext - Base64-encoded ciphertext
      Returns:
      plaintext bytes
    • decryptBase64ToString

      public String decryptBase64ToString(String base64Ciphertext)
      Decrypt Base64-encoded ciphertext to string 解密 Base64 编码的密文为字符串
      Parameters:
      base64Ciphertext - Base64-encoded ciphertext
      Returns:
      plaintext string
    • generateKeyPair

      public KeyPair generateKeyPair()
      Generate key pair for this algorithm 生成此算法的密钥对
      Returns:
      generated key pair
    • withGeneratedKeyPair

      public OpenAsymmetric withGeneratedKeyPair()
      Generate key pair and set it 生成密钥对并设置
      Returns:
      this instance for chaining
    • getAlgorithm

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

      public int getMaxEncryptSize()
      Get maximum encrypt size in bytes 获取最大加密大小(字节)
      Returns:
      maximum plaintext size, or -1 if no limit