tencent cloud

TencentOS Server

Encryption and Signature

다운로드
포커스 모드
폰트 크기
마지막 업데이트 시간: 2026-10-19 17:22:50
AI 번역
This document describes the common encryption and signing technologies in TencentOS Server, including disk partition encryption, file encryption and decryption, and digital signature and verification, helping you protect data confidentiality and integrity.

Encrypting a Disk Partition

Basic Concepts

Disk encryption is a technology that protects data security. Encrypting the data on the disk ensures that unauthorized users cannot access it, which helps prevent data leakage. Disk encryption can be applied to the entire disk (full disk encryption) or specific partitions (partition encryption).
LUKS (Linux Unified Key Setup): a specification for block device encryption that establishes a disk format for data and a password/key management policy. LUKS uses the kernel device mapper subsystem through the dm-crypt module to provide a low-level mapping that handles device data encryption and decryption.
cryptsetup: a partition encryption tool that implements disk encryption by calling dm-crypt in the kernel.

Creating an Encrypted Disk

Warning: Creating an encrypted disk requires formatting the disk partition, which will erase all data on the partition. Back up your data beforehand.
Install the partition tool:
dnf install cryptsetup

Create a virtual disk

Create a virtual disk to encrypt the logical volume in LVM. If LUKS is used to encrypt physical partitions, the current step can be skipped directly.
Use the dd command to create a file located at /tmp/my_virtual_disk.img with a size of 1 GB (1024 MB).
dd if=/dev/zero of=/tmp/my_virtual_disk.img bs=1M count=1024
Encrypt the disk with LUKS:
cryptsetup luksFormat /tmp/my_virtual_disk.img
Open the encrypted partition and create a mapping to /dev/mapper/my_encrypted_volume:
cryptsetup luksOpen /tmp/my_virtual_disk.img my_encrypted_volume
Format the mapped partition:
mkfs.ext4 /dev/mapper/my_encrypted_volume
Create a mount point and mount the partition:
mkdir /mnt/my_encrypted_volume
mount /dev/mapper/my_encrypted_volume /mnt/my_encrypted_volume

Viewing Encrypted Disk Status

sudo cryptsetup status my_encrypted_volume
Output example:
/dev/mapper/my_encrypted_volume is active and is in use.
type: LUKS2
cipher: aes-xts-plain64
keysize: 512 bits
key location: keyring
device: /dev/loop0
loop: /tmp/my_virtual_disk.img
sector size: 4096
offset: 32768 sectors
size: 2064384 sectors
mode: read/write
Unmount and disable the partition:
umount /mnt/my_encrypted_volume
cryptsetup luksClose /dev/mapper/my_encrypted_volume

File Encryption

File encryption is a technology that converts file content into an unreadable format to protect the confidentiality and security of files. By using an encryption algorithm, the original file can be converted into ciphertext, making the file content unreadable to anyone except authorized users.
File encryption typically uses symmetric or public-key encryption algorithms. A symmetric encryption algorithm uses the same key to encrypt and decrypt data, so the security of the key must be ensured. A public-key encryption algorithm uses a key pair (a public key and a private key), where the public key is used to encrypt data and the private key is used to decrypt data. Because the public key can be made public, public-key encryption algorithms are more secure.
File encryption can be used to protect various types of files, including text files, image files, audio files, and video files. Encrypted files can prevent unauthorized access and theft, thereby protecting personal privacy and business confidentiality.
OpenSSL is a full-featured cryptographic library that provides not only APIs for programming but also a powerful command line tool for common operations such as encryption and decryption, signature generation and verification, and certificate management. This section describes how to encrypt and decrypt files by using the openssl command line.

Introduction to OpenSSL

OpenSSL is developed in C, which gives it excellent cross-platform performance and allows it to run on different platforms. OpenSSL supports Linux, Windows, BSD, Mac, and other platforms, making it widely applicable.
OpenSSL implements multiple symmetric encryption algorithms (AES, DES, Blowfish, CAST, IDEA, RC2, RC4, and RC5), four asymmetric encryption algorithms (DH, RSA, DSA, and Elliptic Curve Cryptography (ECC)), multiple message digest algorithms (MD2, MD5, MDC2, SHA1, and RIPEMD), and key and certificate management. The OpenSSL license is a combination of the SSLeay license and the OpenSSL license, both of which are BSD-style licenses. According to the license terms, OpenSSL can be used for various commercial and non-commercial purposes.
The entire OpenSSL software package can be roughly divided into three main features: the cryptographic algorithm library, the SSL protocol library, and applications.
Note:
TencentOS Server 4 ships with OpenSSL 3.x by default. Compared with version 1.x, OpenSSL 3.x adopts the Provider architecture, delivering significant improvements in both performance and security.

