pub fn encrypt(&self, iv: Option<&[u8]>, data: &[u8]) -> Result<Buffer> {
        let mut iv_copy = iv.map(|iv| Buffer::from(iv));
        let iv_ptr = iv_copy.as_mut().map_or(null_mut(), |iv| iv.as_mut_ptr());
        let iv_len = iv_copy.as_ref().map_or(0, |iv| iv.len() as ULONG);

        let mut encrypted_len = MaybeUninit::<ULONG>::uninit();
        unsafe {
            Error::check(BCryptEncrypt(
                self.handle.as_ptr(),
                data.as_ptr() as PUCHAR,
                data.len() as ULONG,
                null_mut(),
                iv_ptr,
                iv_len,
                null_mut(),
                0,
                encrypted_len.as_mut_ptr(),
                BCRYPT_BLOCK_PADDING,
            ))?;

            let mut output = Buffer::new(encrypted_len.assume_init() as usize);

            Error::check(BCryptEncrypt(
                self.handle.as_ptr(),
                data.as_ptr() as PUCHAR,
                data.len() as ULONG,
                null_mut(),
                iv_ptr,
                iv_len,
                output.as_mut_ptr(),
                output.len() as ULONG,
                encrypted_len.as_mut_ptr(),
                BCRYPT_BLOCK_PADDING,
            ))
            .map(|_| output)
        }
    }
/// Encrypts data using the symmetric key
///
/// The IV is not needed for [`ChainingMode::Ecb`], so `None` should be used
/// in this case.
///
/// For chaining modes needing an IV, `None` can be used and a default IV will be used.
/// This is not documented on Microsoft's website and is therefore not recommended.
///
/// The data is padded with zeroes to a multiple of the block size of the cipher. If
/// the data length equals the block size of the cipher, one additional block of
/// padding is appended to the data.
///
/// # Examples
///
/// ```
/// # use win_crypto_ng::symmetric::{ChainingMode, SymmetricAlgorithm, SymmetricAlgorithmId};
/// # let algo = SymmetricAlgorithm::open(SymmetricAlgorithmId::Aes, ChainingMode::Cbc).unwrap();
/// let key = algo.new_key("0123456789ABCDEF".as_bytes()).unwrap();
/// let iv = "_THIS_IS_THE_IV_".as_bytes();
/// let plaintext = "THIS_IS_THE_DATA".as_bytes();
/// let ciphertext = key.encrypt(Some(iv), plaintext).unwrap();
///
/// assert_eq!(ciphertext.as_slice(), [
///     0xE4, 0xD9, 0x90, 0x64, 0xA6, 0xA6, 0x5F, 0x7E,
///     0x70, 0xDB, 0xF9, 0xDD, 0xE7, 0x0D, 0x6F, 0x6A,
///     0x0C, 0xEC, 0xDB, 0xAD, 0x01, 0xB4, 0xB1, 0xDE,
///     0xB4, 0x4A, 0xB8, 0xA0, 0xEA, 0x0E, 0x8F, 0x31]);
/// ```
Encrypts data using the symmetric key
The IV is not needed for [`ChainingMode::Ecb`], so `None` should be used
in this case.

For chaining modes needing an IV, `None` can be used and a default IV will be used.
This is not documented on Microsoft's website and is therefore not recommended.

