File tree Expand file tree Collapse file tree 1 file changed +25
-1
lines changed Expand file tree Collapse file tree 1 file changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -6,10 +6,17 @@ use std::{
66} ;
77
88fn main ( ) {
9+ let encrypted_file_path = "sample-file.txt.encrypted" ;
10+
911 encrypt_file (
1012 "sample-file.txt" ,
1113 "password12345678password12345678" . as_bytes ( ) ,
1214 ) ;
15+
16+ decrypt_file (
17+ encrypted_file_path,
18+ "password12345678password12345678" . as_bytes ( ) ,
19+ ) ;
1320}
1421
1522type Aes256Cbc = Cbc < Aes256 , Pkcs7 > ;
@@ -29,6 +36,23 @@ fn encrypt_file(file_path: &str, key: &[u8]) {
2936 output_file
3037 . write_all ( & cipher_text)
3138 . expect ( "Failed to write encrypted data to the output file" ) ;
39+ }
40+
41+ fn decrypt_file ( file_path : & str , key : & [ u8 ] ) {
42+ let encrypted_content = fs:: read ( file_path) . expect ( "Failed to read the encrypted file" ) ;
43+
44+ let iv = [ 0u8 ; 16 ] ;
3245
33- println ! ( "IV: {:?}" , iv) ;
46+ let cipher = Aes256Cbc :: new_from_slices ( key, & iv) . expect ( "Failed to create AES cipher" ) ;
47+
48+ let decrypted_content = cipher
49+ . decrypt_vec ( & encrypted_content)
50+ . expect ( "Failed to decrypt data" ) ;
51+
52+ let mut output_file =
53+ File :: create ( format ! ( "{}.decrypted" , file_path) ) . expect ( "Failed to create the output file" ) ;
54+
55+ output_file
56+ . write_all ( & decrypted_content)
57+ . expect ( "Failed to write decrypted data to the output file" ) ;
3458}
You can’t perform that action at this time.
0 commit comments