Encrypting Files Using OpenSSL

Symmetric Encryption Algorithm

Symmetric encryption algorithms are typically used when a large amount of data needs to be encrypted, with both parties using the same key for encryption and decryption. The advantages of symmetric algorithms include openness, low computational overhead, high encryption speed, and high encryption efficiency. The disadvantages of symmetric algorithms include the large number of keys generated and the difficulty of key distribution.
Common symmetric algorithms include DES, 3DES, TDEA, Blowfish, RC2, RC4, RC5, IDEA, SKIPJACK, AES, and the SM1 and SM4 algorithms issued by the State Cryptography Administration.
Display the current version of openssl:
openssl version
Output example (TencentOS Server 4):
OpenSSL 3.0.12 24 Oct 2023 (Library: OpenSSL 3.0.12 24 Oct 2023)
Display the symmetric encryption algorithms supported by the system:
openssl enc -list
Output example:
-aes-128-cbc -aes-128-cfb -aes-128-cfb1
-aes-128-cfb8 -aes-128-ctr -aes-128-ecb
-aes-128-ofb -aes-192-cbc -aes-192-cfb
-aes-192-cfb1 -aes-192-cfb8 -aes-192-ctr
..........
-sm4 -sm4-cbc -sm4-cfb
-sm4-ctr -sm4-ecb -sm4-ofb
Take -aes-128-cbc as an example to illustrate the specific meaning of this parameter:
aes: indicates the use of the AES symmetric encryption algorithm and is the name of an encryption algorithm. OpenSSL provides a total of 9 symmetric encryption algorithms, of which 8 are block ciphers and only one stream cipher is RC4. The 8 block ciphers are AES, DES, Blowfish, CAST, IDEA, RC2/5, SEED, and Camellia.
128: indicates a 128-bit key length.
cbc: indicates the block cipher encryption mode. Common modes include Electronic Codebook (ECB), Cipher Block Chaining (CBC), Cipher Feedback (CFB), and Output Feedback (OFB).
Use the AES-256-CBC algorithm for symmetric encryption and decryption of files, and use a user-entered password for encryption:
# This command requires the user to input the password.
# -pbkdf2: indicates that the PBKDF2 (Password-Based Key Derivation Function 2) algorithm is used for key derivation.
# -in file.txt: indicates the plaintext file to be encrypted.
# -out file.enc: indicates the encrypted ciphertext file.
openssl enc -aes-256-cbc -pbkdf2 -in file.txt -out file.enc
After execution, you will be prompted to enter the password:
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
Use the same user password to decrypt:
openssl enc -aes-256-cbc -pbkdf2 -d -in file.enc -out file.txt -pass pass:<password>
Directly use the key for symmetric encryption of files:
# Generate a 256-bit AES key.
openssl rand -base64 32 > mykey.txt

# Use the key for symmetric encryption.
openssl enc -aes-256-cbc -pbkdf2 -in file.txt -out file.enc -pass file:mykey.txt

# Use the same key for decryption.
openssl enc -aes-256-cbc -d -pbkdf2 -in file.enc -out plaintext.txt -pass file:mykey.txt
Use the SM4 national cryptographic algorithm for symmetric encryption and decryption.
Encrypt, where the -K option specifies the key used for encryption, and the -iv option specifies the initialization vector used for encryption:
openssl enc -sm4 -in file.txt -K <32-bit hexadecimal key> -iv <32-bit hexadecimal IV> -out file.enc
Perform the decryption. The same key and initialization vector used for encryption are required:
openssl enc -d -sm4 -in file.enc -K <32-bit hexadecimal key> -iv <32-bit hexadecimal IV> -out file.raw

Asymmetric Encryption Algorithm

OpenSSL implements four asymmetric encryption algorithms in total: DH, RSA, DSA, and Elliptic Curve Cryptography (ECC). The DH algorithm is generally used for key exchange. The RSA algorithm can be used for both key exchange and digital signatures, and it can also be used for data encryption and decryption if you can tolerate its slow speed. The DSA algorithm is generally used only for digital signatures.
Here, we use the RSA algorithm and the Elliptic Curve Cryptography (ECC) algorithm to encrypt and decrypt files.
Encrypting and Decrypting Files with RSA
1. Use the genrsa subcommand to generate a key pair, which is a file:
# The generated key length is 2,048 bits.
openssl genrsa -out mykey.pem 2048
Output example:
Generating RSA private key, 2048 bit long modulus (2 primes)
..................+++++
.+++++
e is 65537 (0x010001)
2. Extract the public key from the key pair:
openssl rsa -in mykey.pem -pubout -out mypubkey.pem
Output example:
writing RSA key
3. Display the information of the public key:
openssl rsa -pubin -in mypubkey.pem -text
4. Encryption command:
openssl pkeyutl -encrypt -in plaintext.txt -inkey mypubkey.pem -pubin -out ciphertext.txt
Here, plaintext.txt is the plaintext file to be encrypted, mypubkey.pem is the public key file, and ciphertext.txt is the encrypted ciphertext file.
5. Decryption command:
openssl pkeyutl -decrypt -in ciphertext.txt -inkey mykey.pem -out plaintext.txt
Here, ciphertext.txt is the encrypted ciphertext file, mykey.pem is the private key file, and plaintext.txt is the decrypted plaintext file.
Encrypting and Decrypting Files with SM2
SM2 is an asymmetric encryption algorithm based on Elliptic Curve Cryptography (ECC), issued by the State Cryptography Administration.
1. Generate an SM2 key pair:
openssl ecparam -genkey -name SM2 -out sm2.pem
2. Extract the SM2 public key:
openssl ec -in sm2.pem -pubout -out sm2.pub.pem
Output example:
read EC key
writing EC key
3. SM2 encryption:
openssl pkeyutl -encrypt -in plaintext.txt -inkey sm2.pub.pem -pubin -out ciphertext.txt
Here, plaintext.txt is the plaintext file, and ciphertext.txt is the encrypted ciphertext file.
4. SM2 decryption:
openssl pkeyutl -decrypt -in ciphertext.txt -inkey sm2.pem -out plaintext.txt
Here, ciphertext.txt is the ciphertext file, and plaintext.txt is the decrypted plaintext file.

Signature and Verification

The digital signature is a security authentication technology derived from the development of public key cryptography, primarily used to provide services such as entity authentication, authenticated key transport, and authenticated key agreement. In simple terms, the message sender can add a code as a signature to represent the characteristics of a message file. If the file changes, the corresponding digital signature must also change, which means that different files get different digital signatures after encoding. When using the digital signature technology based on the RSA public key algorithm, if cryptographic attackers attempt to tamper with or impersonate the original message, they cannot obtain the correct digital signature because the private key of the message sender cannot be obtained. If the recipient tries to deny or forge the signature, a notary party can verify the message using the correct digital signature, determining whether the message comes from the sender. This approach effectively guarantees the integrity of the message and verifies the authenticity and non-repudiation of the sender's identity.
Digital signatures can be categorized into the following types:
RSA digital signature: RSA is one of the earliest digital signature algorithms, and it uses an RSA key pair for digital signing and verification. The RSA digital signature algorithm offers high security and reliability, but its high computational complexity makes it unsuitable for signing and verifying large-scale data.
DSA digital signature: DSA is a digital signature algorithm based on the discrete logarithm problem, and it uses a DSA key pair for digital signing and verification. The DSA digital signature algorithm offers high security and fast computation speed, making it suitable for signing and verifying large-scale data.
ECDSA digital signature: ECDSA is a digital signature algorithm based on elliptic curves, and it uses an ECC key pair for digital signing and verification. The ECDSA digital signature algorithm offers high security and fast computation speed, making it suitable for signing and verifying large-scale data.

Digital Signing Process

Signature generation process: The sender signs the message using their own private key.
Signature verification process: The receiver verifies the signature using the sender's public key.
This section still uses openssl as an example to complete digital signing and signature verification.

RSA Digital Signature

