[][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 new_192(user_key: &[u8; 24]) -> Cipher[src]

Create an AES 192-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 192-bits, i.e. 24 bytes.

Examples

use libaes::Cipher;

let my_key = b"This is the key! 192 bit"; // 24 bytes
let cipher = Cipher::new_192(my_key);

pub fn new_256(user_key: &[u8; 32]) -> Cipher[src]

Create an AES 256-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 256-bits, i.e. 32 bytes.

Examples

use libaes::Cipher;

let my_key = b"This is the key!This is the key!"; // 32 bytes
let cipher = Cipher::new_256(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 (PKCS7) is included. iv is a 16-byte slice. Panics if iv is less than 16 bytes.

This method works for all key sizes.

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]
) -> Result<Vec<u8>, CipherError>
[src]

Decrypt in CBC mode.

The input data is not modified. The output is a new Vec if successful, otherwise an error is returned. Padding is handled automatically.

iv is a 16-byte slice. Panics if iv is less than 16 bytes.

This method works for all key sizes.

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).unwrap();
assert_eq!(plaintext, &decrypted[..]);

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

Encrypt in CFB128 mode (i.e. CFB mode with 128-bit segment size).

The input data is not modified. The output is a new Vec. No padding. 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, i.e. 128-bit
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.cfb128_encrypt(iv, plaintext);

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

Decrypt in CFB128 mode (i.e. CFB mode with 128-bit segment size).

The input data is not modified. The output is a new Vec. No padding. 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"\x16\x94\x9e\xe0\x7c\x4b\x7a\xc2\x83\xca\x96\x4f\xae\
                   \x6a\x35\xbc\x8e\xdb\x19\x0a\x4d\x8d\x19\x59\x0f\x2a";

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

Auto Trait Implementations

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.