引入依赖
[dependencies]
hex-literal = "0.4.1"
hex="0.4.3"
des = "0.8.1"
base64 = "0.21.0"
上代码:
use des::cipher::generic_array::GenericArray;
use des::cipher::BlockEncrypt;
use des::cipher::KeyInit;
use des::TdesEde3;
use hex_literal::hex;
fn main() {
//24位的key
let key = hex!("111111111111111122222222222222223333333333333333");
println!("len={}", key.len());
let mut key_byte = [0u8; 24];
key_byte.copy_from_slice(&key.to_vec());
let key2 = GenericArray::from(key_byte);
let tdes = TdesEde3::new(&key2);
//数据是1234,但不足8位,采用PKCS7Padding或PKCS5Padding 后面几位填4
let mut block_data = [0u8; 8];
block_data[0] = b'1';
block_data[1] = b'2';
block_data[2] = b'3';
block_data[3] = b'4';
block_data[4] = 4;
block_data[5] = 4;
block_data[6] = 4;
block_data[7] = 4;
let mut block = GenericArray::from(block_data);
tdes.encrypt_block(&mut block);
let after_vec = block.to_vec();
println!("{}", after_vec.len());
let s = hex::encode(after_vec);
println!("{}", s);
}