#aes #read-write #write #read #stream #encryption-decryption #io-read

aes-stream

Read/Write Wrapper for AES Encryption and Decryption during I/O Operations

4 releases

Uses old Rust 2015

0.2.1 Jul 3, 2017
0.2.0 Jul 2, 2017
0.1.2 Jul 2, 2017
0.1.1 Jul 1, 2017
0.1.0 Jul 1, 2017

#64 in #aes

42 downloads per month
Used in 2 crates

MIT/Apache

26KB
340 lines

AES Stream

License

Licensed under either of

at your option.


lib.rs:

Read/Write Wrapper for AES Encryption and Decryption during I/O Operations

This crate provides an AesWriter, which can be used to wrap any existing Write implementation with AES encryption, and AesReader, which can wrap any existing Read implemntation with AES decryption. If the inner reader provides a Seek implementation, AesReader will do so as well. See their struct-level documentation for more information.

In fact this crate is not limited to AES. It can wrap any kind of BlockEncryptor i.e. BlockDecryptor with CBC.

Examples

All examples use the following extern crates and imports:

extern crate crypto;
extern crate rand;
extern crate aesstream;

use std::io::{Read, Write, Cursor};
use std::fs::File;
use crypto::aessafe::{AesSafe128Encryptor, AesSafe128Decryptor};
use rand::{Rng, OsRng};
use aesstream::{AesWriter, AesReader};

You can use AesWriter to wrap a file with encryption.

let key: [u8; 16] = OsRng::new()?.gen();
let file = File::create("...")?;
let encryptor = AesSafe128Encryptor::new(&key);
let mut writer = AesWriter::new(file, encryptor)?;
writer.write_all("Hello World!".as_bytes())?;

And AesReader to decrypt it again.

let key: [u8; 16] = OsRng::new()?.gen();
let file = File::open("...")?;
let decryptor = AesSafe128Decryptor::new(&key);
let mut reader = AesReader::new(file, decryptor)?;
let mut decrypted = String::new();
reader.read_to_string(&mut decrypted)?;
assert_eq!(decrypted, "Hello World!");

They can be used to en- and decrypt in-memory as well.

let key: [u8; 16] = OsRng::new()?.gen();
let encryptor = AesSafe128Encryptor::new(&key);
let mut encrypted = Vec::new();
{
    let mut writer = AesWriter::new(&mut encrypted, encryptor)?;
    writer.write_all("Hello World!".as_bytes())?;
}
let decryptor = AesSafe128Decryptor::new(&key);
let mut reader = AesReader::new(Cursor::new(encrypted), decryptor)?;
let mut decrypted = String::new();
reader.read_to_string(&mut decrypted)?;
assert_eq!(decrypted, "Hello World!");

Dependencies

~4MB
~51K SLoC