[][src]Struct libaes::Cipher

pub struct Cipher { /* fields omitted */ }

AES cipher struct

The user would create a cipher first by calling new_<key-length>() (e.g. new_128()), and then call encrypt and/or decrypt methods as needed.

Implementations

impl Cipher[src]

pub fn new_128(user_key: &[u8; 16]) -> Cipher[src]

Create an AES 128-bit cipher with a given key.

Once created, a cipher can encrypt or decrypt any times of data using the same key. user_key must be at length of 128-bits, i.e. 16 bytes.

Examples

use libaes::Cipher;

let my_key = b"This is the key!"; // 16 bytes
let cipher = Cipher::new_128(my_key);

pub fn cbc_encrypt(&self, iv: &[u8], data: &[u8]) -> Vec<u8>[src]

Encrypt in CBC mode.

The input data is not modified. The output is a new Vec. Padding is done automatically. iv is a 16-byte slice. Panics if iv is less than 16 bytes.

Examples

use libaes::Cipher;

let my_key = b"This is the key!"; // key is 16 bytes
let plaintext = b"A plaintext"; // less than 16 bytes, will be padded automatically
let iv = b"This is 16 bytes";
let cipher = Cipher::new_128(my_key);
let encrypted = cipher.cbc_encrypt(iv, plaintext);

pub fn cbc_decrypt(&self, iv: &[u8], data: &[u8]) -> Vec<u8>[src]

Decrypt in CBC mode.

The input data is not modified. The output is a new Vec. Padding is handled automatically. iv is a 16-byte slice. Panics if iv is less than 16 bytes.

Examples

use libaes::Cipher;

let my_key = b"This is the key!";
let iv = b"This is 16 bytes";
let plaintext = b"This is plain text for AES";
let ciphertext = b"\xfe\x2e\xa4\x03\xd2\x65\xa9\xe6\x78\xee\x16\x35\xf3\xa6\xe6\xf4\
                   \xa3\x16\xb6\xd4\x35\x6d\x2b\x6f\x49\xff\x9e\x3c\xe4\x66\x16\xb9";

let cipher = Cipher::new_128(my_key);
let decrypted = cipher.cbc_decrypt(iv, ciphertext);
assert_eq!(plaintext, &decrypted[..]);

Auto Trait Implementations

impl RefUnwindSafe for Cipher

impl Send for Cipher

impl Sync for Cipher

impl Unpin for Cipher

impl UnwindSafe for Cipher

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.