Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use GPG to encrypt and decrypt files under Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use GPG to encrypt and decrypt files under Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

GnuPG (English: GNU Privacy Guard, abbreviation: GPG) is a free tool added, which is mostly used for the transmission of encrypted information. In addition to password-only encryption, the biggest difference of gpg is that it provides a "public key / private key" pair. Using the "public key" for others to encrypt information no longer need to tell the password, send encrypted information.

Install GPG

GPG is widely used. You can find it in the repository of almost every distribution. If you haven't installed it yet, install it now.

Debian/Ubuntu

$sudo apt install gnupg

Fedora

# dnf install gnupg2

Arch

# pacman-S gnupg

Gentoo

# emerge-- ask app-crypt/gnupg creates a key

You need a key pair to encrypt and decrypt the file. If you have already generated a key pair for SSH, you can use it directly. If not, GPG includes tools to generate key pairs.

$gpg-full-generate-key

GPG has a command line program that can help you generate keys step by step. It also has a much simpler tool, but this tool does not allow you to set the key type, key length and expiration time, so this tool is not recommended.

GPG will first ask you about the type of key. If there is nothing special, just choose the default value.

The next step is to set the key length. 4096 is a good choice.

After that, you can set the expiration date. Set to 0 if you want the key to never expire.

Then, enter your name.

Finally, enter the email address.

You can add a comment if you need it.

When all this is done, GPG will ask you to verify the information.

GPG will also ask you if you need to set a password for the key. This step is optional, but it will increase the degree of protection. If you need to set a password, GPG will collect your operation information to increase the robustness of the key. When all this is done, GPG displays information about the key.

Basic methods of encryption

Now that you have your own key, it's very easy to encrypt the file. Use the following command to create a blank text file in the / tmp directory.

$touch / tmp/test.txt

Then encrypt it with GPG. Here the-e flag tells GPG that you want to encrypt the file, and the-r flag specifies the recipient.

$gpg-e-r "Your Name" / tmp/test.txt

GPG needs to know the recipient and sender of this file. Since this file is given to you, there is no need to specify the sender, and the recipient is yourself.

The basic method of decryption

After you receive the encrypted file, you need to decrypt it. You do not need to specify the key to decrypt. This information is encoded in the file. GPG will try to decrypt it with the key.

$gpg-d / tmp/test.txt.gpg send the file

Suppose you need to send a file to someone else. You need to have the recipient's public key. It's up to you to decide exactly how to get the key. You can ask them to send the public key directly to you, or you can get it through the key server.

After receiving the other party's public key, import the public key into GPG.

$gpg-import yourfriends.key

These public keys, like the keys you create, come with name and email address information. Remember, in order for others to decrypt your files, they also need your public key. So export the public key and send it out.

Gpg-- export-a "Your Name" > your.key

Now you can start encrypting the files you want to send. It is similar to the previous steps, except that you need to designate yourself as the sender.

$gpg-e-u "Your Name"-r "Their Name" / tmp/test.txt

Thank you for reading! This is the end of this article on "how to use GPG to encrypt and decrypt files under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report