pub fn encrypt_short_buffer<R: RngCore + CryptoRng>(
rng: &mut R,
plaintext: &[u8],
public_key: &[u8],
) -> Result<Vec<u8>>Expand description
Encrypt a short buffer (like AES keys) to a public key.
This function encrypts small buffers (typically 32 bytes or less, like AES keys) directly to a public key without using ECDH key exchange. This is useful for key wrapping scenarios.
Currently supports RSA keys only. For RSA, the plaintext must be smaller than the key size minus 42 bytes (for OAEP with SHA-256 padding).
§Arguments
rng- A cryptographically secure random number generatorplaintext- The plaintext to encrypt (should be short, e.g., 32 bytes for AES-256 keys)public_key- The recipient’s public key (PKIX DER format or raw RSA public key bytes)
§Returns
Ok(Vec<u8>)- Encrypted ciphertextErr(BottleError::UnsupportedAlgorithm)- If the key type is not supportedErr(BottleError::Encryption)- If encryption fails
§Example
use rust_bottle::utils::encrypt_short_buffer;
use rust_bottle::keys::RsaKey;
use rust_bottle::ecdh::rsa_encrypt;
use rand::rngs::OsRng;
let rng = &mut OsRng;
let rsa_key = RsaKey::generate(rng, 2048).unwrap();
// Encrypt a 32-byte AES key
// Note: For now, use rsa_encrypt directly with RsaPublicKey
// PKIX parsing for RSA is not yet fully implemented
let aes_key = vec![0u8; 32];
let ciphertext = rsa_encrypt(rng, &aes_key, rsa_key.public_key()).unwrap();