1. Generate an RSA key pair with a key length of 2,048 bits:
openssl genrsa -out rsaprivatekey.pem 2048
2. Extract the public key from the key pair:
openssl rsa -in rsaprivatekey.pem -pubout -out rsapublickey.pem
3. Generate the signature file signature.txt for the plain.txt file using the SHA-256 digest algorithm and the signature algorithm:
echo "hello" >> plain.txt
openssl dgst -sha256 -sign rsaprivatekey.pem -out signature.txt plain.txt
4. To verify the signature file using the same digest algorithm and signature algorithm, compare the signature file with the original file:
openssl dgst -sha256 -verify rsapublickey.pem -signature signature.txt plain.txt
Output upon successful verification:
Verified OK

DSA Digital Signature

1. Generating Parameter and Key Pair Files

1. Generate a parameter file, similar to a DH parameter file (a DH parameter file is a file that contains the parameters required by the Diffie-Hellman key exchange algorithm):
openssl dsaparam -out dsaparam.pem 1024
2. Generate a key pair dsaprivatekey.pem from the parameter file:
openssl gendsa -out dsaprivatekey.pem dsaparam.pem
3. Encrypt the private key file using the DES3 algorithm:
openssl gendsa -out dsaprivatekey2.pem -des3 dsaparam.pem
Output example:
Generating DSA key, 1024 bits
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
4. Extract the public key from the key pair file:
openssl dsa -in dsaprivatekey.pem -pubout -out dsapublickey.pem

2. Generating and Verifying Signatures

echo "hello" > plain.txt

# Perform signing. After checking signature.txt, you can find the original information.
openssl dgst -sha256 -sign dsaprivatekey.pem -out signature.txt plain.txt

# Verify the signature.
openssl dgst -sha256 -verify dsapublickey.pem -signature signature.txt plain.txt
Output upon successful verification:
Verified OK

ECDSA Digital Signature Based on SM2

SM2 signatures use the SM3 digest algorithm as the default hash. In OpenSSL 3.x, SM2 signing and verification require the -rawin parameter or an SM2 signature context to specify the digest algorithm.

1. Generating an SM2 Key Pair

Generate an SM2 private key directly without generating an ECC parameter file in advance:
openssl ecparam -genkey -name SM2 -out priv.key

2. Splitting the Key to Obtain the Public Key

# Generate the public key corresponding to the private key.
openssl ec -in priv.key -pubout -out pub.key
Output example:
read EC key
writing EC key
Display the information of the public key:
openssl ec -in pub.key -pubin -text -noout

3. SM2 Signing

Sign the file using the SM2 algorithm. In OpenSSL 3.x, specify the raw input and SM3 digest by using -rawin -sm3:
# Prepare the file to be signed.
echo "hello" > plain.txt

# Sign the file using the SM2 private key and the SM3 digest algorithm.
openssl pkeyutl -sign -in plain.txt -inkey priv.key -out signature.txt -rawin -digest sm3

4. SM2 Verification

Verify the signature using the SM2 public key:
openssl pkeyutl -verify -in plain.txt -inkey pub.key -pubin -rawin -digest sm3 -sigfile signature.txt
Output upon successful verification:
Signature Verified Successfully

Introduction to Certificate Operations

OpenSSL provides comprehensive X.509 certificate management features, including certificate issuance, viewing, verification, and conversion.

1. Generating a Self-Signed Certificate

# Generate a private key and a self-signed certificate with a validity period of 365 days.
openssl req -x509 -newkey rsa:2048 -keyout cakey.pem -out cacert.pem -days 365 -nodes

2. Viewing Certificate Information

# View certificate details
openssl x509 -in cacert.pem -text -noout

# View the public key of the certificate.
openssl x509 -in cacert.pem -pubkey -noout

3. Converting Certificate Formats

# Convert PEM format to DER format.
openssl x509 -in cacert.pem -outform DER -out cacert.der

# Convert DER format to PEM format.
openssl x509 -in cacert.der -inform DER -outform PEM -out cacert.pem

4. Generating a Self-Signed Certificate with SM2

In OpenSSL 3.x, you can issue certificates using the SM2 algorithm:
# Generate an SM2 private key.
openssl ecparam -genkey -name SM2 -out sm2key.pem

# Generate a self-signed certificate using the SM2 private key (requires the SM3 digest algorithm).
openssl req -new -x509 -key sm2key.pem -out sm2cert.pem -days 365 -sm3 -nodes


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백