This document is the property of Al Nafi.
Any unauthorized redistribution or reproduction, including in printed form, is strictly
prohibited. This document can only be read electronically.
Lab 9: Data Encryption with OpenSSL
Objective:
Understand the concepts of data encryption and decryption.
TASKS:
Generate public and private key pairs. 2. Encrypt sample data using the public key. 3. Decrypt
the data using the private key. 4. Verify data integrity with SHA-256.
td
Step 1: Install OpenSSL (if not already installed)
For Ubuntu/Debian:
tl
sudo apt update
Pv
sudo apt install openssl
For CentOS/RHEL:
sudo yum install openssl
ng
For macOS:
brew install openssl
ni
ar
Step 2: Generate a Private Key
Le
Generate a 2048-bit RSA Private Key:
Use the following command to generate a private key (private.pem):
IE
openssl genpkey -algorithm RSA -out private.pem -pkeyopt
rsa_keygen_bits:2048
AF
View the Private Key (optional):
To view the contents of the private key:
N
openssl pkey -in private.pem -text
AL
Step 3: Extract the Public Key
Generate the Public Key from the Private Key:
Extract the public key from the private key and save it as public.pem:
openssl rsa -pubout -in private.pem -out public.pem
View the Public Key (optional):
To view the contents of the public key:
openssl pkey -pubin -in public.pem -text
Task 2: Encrypt Sample Data Using the Public Key
td
Step 1: Create Sample Data File
Create a File with Sample Data:
tl
Create a text file sample.txt with the following content:
Pv
echo "This is a secret message that needs to be encrypted." > sample.txt
ng
Step 2: Encrypt Data Using Public Key
Encrypt the Sample Data: ni
Use the public key to encrypt the sample.txt file, and save the output as encrypted.bin:
ar
Le
openssl rsautl -encrypt -inkey public.pem -pubin -in sample.txt -out
encrypted.bin
IE
Verify Encrypted Data:
The encrypted.bin file will contain the encrypted data. It won't be human-readable.
AF
Task 3: Decrypt the Data Using the Private Key
Step 1: Decrypt the Encrypted Data
N
Decrypt the Encrypted File:
AL
Use the private key to decrypt the encrypted.bin file, and save the decrypted data to
decrypted.txt:
openssl rsautl -decrypt -inkey private.pem -in encrypted.bin -out
decrypted.txt
Verify the Decrypted Data:
Open and read the decrypted.txt file to ensure the data matches the original:
cat decrypted.txt
The decrypted data should read:
This is a secret message that needs to be encrypted.
Task 4: Verify Data Integrity with SHA-256
td
Step 1: Generate a SHA-256 Hash of the Original Data
tl
Create a Hash of the Original sample.txt File:
Pv
Use SHA-256 to generate a hash of the original data:
ng
openssl dgst -sha256 sample.txt
This will generate an output similar to:
SHA256(sample.txt)=
ni
ar
7ae3d7b6247f5bc3dbeb50658295e39f5b6db25bc5cc037fcfcdb1eaece6a476
Le
1.
Step 2: Verify Integrity of the Decrypted Data
IE
1. Create a Hash of the Decrypted decrypted.txt File:
Generate the SHA-256 hash of the decrypted data:
AF
N
openssl dgst -sha256 decrypted.txt
○
AL
2. Compare the Hashes:
○ Ensure that the SHA-256 hash of the decrypted file matches the original file's
hash. If both hashes are identical, the data was decrypted correctly and remains
intact.
Conclusion
By following this lab, you have:
1. Generated a public and private key pair using OpenSSL.
2. Encrypted sample data using the public key.
3. Decrypted the data using the private key.
4. Verified the data integrity by comparing the SHA-256 hash of the original and decrypted
data.
This exercise demonstrates the basics of public-key encryption and data integrity verification.
Let me know if you need any further assistance or clarification on any steps
td
tl
Pv
ng
ni
ar
Le
IE
AF
N
AL