Encrypt File in Ubuntu with GPG

In this article, we’ll explain how to encrypt file in Ubuntu with GPG.

“GPG” or “GNU Privacy Guard” allows you to securely encrypt files and data with a strong encryption algorithm. GPG is an excellent method to ensure secure communication between two parties. It allows sensitive information to be easily shared across an insecure network.

Prerequisites

  • An Ubuntu installed desktop, dedicated server or KVM VPS.
  • A root user access or normal user with administrative privileges to install GPG.

Encrypt File in Ubuntu with GPG

Install GPG in Ubuntu

We can install GPG using following command:

# apt install gpg

Encrypt file using GPG

Following command will encrypt test.txt.

# gpg -c test.txt

Once you run the above command, you will ask to enter a passphrase that will be used for encrypting and decrypting the file.

encrypt file in Linux

After you enter passphrase, file gets encrypted and one file gets create in the following format:

test.txt.gpg

To decrypt the file, run a command in the following format:

# gpg test.txt.gpg

Use the passphrase you initially used to encrypt the file. Once decrypted, you will get back a “test.txt” file.

Generate a GPG key pair

You can use a pair of “public” and “private” keys to encrypt and decrypt files. To generate a GPG key pair, you can use the command below:

# gpg — generate-key

Follow the instructions and enter a username and password when prompted. After generating the key pair, GPG will show the generated public key in terminal output.

To export public key to a file, run the following command:

# gpg — armor — export — output “public_key”

Replace “public_key” with your own public key generated in the step above. Your generated private keys will be available in “$HOME/.gnupg/” directory.

Encrypt a file using public key of someone else, use the following command:

# gpg — encrypt — recipient-file public_key.file test.txt

Note: Replace public_key.file with public key of the person you want to send the file.

Decrypt a file that was encrypted using your public key, use the following command:

# gpg — decrypt — output test.txt test.txt.gpg

GPG will automatically detect your private keys during decryption as long as they are stored in the “$HOME/.gnupg/” directory.

Learn more about GPG cheatsheet.

In this article, we have seen how to encrypt file in Ubuntu with GPG.

--

--

--

HostnExtra was founded in 2016. All of our servers are located in the continental United States and United Kingdom. View more details https://www.hostnextra.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Privacy Law for the Digital Economy: The new Digital Charter Implementation Act

JET NFT VIEWER TUTORIAL FOR MOBILE

Introduction to Cryptography

ZEROPAY Airdrop

Y2Mate 2022 | Y2Mate.com Download Y2Mate mp3 & videos Download

Y2Mate 2022 | Y2Mate.com Download Y2Mate mp3 & videos Download

WhatsApp's new privacy policy EXPLAINED.

DRM 101 — Introduction to DRM systems

Filecoin — A Revolutionary Blockchain Storage Platform That Can Change The Industry

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
HostnExtra Technologies

HostnExtra Technologies

HostnExtra was founded in 2016. All of our servers are located in the continental United States and United Kingdom. View more details https://www.hostnextra.com

More from Medium

Upgrade cURL on Ubuntu Server

Headless Raspberry PI’s on Tailscale: A cloud-like provisioning process

Using custom domain on Synology NAS with Dynamic IP

Hashicorp Vault Rest API