我在尝试解密已保存到文本文件的消息时遇到了一些问题。尝试解密消息时,我收到错误消息-“线程'main'在'调用Result::unwrap()
一个Err
值:BlockModeError'时惊慌失措”。这真的并没有给我太多可玩的东西。而且我在网上找不到任何关于其他人有问题的信息。我正在使用这个板条箱block-modes。这是他们用来使用块模式文档的示例
use block_modes::block_padding::Pkcs7;
type Aes128Cbc = Cbc<Aes128, Pkcs7>;
pub struct FileHandler{
file: File,
file_path: String,
encyption_key: [u8; 16],
}
impl FileHandler{
pub fn new() -> FileHandler{
let mut temp_dir = PathBuf::from(UserDirs::new().unwrap().document_dir().unwrap());
temp_dir.push("storage.txt"); // Creating Path for docs folder
let temp_file: File;
match OpenOptions::new().append(true).read(true).open(&temp_dir){
Ok(result) => {temp_file = result},
Err(_) => { temp_file = File::create(&temp_dir).expect(&format!("Could not create file! - {}", temp_dir.to_str().unwrap())); }
} // Opening the file in read and append
FileHandler {
file: temp_file,
file_path: String::from(temp_dir.to_str().unwrap()),
encyption_key: [0u8; 16],
} // returning the FileHandler object
}
fn write(&mut self, string: &str, url_key: &Url){
let mut iv_buffer = [0u8; 16];
let cypher = Aes128Cbc::new_from_slices(&self.encyption_key, &iv_buffer).unwrap(); // creates the cyoher object
for letter in cypher.encrypt_vec(string.as_bytes()){
self.file.write_all(&[letter]).expect("Failed to write to file!"); // loops through each encrypted letter and writes it
}
}
pub fn read_details(&mut self) -> Option<(String, String)> {
let mut iv_buffer = [0u8; 16];
let metadata = fs::metadata(&self.file_path).unwrap();
let mut buffer = vec![0; metadata.len() as usize]; // making a buffer that will fit the contents of the file
self.file.read(&mut buffer).expect("buffer overflow"); // reading to the buffer
let cypher = Aes128Cbc::new_from_slices(&self.encyption_key, &iv_buffer).unwrap(); // creating cypher object
let temp = cypher.decrypt_vec(&mut buffer).unwrap(); // Where the error occurs
None
}
}
任何帮助都将不胜感激,因为我已经为此困扰了一段时间。