Follow

Follow
How To Build CLI App In Rust Using Clap - Part 4

How To Build CLI App In Rust Using Clap - Part 4

Decrypt Command

Shreyas K S's photo
Shreyas K S
·Oct 10, 2022·

2 min read

Play this article

Previously in this series, we covered the encryption command and configured the main function to handle the parsed arguments for the encrypt command. In this post, we will add the last command to our cli application which does the decryption of the encrypted file.

Command Decrypt

The decrypt command will be having two flags encrypted_file and key_path which decrypt the encrypted file using the secret.

Let's create a new file decrypt.rs. Include this file in the module tree by adding:

mod decrypt;
use decrypt::decrypt;

Now we will add a new function decrypt in the file we created.

use super::*;

pub fn decrypt(encrypted_file_path: String, secret: String) {
    let encrypted_content = read(encrypted_file_path.clone()).unwrap();
    let secret = read(secret).unwrap();
    let secret: Secret = serde_json::from_slice(&secret).unwrap();

    let cipher = AES::new_from_slices(&secret.decode_key(), &secret.decode_iv()).unwrap();

    let mut buffer = base64::decode(encrypted_content).unwrap();
    let decrypted_data = cipher.decrypt(&mut buffer).unwrap();

    write(encrypted_file_path, str::from_utf8(decrypted_data).unwrap()).unwrap();
}

In the above-mentioned code, we are reading the contents of the secret file and encrypted file. This secret content that is read from the file is deserialized into Secret and decoded to get the key and initial value. Data which we read from the encrypted file is decoded using bas64 decoding.

This decoded Key and Initial value are used to create an instance of AES cipher. We use this instance of cipher to decrypt the content and write it into the file.

Final step, we will call this function inside the decrypt command which provides an encrypted file and secret file path as arguments.

fn main() {
    let cryptifer = Cryptifer::parse();

    match cryptifer.command {
        commands::Commands::Generate { output_path } => {
            Secret::new(output_path);
        }
        commands::Commands::Encrypt {
            file_path,
            key_path,
        } => {
            encrypt(file_path, key_path);
        }
        commands::Commands::Decrypt {
            encrypted_file,
            key_path,
        } => decrypt(encrypted_file, key_path),
    }
}

Let us run our cli application to decrypt the file

cargo run decrypt  -f <encrypted_file_path> -k <secret_file_path>

This will decrypt the file using a secret.

⚡ Wrapping Things Up

Cheers 🥂 . We completed our first command line application rust using Clap. Now our cli application can generate a secret, encrypt the file using a secret and decrypt the encrypted file using a secret. The upcoming post in this series will be its final one in that we will be focusing on Error Handling and Code cleanup. Please let me know if the information in any of the posts is obscure so that I can update the content to make it clearer.

Did you find this article valuable?

Support Shreyas K S by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this