-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
40 additions
and
135 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,35 @@ | ||
#[macro_use] extern crate hex_literal; | ||
extern crate aes_soft as aes; | ||
extern crate block_modes; | ||
|
||
use aes::Aes128; | ||
use block_modes::{BlockMode, Cbc}; | ||
use block_modes::block_padding::Pkcs7; | ||
use hex_literal::hex; | ||
|
||
// create an alias for convenience | ||
type Aes128Cbc = Cbc<Aes128, Pkcs7>; | ||
|
||
fn main() { | ||
//println!("Hello World!"); | ||
|
||
let key = hex!("000102030405060708090a0b0c0d0e0f"); | ||
let iv = hex!("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"); | ||
let plaintext = b"Hello world!"; | ||
let cipher = Aes128Cbc::new_var(&key, &iv).unwrap(); | ||
|
||
// buffer must have enough space for message+padding | ||
let mut buffer = [0u8; 32]; | ||
// copy message to the buffer | ||
let pos = plaintext.len(); | ||
buffer[..pos].copy_from_slice(plaintext); | ||
let ciphertext = cipher.encrypt(&mut buffer, pos).unwrap(); | ||
|
||
assert_eq!(ciphertext, hex!("1b7a4c403124ae2fb52bedc534d82fa8")); | ||
|
||
// re-create cipher mode instance and decrypt the message | ||
let cipher = Aes128Cbc::new_var(&key, &iv).unwrap(); | ||
let mut buf = ciphertext.to_vec(); | ||
let decrypted_ciphertext = cipher.decrypt(&mut buf).unwrap(); | ||
|
||
assert_eq!(decrypted_ciphertext, plaintext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters