Home > System Tutorial > LINUX > body text

How to use OpenSSL to encrypt and decrypt files

PHPz
Release: 2024-06-20 08:40:18
Original
952 people have browsed it

如何运用OpenSSL 对文件进行加密和解密

OpenSSL uses cryptographic methods to encrypt or decrypt files

1. Use openssl to encrypt a file (data.zip is the original file, back.zip is the encrypted file)

# openssl enc -e -aes256 -in data.zip -out back.zip
Copy after login

Explanation: enc means symmetric encryption or decryption of files, -e means encryption of a file, -aes256 means encryption using the aes256 algorithm, -in means the file that needs to be encrypted, -out means the file generated after encryption new file. During the encryption process, you will be asked to enter an encryption password. Enter it twice to complete the encryption of the file

2. Use openssl to decrypt a file (back.zip is the encrypted file, data.zip is the decrypted file)

# openssl enc -d -aes256 -in back.zip -out data.zip
Copy after login

Explanation: enc means symmetric encryption or decryption of the file, -d means decryption of the file, -aes256 means decryption using the aes256 algorithm, -in means the file that needs to be decrypted, -out means the new file generated after decryption file, when decrypting a file, you will be asked to enter the password set when encrypting the file to decrypt it.

OpenSSL uses key mode to encrypt or decrypt files

1. First you need to use openssl to generate a 2048-bit key rsa.key file (rsa.key key file contains the private key and public key)

# openssl genrsa -out rsa.key 2048
Copy after login

2. Then extract the public key pub.key

from the rsa.key key file
# openssl rsa -in rsa.key -pubout -out pub.key
Copy after login

3. Use the pub.key public key to encrypt a file (data.zip is the original file, back.zip is the encrypted file)

# openssl rsautl -encrypt -inkey pub.key -pubin -in data.zip -out back.zip
Copy after login

4. Use the rsa.key private key to decrypt a file (back.zip is the encrypted file, data.zip is the decrypted file)

# openssl rsautl -decrypt -inkey rsa.key -in back.zip -out data.zip
Copy after login

Finally, we use the OpenSSL tool to encrypt and store all backup data files to ensure that the business system data is protected and leaked. We can also use other encryption tools such as GPG, VeraCrypt, and trueCrypt to encrypt data, but in comparison, using OpenSSL is more convenient because almost every Linux distribution comes with the OpenSSL software package pre-installed.

The above is the detailed content of How to use OpenSSL to encrypt and decrypt files. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!