Function openssl::symm::encrypt[][src]

pub fn encrypt(
    t: Cipher,
    key: &[u8],
    iv: Option<&[u8]>,
    data: &[u8]
) -> Result<Vec<u8>, ErrorStack>

Encrypts data in one go, and returns the encrypted data.

Data is encrypted using the specified cipher type t in encrypt mode with the specified key and initailization vector iv. Padding is enabled.

This is a convenient interface to Crypter to encrypt all data in one go. To encrypt a stream of data increamentally , use Crypter instead.

Examples

Encrypt data in AES128 CBC mode

use openssl::symm::{encrypt, Cipher};

let cipher = Cipher::aes_128_cbc();
let data = b"Some Crypto Text";
let key = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F";
let iv = b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07";
let ciphertext = encrypt(
    cipher,
    key,
    Some(iv),
    data).unwrap();

assert_eq!(
    b"\xB4\xB9\xE7\x30\xD6\xD6\xF7\xDE\x77\x3F\x1C\xFF\xB3\x3E\x44\x5A\x91\xD7\x27\x62\x87\x4D\
      \xFB\x3C\x5E\xC4\x59\x72\x4A\xF4\x7C\xA1",
    &ciphertext[..]);