#crypto #aes #read #write #stream

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

#29 in #write

Download history 12/week @ 2023-06-07 33/week @ 2023-06-14 75/week @ 2023-06-21 30/week @ 2023-06-28 31/week @ 2023-07-05 24/week @ 2023-07-12 47/week @ 2023-07-19 43/week @ 2023-07-26 47/week @ 2023-08-02 115/week @ 2023-08-09 58/week @ 2023-08-16 38/week @ 2023-08-23 29/week @ 2023-08-30 80/week @ 2023-09-06 67/week @ 2023-09-13 27/week @ 2023-09-20

222 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

~3.5MB
~48K SLoC