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§
source§impl Cipher
impl Cipher
sourcepub fn new_128(user_key: &[u8; 16]) -> Cipher
pub fn new_128(user_key: &[u8; 16]) -> Cipher
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);
sourcepub fn new_192(user_key: &[u8; 24]) -> Cipher
pub fn new_192(user_key: &[u8; 24]) -> Cipher
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);
sourcepub fn new_256(user_key: &[u8; 32]) -> Cipher
pub fn new_256(user_key: &[u8; 32]) -> Cipher
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);
sourcepub fn set_auto_padding(&mut self, auto_padding: bool)
pub fn set_auto_padding(&mut self, auto_padding: bool)
Changes the auto_padding
setting of the cipher. The default value is true
.
When setting it to false
, the input data of CBC encryption must be multiple of AES_BLOCK_SIZE
.
And note that auto_padding
is ignored in CFB mode.
sourcepub fn auto_padding(&self) -> bool
pub fn auto_padding(&self) -> bool
Returns the auto_padding
setting in this cipher. If true,
the padding is done in PKCS7.
sourcepub fn cbc_encrypt(&self, iv: &[u8], data: &[u8]) -> Vec<u8>
pub fn cbc_encrypt(&self, iv: &[u8], data: &[u8]) -> Vec<u8>
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.
Note: auto padding is enabled by default. If the cipher disabled auto padding, this method will not add padding. If the input data length is not multiple of AES_BLOCK_SIZE, the output will be an empty Vec (i.e. error).
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);
sourcepub fn cbc_decrypt(&self, iv: &[u8], data: &[u8]) -> Vec<u8>
pub fn cbc_decrypt(&self, iv: &[u8], data: &[u8]) -> Vec<u8>
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[..]);
sourcepub fn cfb128_encrypt(&self, iv: &[u8], data: &[u8]) -> Vec<u8>
pub fn cfb128_encrypt(&self, iv: &[u8], data: &[u8]) -> Vec<u8>
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);
sourcepub fn cfb128_decrypt(&self, iv: &[u8], data: &[u8]) -> Vec<u8>
pub fn cfb128_decrypt(&self, iv: &[u8], data: &[u8]) -> Vec<u8>
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[..]);