usecrypto::aes;usecrypto::aes::KeySize::KeySize256;usecrypto::blockmodes::PkcsPadding;usecrypto::buffer::{ReadBuffer,RefReadBuffer,RefWriteBuffer,WriteBuffer};usecrypto::symmetriccipher::SymmetricCipherError;/// Encrypt a buffer with the given key and iv using AES256/CBC/Pkcs encryption./// 加密(data:加密数据;key:密钥(长度为32的字符串);iv:偏移量(长度为16的字符串))pubfnaes256_cbc_encrypt(
data:&[u8],
key:&str,
iv:&str,)->Result<Vec<u8>,SymmetricCipherError>{let key =string_to_fixed_array_32(key);let iv =string_to_fixed_array_16(iv);letmut encryptor =aes::cbc_encryptor(KeySize256,&key,&iv,PkcsPadding,);letmut buffer =[0;4096];letmut write_buffer =RefWriteBuffer::new(&mut buffer);letmut read_buffer =RefReadBuffer::new(data);letmut final_result =Vec::new();loop{let result = encryptor.encrypt(&mut read_buffer,&mut write_buffer,true)?;
final_result.extend(write_buffer.take_read_buffer().take_remaining().iter().map(|&i| i));match result {crypto::buffer::BufferResult::BufferUnderflow=>break,
_ =>continue,}}Ok(final_result)}/// Decrypt a buffer with the given key and iv using AES256/CBC/Pkcs encryption./// 解密(data:加密数据;key:密钥(长度为32的字符串);iv:偏移量(长度为16的字符串))pubfnaes256_cbc_decrypt(
data:&[u8],
key:&str,
iv:&str,)->Result<Vec<u8>,SymmetricCipherError>{let key =string_to_fixed_array_32(key);let iv =string_to_fixed_array_16(iv);letmut decryptor =aes::cbc_decryptor(KeySize256,&key,&iv,PkcsPadding,);letmut buffer =[0;4096];letmut write_buffer =RefWriteBuffer::new(&mut buffer);letmut read_buffer =RefReadBuffer::new(data);letmut final_result =Vec::new();loop{let result = decryptor.decrypt(&mut read_buffer,&mut write_buffer,true)?;
final_result.extend(write_buffer.take_read_buffer().take_remaining().iter().map(|&i| i));match result {crypto::buffer::BufferResult::BufferUnderflow=>break,
_ =>continue,}}Ok(final_result)}/// 将字符串转为[u8; 32]fnstring_to_fixed_array_32(s:&str)->[u8;32]{
s.as_bytes().try_into().expect("字符串转为[u8; 32]失败")}/// 将字符串转为[u8; 16]fnstring_to_fixed_array_16(s:&str)->[u8;16]{
s.as_bytes().try_into().expect("字符串转为[u8; 16]失败")}#[test]fntest_aes256_cbc(){// 设置key长度为32let key ="abcdefghijklmnopqrstuvwxyz123456";// 设置iv长度为16let iv ="1234567890123456";// 设置需要加密的字符串let data ="Hello, world!";// 加密操作let encrypted_data =aes256_cbc_encrypt(data.as_bytes(),&key,&iv).unwrap();// 解密操作let decrypted_data =aes256_cbc_decrypt(encrypted_data.as_slice(),&key,&iv).unwrap();// 转为原始字符串信息let result =std::str::from_utf8(decrypted_data.as_slice()).unwrap();assert_eq!(data, result);println!("{}", result);}