```rust // IMPORTANT! key and nonce SHOULD generate by random let mut key = [0u8; 32]; let mut nonce = [0; 12];
let mut encryptor = Aes256GcmStreamEncryptor::new(key.clone(), &nonce);
let mut ciphertext = vec![]; ciphertext.extendfromslice(&encryptor.update(b"Hello ")); ciphertext.extendfromslice(&encryptor.update(b" World")); ciphertext.extendfromslice(&encryptor.update(b"!")); let (lastblock, tag) = encryptor.finalize(); ciphertext.extendfromslice(&lastblock); ciphertext.extendfromslice(&tag);
println!("Ciphertext: {}", hex::encode(&ciphertext)); ```
Open example: encryptanddecrypt.rs
shell
$ cargo run --example encrypt_and_decrypt
Finished dev [unoptimized + debuginfo] target(s) in 0.10s
Running `target/debug/examples/encrypt_and_decrypt`
Ciphertext: 86c22c5122404b39683ca9b79b889fd00a6212d1be2ebc3f4f8f22f90b
Plaintext: Hello World!
Thanks: https://developer.aliyun.com/article/952809