The data is padded with zeroes to a multiple of the block size of the cipher. If
the data length equals the block size of the cipher, one additional block of
padding is appended to the data.
Encrypts data using the symmetric key The IV is not needed for [ ` ChainingMode :: Ecb ` ] so ` None ` should be used in this case . For chaining modes needing an IV ` None ` can be used and a default IV will be used . This is not documented on Microsoft ' s website and is therefore not recommended . The data is padded with zeroes to a multiple of the block size of the cipher . If the data length equals the block size of the cipher one additional block of padding is appended to the data .
pub fn encrypt(&self, iv: Option<&[u8]>, data: &[u8]) -> Result<Buffer> {
        let mut iv_copy = iv.map(|iv| Buffer::from(iv));
        let iv_ptr = iv_copy.as_mut().map_or(null_mut(), |iv| iv.as_mut_ptr());
        let iv_len = iv_copy.as_ref().map_or(0, |iv| iv.len() as ULONG);
        let mut encrypted_len = MaybeUninit::<ULONG>::uninit();
        unsafe {
            Error::check(BCryptEncrypt(
                self.handle.as_ptr(),
                data.as_ptr() as PUCHAR,
                data.len() as ULONG,
                null_mut(),
                iv_ptr,
                iv_len,
                null_mut(),
                0,
                encrypted_len.as_mut_ptr(),
                BCRYPT_BLOCK_PADDING,
            ))?;
            let mut output = Buffer::new(encrypted_len.assume_init() as usize);
            Error::check(BCryptEncrypt(
                self.handle.as_ptr(),
                data.as_ptr() as PUCHAR,
                data.len() as ULONG,
                null_mut(),
                iv_ptr,
                iv_len,
                output.as_mut_ptr(),
                output.len() as ULONG,
                encrypted_len.as_mut_ptr(),
                BCRYPT_BLOCK_PADDING,
            ))
            .map(|_| output)
        }
    }
pub fn encrypt ( & self , iv : Option < & [ u8 ] > , data : & [ u8 ] ) -> Result < Buffer > { let mut iv_copy = iv . map ( | iv | Buffer :: from ( iv ) ) ; let iv_ptr = iv_copy . as_mut ( ) . map_or ( null_mut ( ) , | iv | iv . as_mut_ptr ( ) ) ; let iv_len = iv_copy . as_ref ( ) . map_or ( 0 , | iv | iv . len ( ) as ULONG ) ; let mut encrypted_len = MaybeUninit :: < ULONG > :: uninit ( ) ; unsafe { Error :: check ( BCryptEncrypt ( self . handle . as_ptr ( ) , data . as_ptr ( ) as PUCHAR , data . len ( ) as ULONG , null_mut ( ) , iv_ptr , iv_len , null_mut ( ) , 0 , encrypted_len . as_mut_ptr ( ) , BCRYPT_BLOCK_PADDING , ) ) ? ; let mut output = Buffer :: new ( encrypted_len . assume_init ( ) as usize ) ; Error :: check ( BCryptEncrypt ( self . handle . as_ptr ( ) , data . as_ptr ( ) as PUCHAR , data . len ( ) as ULONG , null_mut ( ) , iv_ptr , iv_len , output . as_mut_ptr ( ) , output . len ( ) as ULONG , encrypted_len . as_mut_ptr ( ) , BCRYPT_BLOCK_PADDING , ) ) . map ( | _ | output ) } }
Encrypts data using the symmetric key
The IV is not needed for [`ChainingMode::Ecb`], so `None` should be used
in this case.
Encrypts data using the symmetric key The IV is not needed for [ ` ChainingMode :: Ecb ` ] so ` None ` should be used in this case .
&self
iv
Option<&[u8]>
data
&[u8]
&self
iv
Option<&[u8]>
data
&[u8]
examples
None
use win_crypto_ng::symmetric::{chainingmode, symmetricalgorithm, symmetricalgorithmid};
None
let algo = symmetricalgorithm::open(symmetricalgorithmid::aes, chainingmode::cbc).unwrap();
let key = algo.new_key("0123456789ABCDEF".as_bytes()).unwrap();
let iv = "_THIS_IS_THE_IV_".as_bytes();
let plaintext = "THIS_IS_THE_DATA".as_bytes();
let ciphertext = key.encrypt(Some(iv), plaintext).unwrap().


let key = algo . new_key ( " 0123456789ABCDEF " . as_bytes () ) . unwrap () ; let iv = " _THIS_IS_THE_IV_ " . as_bytes () ; let plaintext = " THIS_IS_THE_DATA " . as_bytes () ; let ciphertext = key . encrypt ( Some ( iv ) plaintext ) . unwrap () .