Class SealedBox
java.lang.Object
cloud.opencode.base.crypto.sealedbox.SealedBox
Sealed Box - Anonymous public-key encryption (NaCl/Libsodium style)
密封盒 - 匿名公钥加密(NaCl/Libsodium 风格)
Provides anonymous public-key encryption where the sender's identity is not revealed to the recipient. This is achieved by generating an ephemeral key pair for each encryption operation.
提供匿名公钥加密,发送者的身份不会向接收者透露。 这是通过为每次加密操作生成临时密钥对来实现的。
Features | 主要功能:
- Anonymous encryption - sender identity not revealed - 匿名加密 - 不透露发送者身份
- Ephemeral key generation - 临时密钥生成
- X25519 key exchange + AES-GCM encryption - X25519 密钥交换 + AES-GCM 加密
- Authenticated encryption - 认证加密
- Simple API - 简单 API
Usage Examples | 使用示例:
// Generate recipient's key pair
KeyPair recipientKeyPair = SealedBox.generateKeyPair();
// Sender encrypts (only needs recipient's public key)
byte[] message = "Secret message".getBytes();
byte[] sealed = SealedBox.seal(message, recipientKeyPair.getPublic());
// Recipient decrypts (needs their private key)
byte[] opened = SealedBox.open(sealed, recipientKeyPair);
// With builder for custom configuration
SealedBox box = SealedBox.builder()
.algorithm(SealedBox.Algorithm.X25519_AES_GCM)
.build();
byte[] sealed = box.encrypt(message, recipientPublicKey);
byte[] opened = box.decrypt(sealed, recipientKeyPair);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-crypto V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumSupported algorithms for SealedBox.static final classBuilder for SealedBox. -
Method Summary
Modifier and TypeMethodDescriptionstatic SealedBox.Builderbuilder()Creates a new builder.byte[]Decrypts a sealed message.byte[]Encrypts a message for a recipient.static KeyPairGenerates a key pair suitable for SealedBox.static byte[]Opens (decrypts) a sealed message.static StringopenAsString(byte[] sealed, KeyPair recipientKeyPair) Opens (decrypts) a sealed message and returns as string.static byte[]Seals (encrypts) a message for a recipient.static byte[]Seals (encrypts) a string message for a recipient.
-
Method Details
-
seal
Seals (encrypts) a message for a recipient. 为接收者密封(加密)消息。- Parameters:
plaintext- the message to encrypt - 要加密的消息recipientPublicKey- the recipient's public key - 接收者的公钥- Returns:
- the sealed message - 密封的消息
-
seal
-
open
Opens (decrypts) a sealed message. 打开(解密)密封的消息。- Parameters:
sealed- the sealed message - 密封的消息recipientKeyPair- the recipient's key pair - 接收者的密钥对- Returns:
- the decrypted message - 解密的消息
-
openAsString
Opens (decrypts) a sealed message and returns as string. 打开(解密)密封的消息并返回字符串。- Parameters:
sealed- the sealed message - 密封的消息recipientKeyPair- the recipient's key pair - 接收者的密钥对- Returns:
- the decrypted message as string - 解密的消息字符串
-
generateKeyPair
Generates a key pair suitable for SealedBox. 生成适用于 SealedBox 的密钥对。- Returns:
- the key pair - 密钥对
-
builder
Creates a new builder. 创建新的构建器。- Returns:
- the builder - 构建器
-
encrypt
Encrypts a message for a recipient. 为接收者加密消息。- Parameters:
plaintext- the message to encrypt - 要加密的消息recipientPublicKey- the recipient's public key - 接收者的公钥- Returns:
- the encrypted message with ephemeral public key - 带有临时公钥的加密消息
-
decrypt
Decrypts a sealed message. 解密密封的消息。- Parameters:
sealed- the sealed message - 密封的消息recipientKeyPair- the recipient's key pair - 接收者的密钥对- Returns:
- the decrypted message - 解密的消息
-