Class RsaOaepCipher
java.lang.Object
cloud.opencode.base.crypto.asymmetric.RsaOaepCipher
- All Implemented Interfaces:
AsymmetricCipher
RSA cipher implementation with OAEP padding - Recommended RSA encryption
RSA 密码实现(OAEP 填充)- 推荐的 RSA 加密方式
OAEP (Optimal Asymmetric Encryption Padding) provides better security than PKCS1. This implementation is recommended for all new applications. OAEP(最优非对称加密填充)比 PKCS1 提供更好的安全性。 建议所有新应用使用此实现。
Features | 主要功能:
- RSA-OAEP with SHA-256/384/512 - RSA-OAEP(SHA-256/384/512)
- Recommended for new applications - 推荐用于新应用
Usage Examples | 使用示例:
RsaOaepCipher oaep = RsaOaepCipher.sha256();
oaep.setPublicKey(publicKey);
byte[] encrypted = oaep.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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for RSA-OAEP cipher RSA-OAEP 密码构建器 -
Method Summary
Modifier and TypeMethodDescriptionstatic RsaOaepCipher.Builderbuilder()Create a builder for customizing RSA-OAEP parameters 创建用于自定义 RSA-OAEP 参数的构建器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 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 获取算法名称intGet maximum size of data that can be encrypted in a single operation 获取单次操作可加密的最大数据大小Get the private key 获取私钥Get the public key 获取公钥setKeyPair(KeyPair keyPair) Set both public and private keys from key pair 从密钥对设置公钥和私钥setPrivateKey(byte[] encodedKey) Set private key from encoded byte array 从编码的字节数组设置私钥setPrivateKey(PrivateKey privateKey) Set private key for decryption operations 设置用于解密操作的私钥setPrivateKeyPem(String pem) Set private key from PEM formatted string 从 PEM 格式字符串设置私钥setPublicKey(byte[] encodedKey) Set public key from encoded byte array 从编码的字节数组设置公钥setPublicKey(PublicKey publicKey) Set public key for encryption operations 设置用于加密操作的公钥setPublicKeyPem(String pem) Set public key from PEM formatted string 从 PEM 格式字符串设置公钥static RsaOaepCiphersha256()Create RSA-OAEP cipher with SHA-256 digest (recommended) 创建使用 SHA-256 摘要的 RSA-OAEP 密码(推荐)static RsaOaepCiphersha384()Create RSA-OAEP cipher with SHA-384 digest 创建使用 SHA-384 摘要的 RSA-OAEP 密码static RsaOaepCiphersha512()Create RSA-OAEP cipher with SHA-512 digest 创建使用 SHA-512 摘要的 RSA-OAEP 密码static RsaOaepCipherwithGeneratedKeyPair(int keySize) Create RSA-OAEP cipher with generated key pair 创建带生成密钥对的 RSA-OAEP 密码
-
Method Details
-
sha256
Create RSA-OAEP cipher with SHA-256 digest (recommended) 创建使用 SHA-256 摘要的 RSA-OAEP 密码(推荐)- Returns:
- new RSA-OAEP cipher with SHA-256
-
sha384
Create RSA-OAEP cipher with SHA-384 digest 创建使用 SHA-384 摘要的 RSA-OAEP 密码- Returns:
- new RSA-OAEP cipher with SHA-384
-
sha512
Create RSA-OAEP cipher with SHA-512 digest 创建使用 SHA-512 摘要的 RSA-OAEP 密码- Returns:
- new RSA-OAEP cipher with SHA-512
-
withGeneratedKeyPair
Create RSA-OAEP cipher with generated key pair 创建带生成密钥对的 RSA-OAEP 密码- Parameters:
keySize- the key size in bits (minimum 2048 recommended)- Returns:
- RSA-OAEP cipher with generated keys
- Throws:
IllegalArgumentException- if keySize is invalid
-
builder
Create a builder for customizing RSA-OAEP parameters 创建用于自定义 RSA-OAEP 参数的构建器- Returns:
- new builder instance
-
setPublicKey
Description copied from interface:AsymmetricCipherSet public key for encryption operations 设置用于加密操作的公钥- Specified by:
setPublicKeyin interfaceAsymmetricCipher- Parameters:
publicKey- the public key- Returns:
- this cipher instance for method chaining
-
setPublicKey
Description copied from interface:AsymmetricCipherSet public key from encoded byte array 从编码的字节数组设置公钥- Specified by:
setPublicKeyin interfaceAsymmetricCipher- Parameters:
encodedKey- the encoded public key bytes- Returns:
- this cipher instance for method chaining
-
setPublicKeyPem
Description copied from interface:AsymmetricCipherSet public key from PEM formatted string 从 PEM 格式字符串设置公钥- Specified by:
setPublicKeyPemin interfaceAsymmetricCipher- Parameters:
pem- the PEM formatted public key- Returns:
- this cipher instance for method chaining
-
setPrivateKey
Description copied from interface:AsymmetricCipherSet private key for decryption operations 设置用于解密操作的私钥- Specified by:
setPrivateKeyin interfaceAsymmetricCipher- Parameters:
privateKey- the private key- Returns:
- this cipher instance for method chaining
-
setPrivateKey
Description copied from interface:AsymmetricCipherSet private key from encoded byte array 从编码的字节数组设置私钥- Specified by:
setPrivateKeyin interfaceAsymmetricCipher- Parameters:
encodedKey- the encoded private key bytes- Returns:
- this cipher instance for method chaining
-
setPrivateKeyPem
Description copied from interface:AsymmetricCipherSet private key from PEM formatted string 从 PEM 格式字符串设置私钥- Specified by:
setPrivateKeyPemin interfaceAsymmetricCipher- Parameters:
pem- the PEM formatted private key- Returns:
- this cipher instance for method chaining
-
setKeyPair
Description copied from interface:AsymmetricCipherSet both public and private keys from key pair 从密钥对设置公钥和私钥- Specified by:
setKeyPairin interfaceAsymmetricCipher- 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:AsymmetricCipherEncrypt data using public key 使用公钥加密数据- Specified by:
encryptin interfaceAsymmetricCipher- Parameters:
plaintext- the data to encrypt- Returns:
- encrypted bytes
-
encrypt
Description copied from interface:AsymmetricCipherEncrypt string using public key 使用公钥加密字符串- Specified by:
encryptin interfaceAsymmetricCipher- Parameters:
plaintext- the string to encrypt- Returns:
- encrypted bytes
-
encryptBase64
Description copied from interface:AsymmetricCipherEncrypt data and return Base64 encoded result 加密数据并返回 Base64 编码结果- Specified by:
encryptBase64in interfaceAsymmetricCipher- Parameters:
plaintext- the data to encrypt- Returns:
- Base64 encoded ciphertext
-
encryptHex
Description copied from interface:AsymmetricCipherEncrypt data and return hexadecimal encoded result 加密数据并返回十六进制编码结果- Specified by:
encryptHexin interfaceAsymmetricCipher- Parameters:
plaintext- the data to encrypt- Returns:
- hexadecimal encoded ciphertext
-
decrypt
public byte[] decrypt(byte[] ciphertext) Description copied from interface:AsymmetricCipherDecrypt data using private key 使用私钥解密数据- Specified by:
decryptin interfaceAsymmetricCipher- Parameters:
ciphertext- the encrypted data- Returns:
- decrypted bytes
-
decryptToString
Description copied from interface:AsymmetricCipherDecrypt data and return as string 解密数据并返回字符串- Specified by:
decryptToStringin interfaceAsymmetricCipher- Parameters:
ciphertext- the encrypted data- Returns:
- decrypted string
-
decryptBase64
Description copied from interface:AsymmetricCipherDecrypt Base64 encoded ciphertext 解密 Base64 编码的密文- Specified by:
decryptBase64in interfaceAsymmetricCipher- Parameters:
base64Ciphertext- the Base64 encoded ciphertext- Returns:
- decrypted bytes
-
decryptHex
Description copied from interface:AsymmetricCipherDecrypt hexadecimal encoded ciphertext 解密十六进制编码的密文- Specified by:
decryptHexin interfaceAsymmetricCipher- Parameters:
hexCiphertext- the hexadecimal encoded ciphertext- Returns:
- decrypted bytes
-
getAlgorithm
Description copied from interface:AsymmetricCipherGet the algorithm name 获取算法名称- Specified by:
getAlgorithmin interfaceAsymmetricCipher- Returns:
- the algorithm name
-
getMaxEncryptSize
public int getMaxEncryptSize()Description copied from interface:AsymmetricCipherGet maximum size of data that can be encrypted in a single operation 获取单次操作可加密的最大数据大小- Specified by:
getMaxEncryptSizein interfaceAsymmetricCipher- Returns:
- maximum encrypt size in bytes, or -1 if not applicable
-
generateKeyPair
Description copied from interface:AsymmetricCipherGenerate a new key pair for this cipher 为此加密器生成新的密钥对- Specified by:
generateKeyPairin interfaceAsymmetricCipher- Returns:
- generated key pair
-
getPublicKey
-
getPrivateKey
Get the private key 获取私钥- Returns:
- the private key, or null if not set
-