pub struct Cipher { /* private fields */ }
Expand description

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

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);

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);

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);

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);

Decrypt in CBC mode.

The input data is not modified. The output is a new Vec. Padding is handled automatically. If decrypt encountered unexpected errors, the output is an empty Vec. If the cipher has the wrong key, the output will either be a wrong plaintext or an empty Vec.

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

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);

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

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.