For security reasons in work and life, some important files are usually encrypted and backed up or encrypted and saved. How to do it in Linux? This article will introduce you to common methods of encrypting files in Linux.
01Use vim/vi encryption
Advantages: After encryption, if you don’t know the password, you won’t see the plain text, including the root user; Disadvantages: It is obvious that others know that the encryption is encrypted, which is easy for others to destroy the encrypted files, including content destruction and deletion;
Vim encryption example
First use vim to create a new file
vim Big cousin.txt Press i to enter the editing mode, press ESC after entering the content, then enter :X (note that it is capitalized X), and press Enter;
At this time, the system prompts you to enter your password, save it twice and exit after :wq. Now this file has been encrypted;
Use cat or more to view the file content, and it will be displayed as garbled; use vim/vi to re-edit this file, and it will be prompted to enter the password. If the entered password is incorrect, it will also be displayed as garbled!
Similarly, when we use vim to edit the document, we will prompt for a password. Only after entering the correct password can you edit the document.
So, how to remove the encryption of the file? We only need to enter: set key=Enter and save the file!
02Use gzexe encryption
gzexe is an encryption program that comes with the system. It not only encrypts but also compresses files. Advantages: Simple and convenient encryption. Disadvantages: You cannot set your own password, it is easy to crack.
Encryption/Decryption
gzexe 22.txt #Encryption
gzexe -d 22.txt #Decryption
Use the tar command to encrypt, compress and decompress files
This is very simple, similar to adding a password to a compressed package under Windows.
Encryption
tar -czf - filename | openssl des3 -salt -k passwd | dd of=filename.des3 command description:
In the tar -czf - filename command, -zc specifies that the given file is compressed using gzip. If you want to use other compression commands, you can use the corresponding options instead. -f - means to write the created archive file to standard output. The tar command can treat - as a file name and perform some special processing. It will be explained in detail later. filename is a packaged and compressed file name, which can provide multiple file names or directory names. The openssl des3 -salt -k passwd command specifies encryption using the des3 algorithm, and -k passwd specifies encryption and encryption, and you can modify passwd to other passwords. The dd of=filename.des3 command specifies that the encrypted file name is filename.des3, which can be modified to other file names.
Encryption Example
The following command encrypts and compresses the filename file (22.txt) to generate a bbskali.des3 encrypted compressed file, 666666 is the encrypted password
tar -zcf - 22.txt |openssl des3 -salt -k 666666 | dd of=22.txt.des3
Decryption
dd if=22.txt.des3 |openssl des3 -d -k 666666 | tar zxf - dd if=22.txt.des3 The command specifies reading the content of the 22.txt.des3 file. The openssl des3 -d -k 666666 command means decryption using the des3 algorithm.
The content after decryption is the archive file content generated by the previous tar command, which will be written to the standard output and passed to the standard input of the subsequent tar command through a pipeline. tar zxf - The command indicates that the archive file contents to be extracted are read from standard input, and the extracted files will be written to the local area.
03 ZIP encryption
Compared with tar, zip encryption is very simple.
Use the command zip -e filename.zip filename to prompt to enter the password, enter the password twice. This file is encrypted.
zip -e 22.txt.zip 22.txt
Decompression
After executing the command, you will be prompted to enter your password.
unzip 22.txt.zip
Summary
File encryption is an indispensable part of our work. The encryption methods and methods used by unused tools have their own advantages. The best thing for you is!
Recommended Comments