If you want to learn how to encrypt and decrypt a partition on Raspberry Pi, follow this article’s guidelines.
Encrypt and Decrypt a Partition in Raspberry Pi
To encrypt and decrypt a Raspberry Pi partition, follow the below-given steps:
Step 1: First install the encryption utility called cryptsetup on Raspberry Pi from the following command:
Step 2: Attach a disk drive to Raspberry Pi on which you want to create a partition. Here, in my case, it’s a USB drive, which can be found through the following command:
Step 3: Since here we are dealing with removal media, we have to unmount the drive first from the following command:
Note: The drive could be different in your case.
Step 4: To confirm the partition is unmounted, you can run the following command:
Step 5: Now format the targeted partition and create a LUKS encryption container from the following command:
Note: The LUKS encryption container helps you create an encrypted partition.
Step 6: Provide a password for the partition and verify it too.
At this point, the partition is successfully encrypted.
Decrypt a Partition
You cannot directly access the drive since it’s encrypted. To decrypt the drive, you must create a mapper for the drive and open it with the following command:
Note: You must need to provide a password for the encrypted drive you set in Step 5 and Step 6. The above command will create a mapper drive on your system, located in the “/dev/mapper”. You can confirm it from the “ls” command:
Since the drive doesn’t have a partition system, we need to create a file system for it using the following command:
Now, create a directory for the encrypted drive from the following command:
Then copy the data of the encrypted drive to that created directory from the following command:
You can check the drive type by running the following command:
You can now unmount the drive just like you do for other removal media using the following command:
Now the drive is unmounted, we should close the partition volume and dissociate the memory in the kernel from the mapping and key using the following command:
To confirm the drive is no longer available for the system, you can run the following command:
Conclusion
The Raspberry Pi users can encrypt and decrypt a partition on the system by first installing the cryptsetup utility. Then they must unmount a partition drive and perform the encryption process with the LUKS encryption container. It provides users the ability to set a strong password for a partition. Later, the users can decrypt the same partition by creating a mapper for the drive, creating a file system, and copying the encrypted partition data onto another drive. After that, they can unmount and close the partition to complete the decryption process on the Raspberry Pi system.