Class RsaCipher

java.lang.Object
cloud.opencode.base.crypto.asymmetric.RsaCipher
All Implemented Interfaces:
AsymmetricCipher

public final class RsaCipher extends Object implements AsymmetricCipher
RSA cipher implementation with PKCS1 padding - Legacy RSA encryption RSA 密码实现(PKCS1 填充)- 传统 RSA 加密

Note: RSA with PKCS1 padding is considered legacy. For new applications, use RsaOaepCipher which provides better security. 注意:使用 PKCS1 填充的 RSA 被认为是传统方式。对于新应用, 建议使用 RsaOaepCipher,它提供更好的安全性。

Features | 主要功能:

  • RSA-PKCS1 encryption/decryption - RSA-PKCS1 加密/解密
  • Key pair generation - 密钥对生成

Usage Examples | 使用示例:

RsaCipher rsa = RsaCipher.create();
rsa.setPublicKey(publicKey);
byte[] encrypted = rsa.encrypt(data);

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(k^2) for encrypt/decrypt (k=key bits) - 时间复杂度: O(k^2),k为密钥位数
  • Space complexity: O(k) - 空间复杂度: O(k)
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static RsaCipher create()
      Create a new RSA cipher instance 创建新的 RSA 密码实例
      Returns:
      new RSA cipher instance
    • rsa2048

      public static RsaCipher rsa2048()
      Create RSA cipher with generated 2048-bit key pair 创建带 2048 位密钥对的 RSA 密码
      Returns:
      RSA cipher with generated keys
    • rsa4096

      public static RsaCipher rsa4096()
      Create RSA cipher with generated 4096-bit key pair 创建带 4096 位密钥对的 RSA 密码
      Returns:
      RSA cipher with generated keys
    • withGeneratedKeyPair

      public static RsaCipher withGeneratedKeyPair(int keySize)
      Create RSA cipher with generated key pair of specified size 创建带指定大小密钥对的 RSA 密码
      Parameters:
      keySize - the key size in bits (minimum 2048 recommended)
      Returns:
      RSA cipher with generated keys
      Throws:
      IllegalArgumentException - if keySize is invalid
    • setPublicKey

      public AsymmetricCipher setPublicKey(PublicKey publicKey)
      Description copied from interface: AsymmetricCipher
      Set public key for encryption operations 设置用于加密操作的公钥
      Specified by:
      setPublicKey in interface AsymmetricCipher
      Parameters:
      publicKey - the public key
      Returns:
      this cipher instance for method chaining
    • setPublicKey

      public AsymmetricCipher setPublicKey(byte[] encodedKey)
      Description copied from interface: AsymmetricCipher
      Set public key from encoded byte array 从编码的字节数组设置公钥
      Specified by:
      setPublicKey in interface AsymmetricCipher
      Parameters:
      encodedKey - the encoded public key bytes
      Returns:
      this cipher instance for method chaining
    • setPublicKeyPem

      public AsymmetricCipher setPublicKeyPem(String pem)
      Description copied from interface: AsymmetricCipher
      Set public key from PEM formatted string 从 PEM 格式字符串设置公钥
      Specified by:
      setPublicKeyPem in interface AsymmetricCipher
      Parameters:
      pem - the PEM formatted public key
      Returns:
      this cipher instance for method chaining
    • setPrivateKey

      public AsymmetricCipher setPrivateKey(PrivateKey privateKey)
      Description copied from interface: AsymmetricCipher
      Set private key for decryption operations 设置用于解密操作的私钥
      Specified by:
      setPrivateKey in interface AsymmetricCipher
      Parameters:
      privateKey - the private key
      Returns:
      this cipher instance for method chaining
    • setPrivateKey

      public AsymmetricCipher setPrivateKey(byte[] encodedKey)
      Description copied from interface: AsymmetricCipher
      Set private key from encoded byte array 从编码的字节数组设置私钥
      Specified by:
      setPrivateKey in interface AsymmetricCipher
      Parameters:
      encodedKey - the encoded private key bytes
      Returns:
      this cipher instance for method chaining
    • setPrivateKeyPem

      public AsymmetricCipher setPrivateKeyPem(String pem)
      Description copied from interface: AsymmetricCipher
      Set private key from PEM formatted string 从 PEM 格式字符串设置私钥
      Specified by:
      setPrivateKeyPem in interface AsymmetricCipher
      Parameters:
      pem - the PEM formatted private key
      Returns:
      this cipher instance for method chaining
    • setKeyPair

      public AsymmetricCipher setKeyPair(KeyPair keyPair)
      Description copied from interface: AsymmetricCipher
      Set both public and private keys from key pair 从密钥对设置公钥和私钥
      Specified by:
      setKeyPair in interface AsymmetricCipher
      Parameters:
      keyPair - the key pair containing public and private keys
      Returns:
      this cipher instance for method chaining
    • encrypt

      public byte[] encrypt(byte[] plaintext)
      Description copied from interface: AsymmetricCipher
      Encrypt data using public key 使用公钥加密数据
      Specified by:
      encrypt in interface AsymmetricCipher
      Parameters:
      plaintext - the data to encrypt
      Returns:
      encrypted bytes
    • encrypt

      public byte[] encrypt(String plaintext)
      Description copied from interface: AsymmetricCipher
      Encrypt string using public key 使用公钥加密字符串
      Specified by:
      encrypt in interface AsymmetricCipher
      Parameters:
      plaintext - the string to encrypt
      Returns:
      encrypted bytes
    • encryptBase64

      public String encryptBase64(byte[] plaintext)
      Description copied from interface: AsymmetricCipher
      Encrypt data and return Base64 encoded result 加密数据并返回 Base64 编码结果
      Specified by:
      encryptBase64 in interface AsymmetricCipher
      Parameters:
      plaintext - the data to encrypt
      Returns:
      Base64 encoded ciphertext
    • encryptHex

      public String encryptHex(byte[] plaintext)
      Description copied from interface: AsymmetricCipher
      Encrypt data and return hexadecimal encoded result 加密数据并返回十六进制编码结果
      Specified by:
      encryptHex in interface AsymmetricCipher
      Parameters:
      plaintext - the data to encrypt
      Returns:
      hexadecimal encoded ciphertext
    • decrypt

      public byte[] decrypt(byte[] ciphertext)
      Description copied from interface: AsymmetricCipher
      Decrypt data using private key 使用私钥解密数据
      Specified by:
      decrypt in interface AsymmetricCipher
      Parameters:
      ciphertext - the encrypted data
      Returns:
      decrypted bytes
    • decryptToString

      public String decryptToString(byte[] ciphertext)
      Description copied from interface: AsymmetricCipher
      Decrypt data and return as string 解密数据并返回字符串
      Specified by:
      decryptToString in interface AsymmetricCipher
      Parameters:
      ciphertext - the encrypted data
      Returns:
      decrypted string
    • decryptBase64

      public byte[] decryptBase64(String base64Ciphertext)
      Description copied from interface: AsymmetricCipher
      Decrypt Base64 encoded ciphertext 解密 Base64 编码的密文
      Specified by:
      decryptBase64 in interface AsymmetricCipher
      Parameters:
      base64Ciphertext - the Base64 encoded ciphertext
      Returns:
      decrypted bytes
    • decryptHex

      public byte[] decryptHex(String hexCiphertext)
      Description copied from interface: AsymmetricCipher
      Decrypt hexadecimal encoded ciphertext 解密十六进制编码的密文
      Specified by:
      decryptHex in interface AsymmetricCipher
      Parameters:
      hexCiphertext - the hexadecimal encoded ciphertext
      Returns:
      decrypted bytes
    • getAlgorithm

      public String getAlgorithm()
      Description copied from interface: AsymmetricCipher
      Get the algorithm name 获取算法名称
      Specified by:
      getAlgorithm in interface AsymmetricCipher
      Returns:
      the algorithm name
    • getMaxEncryptSize

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

      public KeyPair generateKeyPair()
      Description copied from interface: AsymmetricCipher
      Generate a new key pair for this cipher 为此加密器生成新的密钥对
      Specified by:
      generateKeyPair in interface AsymmetricCipher
      Returns:
      generated key pair
    • getPublicKey

      public PublicKey getPublicKey()
      Get the public key 获取公钥
      Returns:
      the public key, or null if not set
    • getPrivateKey

      public PrivateKey getPrivateKey()
      Get the private key 获取私钥
      Returns:
      the private key, or null if not set