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

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

#36 in #io-operations

Download history 27/week @ 2024-03-24 52/week @ 2024-03-31 20/week @ 2024-04-07 25/week @ 2024-04-14 26/week @ 2024-04-21 22/week @ 2024-04-28 22/week @ 2024-05-05 21/week @ 2024-05-12 26/week @ 2024-05-19 24/week @ 2024-05-26 26/week @ 2024-06-02 18/week @ 2024-06-09 20/week @ 2024-06-16 20/week @ 2024-06-23 2/week @ 2024-06-30 55/week @ 2024-07-07

100 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
~50K SLoC