Raspberry Pi

How to Encrypt and Decrypt Files on Raspberry Pi Using the age Command

Encrypting the files on a system is crucial as this helps in securing the data inside the file by not permitting unauthorized users to access it. It’s handy in case when you are storing a large amount of data on servers because, in that case, you will certainly need to encrypt the files to secure your data from hackers.

If you are a Raspberry Pi user and are interested in performing file encryption and decryption on your device, you should need to install the age command, which is pretty helpful in terms of securing your files.

Installing the age Command Line Utility to Encrypt/Decrypt files on Raspberry Pi

You cannot directly use the age command on the Raspberry Pi operating system because its repository isn’t included in the official source list of Raspberry Pi. However, you can manually download the tar.gz file and install it on your system using the following steps:

Step 1: Download the age tar.gz File

To install the age command on Raspberry Pi, you will first need to retrieve the tar.gz file from the GitHub website using the following command:

$ wget https://github.com/FiloSottile/age/releases/download/v1.0.0/age-v1.0.0-linux-arm.tar.gz

Step 2: Extract age tar.gz File Contents

Once the above tar.gz file is downloaded on Raspberry Pi, you can extract the contents using the following command:

$ tar -xf age-v1.0.0-linux-arm.tar.gz

Step 3: Move age and age-keygen Files to Bin Directory

Next, you will need to move the age file to the location “usr/local/bin” through the following command:

$ sudo mv age/age /usr/local/bin

You will also need to move the age-keygen to the same location where age file is stored by executing the following command:

$ sudo mv age/age-keygen /usr/local/bin

After moving the files to the “usr/local/bin” location, you can confirm the successful installation of age command on Raspberry Pi by checking the version using the following command:

$ age --version

Step 4: Testing the age Command on Raspberry Pi

After successfully installing the age command on Raspberry Pi, it’s now time to create a file for testing purpose so that you can understand how to encrypt files through this command:

$ echo ‘Testing’ > testfile.txt

In the above command, you can replace the name and content of the file with your own choice.

Step 5: Generate Public Key Using the age-keygen Command

Next, you will need to use the “age-keygen” command to successfully generate a public key for encrypting the data and a private key for decrypting the data on Raspberry Pi and to do that, you have to execute the following command:

$ age-keygen -o key.txt

It’s important to note the above command only show you the public key, however, with the file both public and private keys are created and if you want to view them, you can execute the following command:

$ cat key.txt

Step 6: Encrypt File on Raspberry Pi Using the age Command

Now, to encrypt the file on Raspberry Pi, you can use the following syntax with the file name that you want to encrypt.

$ age -r [public_key] [file_name] > [file_name.age]

Step 7: Decrypt File on Raspberry Pi Using the age Command

To decrypt the file on Raspberry Pi, you can use the following syntax with the name of the file you have created in Step 4.

$ age -d -i key.txt [file_name.age] > [file_name]

In this way, you can successfully encrypt and decrypt files on Raspberry Pi through age command.

Removing the age Command Utility From Raspberry Pi

To remove age command from Raspberry Pi, you can remove the age and age-keygen files from the location “usr/local/bin” through the following commands:

$ sudo rm -rf /usr/local/bin/age

$ sudo rm -rf /usr/local/bin/age-keygen

Conclusion

File encryption on Raspberry Pi is extremely useful for users who want to protect their important data from external attacks. The Raspberry Pi users can use the age command to perform file encryption and decryption on their devices. The step-by-step explanation to install and use the age command is provided in the above guidelines. If you follow them, you can perform file encryption and decryption efficiently on your Raspberry Pi device.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.