Open In App

Blockchain – Encrypt & Decrypt Files With Password Using OpenSSL

Improve
Improve
Like Article
Like
Save
Share
Report

Encryption is the process of encoding information or data in such a way that only authorized parties can access it. The process of encrypting information involves using a mathematical algorithm to transform the original information, known as plaintext, into a form that is unreadable to anyone who does not have the means to decrypt it, known as ciphertext. The mathematical algorithm used to encrypt the information is known as a cipher.

Decryption is the process of decoding the encrypted information to make it readable again. To decrypt the information, a decryption key or password is required to reverse the encryption process and transform the ciphertext back into plaintext. Only parties who have the decryption key or password can access the information in the encrypted form.

Encryption and decryption are important for protecting sensitive information, such as personal data or financial information, from unauthorized access. Encrypting the information, it becomes much more difficult for anyone who does not have a decryption key or password to access the information, even if they manage to obtain the encrypted data. This helps to ensure the confidentiality and security of the information.

OpenSSL

OpenSSL is an open-source command line tool that is commonly used to generate private keys, create CSRs, install your SSL/TLS certificate, and identify certificate information.

  • Download OpenSSL’s required libraries from here
  • Run the .exe file and install OpenSSL in the system.
  • Open the command prompt (cmd) and redirect the path to the bin folder.
Redirect path to bin folder

 

Implementation

Two methods will be discussed here to encrypt and decrypt files using OpenSSL:

1. Using Password: It is possible to encrypt and decrypt files using a password with OpenSSL. OpenSSL is a free and open-source cryptography toolkit that can be used to encrypt and decrypt files as well as manage SSL certificates. To encrypt a file with a password, you can use the enc command in OpenSSL, which encrypts data using various algorithms, such as AES (Advanced Encryption Standard) or DES (Data Encryption Standard). To decrypt a file that has been encrypted with a password, you can use the dec command in OpenSSL.

Here is an example of how you can use OpenSSL to encrypt a file with a password:

openssl enc -aes-256-cbc -salt -in file.txt -out file.enc -k password

This command will encrypt the file file.txt using the AES-256-CBC algorithm and write the encrypted file to file.enc. The -salt option adds random data to the encryption process to make it more secure, and the -k option specifies the password that will be used to encrypt the file.

Output:

Encrypt file using OpenSSL

encrypted file

To decrypt the file, you can make use of the following command:

openssl enc -d -aes-256-cbc -in file.enc -out file.txt -k password

This command will decrypt the file file.enc using the AES-256-CBC algorithm and write the decrypted file to file.txt. The -d option tells OpenSSL to decrypt the file, and the -k option specifies the password that was used to encrypt the file.

Output:

Decrypt file Using OpenSSL

decrypted file

While decrypting if the wrong password is specified then it will generate the below output:

Incorrect password entered

 

It is important to note that the password used to encrypt the file is the only way to decrypt it, so it is important to choose a strong password and keep it safe. Additionally, it is recommended to use a different password for each file to enhance security.

2. Public Key Encryption: It is possible to encrypt and decrypt files using public key encryption with OpenSSL. OpenSSL is a free and open-source cryptography toolkit that can be used to encrypt and decrypt files as well as manage SSL certificates. Public key encryption is a type of encryption that uses two keys:

  • Public key: It is used to encrypt the data.
  • Private key: It is used to decrypt the data.

To encrypt a file with a public key using OpenSSL, the rsautl command can be used, which can be used to encrypt and decrypt files using RSA encryption. To encrypt a file with a public key, first generate a public and private key pair using the genrsa command in OpenSSL. 

Here is an example of how you can generate a public and private key pair:

openssl genrsa -out private.key 512

openssl rsa -in private.key -pubout -out public.key

This will generate a private key with the name private.key and a public key with the name public.key. 

genrsa command for private key

private key

genrsa command for public key

public key

Use the rsautl command to encrypt a file with the public key. Here is an example of how you can encrypt a file with a public key:

openssl rsautl -encrypt -inkey public.key -pubin -in file.txt -out file.enc

This command will encrypt the file file.txt using the public key in the file public.key and write the encrypted file to file.enc

Output:

Encrypt file

encryption using public key 

To decrypt a file that was encrypted with a public key using OpenSSL, use the rsautl command again, but this time with the -decrypt option instead of the -encrypt option. You will also need to specify the private key file with the -inkey option, as the private key is used to decrypt the file. Here is an example of how you can decrypt a file with a private key using OpenSSL:

openssl rsautl -decrypt -inkey private.key -in file.enc -out file.dec

This command will decrypt the encrypted file file.enc using the private key in the file private.key and write the decrypted file to file.dec. The -decrypt option tells OpenSSL to decrypt the file, and the -inkey option specifies the private key file to use for decryption.

Output:

Decrypt file

decrypted using public encryption



Last Updated : 02 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads