dm-crypt 模块使用内核设备映射器子系统,提供了一个处理设备数据加密和解密的低级映射。dm-crypt 来实现磁盘加密的功能。dnf install cryptsetup
dd 命令创建一个文件,该文件位于 /tmp/my_virtual_disk.img,大小为 1GB(1024MB)。dd if=/dev/zero of=/tmp/my_virtual_disk.img bs=1M count=1024
cryptsetup luksFormat /tmp/my_virtual_disk.img
/dev/mapper/my_encrypted_volume:cryptsetup luksOpen /tmp/my_virtual_disk.img my_encrypted_volume
mkfs.ext4 /dev/mapper/my_encrypted_volume
mkdir /mnt/my_encrypted_volumemount /dev/mapper/my_encrypted_volume /mnt/my_encrypted_volume
sudo cryptsetup status my_encrypted_volume
/dev/mapper/my_encrypted_volume is active and is in use.type: LUKS2cipher: aes-xts-plain64keysize: 512 bitskey location: keyringdevice: /dev/loop0loop: /tmp/my_virtual_disk.imgsector size: 4096offset: 32768 sectorssize: 2064384 sectorsmode: read/write
umount /mnt/my_encrypted_volumecryptsetup luksClose /dev/mapper/my_encrypted_volume
openssl 命令行的形式,介绍一下如何加解密文件。openssl 的版本:openssl version
OpenSSL 3.0.12 24 Oct 2023 (Library: OpenSSL 3.0.12 24 Oct 2023)
openssl enc -list
-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
-aes-128-cbc 为例,来说明该参数的具体含义:aes:表示使用 AES 对称加密算法,是一种加密算法名称。OpenSSL 一共提供了 9 种对称加密算法,其中 8 种是分组加密算法,仅有一种流加密算法是 RC4。8 种分组加密算法分别是 AES、DES、Blowfish、CAST、IDEA、RC2/5、SEED、Camellia。128:表示 128 位密钥长度。cbc:表示分组密码加密模式,常见模式有电子密码本模式(ECB)、加密分组链接模式(CBC)、加密反馈模式(CFB)和输出反馈模式(OFB)。# 该命令需要用户输入密码# -pbkdf2:表示使用 PBKDF2(Password-Based Key Derivation Function 2)算法进行密钥派生# -in file.txt:表示要加密的明文文件# -out file.enc:表示加密后的密文文件openssl enc -aes-256-cbc -pbkdf2 -in file.txt -out file.enc
enter aes-256-cbc encryption password:Verifying - enter aes-256-cbc encryption password:
openssl enc -aes-256-cbc -pbkdf2 -d -in file.enc -out file.txt -pass pass:<password>
# 生成 256 位的 AES 密钥openssl rand -base64 32 > mykey.txt# 使用密钥进行对称加密openssl enc -aes-256-cbc -pbkdf2 -in file.txt -out file.enc -pass file:mykey.txt# 使用相同的密钥进行解密openssl enc -aes-256-cbc -d -pbkdf2 -in file.enc -out plaintext.txt -pass file:mykey.txt
-K 选项指定加密使用的密钥,-iv 选项指定加密使用的初始向量:openssl enc -sm4 -in file.txt -K <32位十六进制密钥> -iv <32位十六进制IV> -out file.enc
openssl enc -d -sm4 -in file.enc -K <32位十六进制密钥> -iv <32位十六进制IV> -out file.raw
genrsa 子命令生成密钥对,密钥对是一个文件:# 生成的密钥长度是 2048 比特openssl genrsa -out mykey.pem 2048
Generating RSA private key, 2048 bit long modulus (2 primes)..................+++++.+++++e is 65537 (0x010001)
openssl rsa -in mykey.pem -pubout -out mypubkey.pem
writing RSA key
openssl rsa -pubin -in mypubkey.pem -text
openssl pkeyutl -encrypt -in plaintext.txt -inkey mypubkey.pem -pubin -out ciphertext.txt
plaintext.txt 是待加密的明文文件,mypubkey.pem 是公钥文件,ciphertext.txt 是加密后的密文文件。openssl pkeyutl -decrypt -in ciphertext.txt -inkey mykey.pem -out plaintext.txt
ciphertext.txt 是加密后的密文文件,mykey.pem 是私钥文件,plaintext.txt 是解密后的明文文件。openssl ecparam -genkey -name SM2 -out sm2.pem
openssl ec -in sm2.pem -pubout -out sm2.pub.pem
read EC keywriting EC key
openssl pkeyutl -encrypt -in plaintext.txt -inkey sm2.pub.pem -pubin -out ciphertext.txt
plaintext.txt 是明文文件,ciphertext.txt 是加密后的密文文件。openssl pkeyutl -decrypt -in ciphertext.txt -inkey sm2.pem -out plaintext.txt
ciphertext.txt 是密文文件,plaintext.txt 是解密后的明文文件。openssl 为例完成数字签名和验签。openssl genrsa -out rsaprivatekey.pem 2048
openssl rsa -in rsaprivatekey.pem -pubout -out rsapublickey.pem
plain.txt 文件使用 SHA-256 摘要算法和签名算法生成签名文件 signature.txt:echo "hello" >> plain.txtopenssl dgst -sha256 -sign rsaprivatekey.pem -out signature.txt plain.txt
openssl dgst -sha256 -verify rsapublickey.pem -signature signature.txt plain.txt
Verified OK
openssl dsaparam -out dsaparam.pem 1024
dsaprivatekey.pem:openssl gendsa -out dsaprivatekey.pem dsaparam.pem
openssl gendsa -out dsaprivatekey2.pem -des3 dsaparam.pem
Generating DSA key, 1024 bitsEnter PEM pass phrase:Verifying - Enter PEM pass phrase:
openssl dsa -in dsaprivatekey.pem -pubout -out dsapublickey.pem
echo "hello" > plain.txt# 进行签名,查看 signature.txt 可以发现有原始信息openssl dgst -sha256 -sign dsaprivatekey.pem -out signature.txt plain.txt# 验证签名openssl dgst -sha256 -verify dsapublickey.pem -signature signature.txt plain.txt
Verified OK
-rawin 参数或 SM2 签名上下文来指定摘要算法。openssl ecparam -genkey -name SM2 -out priv.key
# 生成私钥对应的公钥openssl ec -in priv.key -pubout -out pub.key
read EC keywriting EC key
openssl ec -in pub.key -pubin -text -noout
-rawin -sm3 指定原始输入和 SM3 摘要:# 准备待签名文件echo "hello" > plain.txt# 使用 SM2 私钥对文件签名,采用 SM3 摘要算法openssl pkeyutl -sign -in plain.txt -inkey priv.key -out signature.txt -rawin -digest sm3
openssl pkeyutl -verify -in plain.txt -inkey pub.key -pubin -rawin -digest sm3 -sigfile signature.txt
Signature Verified Successfully
# 生成私钥和自签名证书,有效期为 365 天openssl req -x509 -newkey rsa:2048 -keyout cakey.pem -out cacert.pem -days 365 -nodes
# 查看证书详细信息openssl x509 -in cacert.pem -text -noout# 查看证书的公钥openssl x509 -in cacert.pem -pubkey -noout
# PEM 格式转换为 DER 格式openssl x509 -in cacert.pem -outform DER -out cacert.der# DER 格式转换为 PEM 格式openssl x509 -in cacert.der -inform DER -outform PEM -out cacert.pem
# 生成 SM2 私钥openssl ecparam -genkey -name SM2 -out sm2key.pem# 使用 SM2 私钥生成自签名证书(需配合 SM3 摘要)openssl req -new -x509 -key sm2key.pem -out sm2cert.pem -days 365 -sm3 -nodes
文档反馈