encrypt

Function encrypt 

Source
pub fn encrypt(public_key: &str, data: &[u8]) -> Result<Vec<u8>, Box<dyn Error>>
Expand description

Encrypts data using a hybrid RSA + AES-GCM approach.

Takes a PEM-encoded RSA public key and plaintext data, then:

  1. Generates a random AES-256 key
  2. Encrypts the data with the AES key
  3. Encrypts the AES key with the RSA public key
  4. Returns a serialized structure containing everything needed for decryption

§Arguments

  • public_key - A PEM-encoded RSA public key
  • data - The plaintext data to encrypt

§Returns

A serialized EncryptedMessage containing the encrypted data

§Errors

Returns an error if:

  • The public key is invalid or cannot be parsed
  • The encryption process fails
  • Serialization fails