pub fn encrypt(
    key_id: Id,
    alg: AsymmetricEncryption,
    plaintext: &[u8],
    salt: Option<&[u8]>,
    ciphertext: &mut [u8],
) -> Result<usize> {
    initialized()?;

    let mut output_length = 0;
    let (salt_ptr, salt_len) = match salt {
        Some(salt) => (salt.as_ptr(), salt.len()),
        None => (core::ptr::null(), 0),
    };

    Status::from(unsafe {
        psa_crypto_sys::psa_asymmetric_encrypt(
            key_id.0,
            alg.into(),
            plaintext.as_ptr(),
            plaintext.len(),
            salt_ptr,
            salt_len,
            ciphertext.as_mut_ptr(),
            ciphertext.len(),
            &mut output_length,
        )
    })
    .to_result()?;
    Ok(output_length)
}
/// Encrypt a short message with a key pair or public key
///
/// The encrypted message is written in `ciphertext`. The function returns the number of bytes written.
///
/// # Example
///
/// ```
/// # use psa_crypto::operations::key_management::generate;
/// # use psa_crypto::operations::asym_encryption::encrypt;
/// # use psa_crypto::types::key::{Attributes, Type, Lifetime, Policy, UsageFlags};
/// # use psa_crypto::types::algorithm::{AsymmetricEncryption, Hash};
/// #
/// # let mut usage_flags: UsageFlags = Default::default();
/// # usage_flags.set_encrypt();
/// # let mut attributes = Attributes {
/// #     key_type: Type::RsaKeyPair,
/// #     bits: 1024,
/// #     lifetime: Lifetime::Volatile,
/// #     policy: Policy {
/// #         usage_flags,
/// #         permitted_algorithms: AsymmetricEncryption::RsaPkcs1v15Crypt.into(),
/// #     },
/// # };
/// # const MESSAGE: [u8; 32] = [
/// #     0x69, 0x3E, 0xDB, 0x1B, 0x22, 0x79, 0x03, 0xF4, 0xC0, 0xBF, 0xD6, 0x91, 0x76, 0x37, 0x84, 0xA2,
/// #     0x94, 0x8E, 0x92, 0x50, 0x35, 0xC2, 0x8C, 0x5C, 0x3C, 0xCA, 0xFE, 0x18, 0xE8, 0x81, 0x37, 0x78,
/// # ];
/// psa_crypto::init().unwrap();
/// let my_key = generate(attributes, None).unwrap();
/// let alg = AsymmetricEncryption::RsaPkcs1v15Crypt;
/// let buffer_size = attributes.asymmetric_encrypt_output_size(alg).unwrap();
/// let mut encrypted_message = vec![0; buffer_size];
///
/// let size = encrypt(my_key,
///                      alg,
///                      &MESSAGE,
///                      None,
///                      &mut encrypted_message).unwrap();
/// encrypted_message.resize(size, 0);
/// ```
Encrypt a short message with a key pair or public key
The encrypted message is written in `ciphertext`. The function returns the number of bytes written.
Encrypt a short message with a key pair or public key The encrypted message is written in ` ciphertext ` . The function returns the number of bytes written .
pub fn encrypt(
    key_id: Id,
    alg: AsymmetricEncryption,
    plaintext: &[u8],
    salt: Option<&[u8]>,
    ciphertext: &mut [u8],
) -> Result<usize> {
    initialized()?;
    let mut output_length = 0;
    let (salt_ptr, salt_len) = match salt {
        Some(salt) => (salt.as_ptr(), salt.len()),
        None => (core::ptr::null(), 0),
    };
    Status::from(unsafe {
        psa_crypto_sys::psa_asymmetric_encrypt(
            key_id.0,
            alg.into(),
            plaintext.as_ptr(),
            plaintext.len(),
            salt_ptr,
            salt_len,
            ciphertext.as_mut_ptr(),
            ciphertext.len(),
            &mut output_length,
        )
    })
    .to_result()?;
    Ok(output_length)
}
pub fn encrypt ( key_id : Id , alg : AsymmetricEncryption , plaintext : & [ u8 ] , salt : Option < & [ u8 ] > , ciphertext : & mut [ u8 ] , ) -> Result < usize > { initialized ( ) ? ; let mut output_length = 0 ; let ( salt_ptr , salt_len ) = match salt { Some ( salt ) => ( salt . as_ptr ( ) , salt . len ( ) ) , None => ( core :: ptr :: null ( ) , 0 ) , } ; Status :: from ( unsafe { psa_crypto_sys :: psa_asymmetric_encrypt ( key_id . 0 , alg . into ( ) , plaintext . as_ptr ( ) , plaintext . len ( ) , salt_ptr , salt_len , ciphertext . as_mut_ptr ( ) , ciphertext . len ( ) , & mut output_length , ) } ) . to_result ( ) ? ; Ok ( output_length ) }
Encrypt a short message with a key pair or public key
The encrypted message is written in `ciphertext`.
Encrypt a short message with a key pair or public key The encrypted message is written in ` ciphertext ` .
key_id
Id
alg
AsymmetricEncryption
plaintext
&[u8]
salt
Option<&[u8]>
ciphertext
&mut [u8]
key_id
Id
alg
AsymmetricEncryption
plaintext
&[u8]
salt
Option<&[u8]>
ciphertext
&mut [u8]
example
None
use psa_crypto::operations::key_management::generate;
None
use psa_crypto::operations::asym_encryption::encrypt;
None
use psa_crypto::types::key::{attributes, type, lifetime, policy, usageflags};
None
use psa_crypto::types::algorithm::{asymmetricencryption, hash};
None
let mut usage_flags: usageflags = default::default();
None
usage_flags.set_encrypt();
None
let mut attributes = attributes {
None
key_type: type::rsakeypair,
None
bits: 1024,
None
lifetime: lifetime::volatile,
None
policy: policy {
None
usage_flags,
None
permitted_algorithms: asymmetricencryption::rsapkcs1v15crypt.into(),
None
},
None
};
None
const message: [u8; 32] = [
None
0x69, 0x3e, 0xdb, 0x1b, 0x22, 0x79, 0x03, 0xf4, 0xc0, 0xbf, 0xd6, 0x91, 0x76, 0x37, 0x84, 0xa2,
None
0x94, 0x8e, 0x92, 0x50, 0x35, 0xc2, 0x8c, 0x5c, 0x3c, 0xca, 0xfe, 0x18, 0xe8, 0x81, 0x37, 0x78,
None
];
:init().unwrap();
let my_key = generate(attributes, None).unwrap();
let alg = AsymmetricEncryption::RsaPkcs1v15Crypt;
let buffer_size = attributes.asymmetric_encrypt_output_size(alg).unwrap();
let mut encrypted_message = vec![0; buffer_size].

let size = encrypt(my_key,
alg,
&MESSAGE,
None,
&mut encrypted_message).unwrap();
encrypted_message.resize(size, 0);
```
: init () . unwrap () ; let my_key = generate ( attributes None ) . unwrap () ; let alg = AsymmetricEncryption :: RsaPkcs1v15Crypt ; let buffer_size = attributes . asymmetric_encrypt_output_size ( alg ) . unwrap () ; let mut encrypted_message = vec! [ 0 ; buffer_size ] . let size = encrypt ( my_key alg &MESSAGE None &mut encrypted_message ) . unwrap () ; encrypted_message . resize ( size 0 ) ; ` ` `