Class OpenAsymmetric
java.lang.Object
cloud.opencode.base.crypto.OpenAsymmetric
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 Summary
Modifier and TypeMethodDescriptionbyte[]decrypt(byte[] ciphertext) Decrypt data with private key 使用私钥解密数据byte[]decryptBase64(String base64Ciphertext) Decrypt Base64-encoded ciphertext 解密 Base64 编码的密文decryptBase64ToString(String base64Ciphertext) Decrypt Base64-encoded ciphertext to string 解密 Base64 编码的密文为字符串byte[]decryptHex(String hexCiphertext) Decrypt hex-encoded ciphertext 解密十六进制编码的密文decryptHexToString(String hexCiphertext) Decrypt hex-encoded ciphertext to string 解密十六进制编码的密文为字符串decryptToString(byte[] ciphertext) Decrypt and return as string (UTF-8) 解密并返回字符串(UTF-8)static OpenAsymmetricecc()Create ECC cipher (ECIES) 创建 ECC 加密(ECIES)byte[]encrypt(byte[] plaintext) Encrypt data with public key 使用公钥加密数据byte[]Encrypt string (UTF-8) 加密字符串(UTF-8)encryptBase64(byte[] plaintext) Encrypt and return as Base64 string 加密并返回 Base64 字符串encryptBase64(String plaintext) Encrypt string and return as Base64 string 加密字符串并返回 Base64 字符串encryptHex(byte[] plaintext) Encrypt and return as hex string 加密并返回十六进制字符串encryptHex(String plaintext) Encrypt string and return as hex string 加密字符串并返回十六进制字符串Generate key pair for this algorithm 生成此算法的密钥对Get algorithm name 获取算法名称intGet maximum encrypt size in bytes 获取最大加密大小(字节)static OpenAsymmetricof(AsymmetricAlgorithm algorithm) Create cipher by algorithm enum 根据算法枚举创建加密器static OpenAsymmetricrsa()Create RSA-PKCS1 cipher 创建 RSA-PKCS1 加密static OpenAsymmetricrsaOaep()Create RSA-OAEP cipher (recommended) 创建 RSA-OAEP 加密(推荐)setKeyPair(KeyPair keyPair) Set key pair 设置密钥对setPrivateKey(PrivateKey privateKey) Set private key for decryption 设置解密私钥setPublicKey(PublicKey publicKey) Set public key for encryption 设置加密公钥static OpenAsymmetricsm2()Create SM2 cipher (requires Bouncy Castle) 创建 SM2 加密(需要 Bouncy Castle)Generate key pair and set it 生成密钥对并设置
-
Method Details
-
rsaOaep
Create RSA-OAEP cipher (recommended) 创建 RSA-OAEP 加密(推荐)- Returns:
- OpenAsymmetric instance
-
rsa
Create RSA-PKCS1 cipher 创建 RSA-PKCS1 加密- Returns:
- OpenAsymmetric instance
-
ecc
Create ECC cipher (ECIES) 创建 ECC 加密(ECIES)- Returns:
- OpenAsymmetric instance
-
sm2
Create SM2 cipher (requires Bouncy Castle) 创建 SM2 加密(需要 Bouncy Castle)- Returns:
- OpenAsymmetric instance
-
of
Create cipher by algorithm enum 根据算法枚举创建加密器- Parameters:
algorithm- asymmetric algorithm- Returns:
- OpenAsymmetric instance
-
setPrivateKey
Set private key for decryption 设置解密私钥- Parameters:
privateKey- private key- Returns:
- this instance for chaining
-
setPublicKey
Set public key for encryption 设置加密公钥- Parameters:
publicKey- public key- Returns:
- this instance for chaining
-
setKeyPair
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
Encrypt string (UTF-8) 加密字符串(UTF-8)- Parameters:
plaintext- plaintext string- Returns:
- ciphertext bytes
-
encryptHex
Encrypt and return as hex string 加密并返回十六进制字符串- Parameters:
plaintext- plaintext bytes- Returns:
- hex ciphertext
-
encryptHex
-
encryptBase64
Encrypt and return as Base64 string 加密并返回 Base64 字符串- Parameters:
plaintext- plaintext bytes- Returns:
- Base64 ciphertext
-
encryptBase64
-
decrypt
public byte[] decrypt(byte[] ciphertext) Decrypt data with private key 使用私钥解密数据- Parameters:
ciphertext- ciphertext bytes- Returns:
- plaintext bytes
-
decryptToString
Decrypt and return as string (UTF-8) 解密并返回字符串(UTF-8)- Parameters:
ciphertext- ciphertext bytes- Returns:
- plaintext string
-
decryptHex
Decrypt hex-encoded ciphertext 解密十六进制编码的密文- Parameters:
hexCiphertext- hex-encoded ciphertext- Returns:
- plaintext bytes
-
decryptHexToString
-
decryptBase64
Decrypt Base64-encoded ciphertext 解密 Base64 编码的密文- Parameters:
base64Ciphertext- Base64-encoded ciphertext- Returns:
- plaintext bytes
-
decryptBase64ToString
-
generateKeyPair
Generate key pair for this algorithm 生成此算法的密钥对- Returns:
- generated key pair
-
withGeneratedKeyPair
Generate key pair and set it 生成密钥对并设置- Returns:
- this instance for chaining
-
getAlgorithm
-
getMaxEncryptSize
public int getMaxEncryptSize()Get maximum encrypt size in bytes 获取最大加密大小(字节)- Returns:
- maximum plaintext size, or -1 if no limit
-