測試環境
Virtual Box,AnolisOS-8.6-x86_64-minimal.iso,4 vCPU, 8G RAM, 60 vDisk。最小化安裝。需聯網。
系統環境
關閉防火墻
systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld
selinux關閉
cat /etc/selinux/config
安裝Python39
dnf install -y python39 python39-pip
配置國內pip源
mkdir -p ~/.pip
touch ~/.pip/pip.conf
vi ~/.pip/pip.conf
[global]
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple/
安裝easy_gmssl
安裝依賴
dnf install -y gcc cmake
安裝easy_gmssl庫
pip3 install easy_gmssl
安裝GmSSL 3.1.1
tar -zxvf GmSSL-3.1.1.tar.gz
cd GmSSL-3.1.1
mkdir build
cd build
cmake ..
make
make install
vi /etc/ld.so.conf,添加一行:
/usr/local/lib
加載動態鏈接
ldconfig
驗證版本
gmssl version
生成公鑰和私鑰
gmssl sm2keygen -pass 123456 -out sm2_private.pem -pubout sm2_public.pem
基于easy_gmssl國密算法的加解密驗簽小腳本
from easy_gmssl import EasySM2SignKey, EasySM2VerifyKey, EasySm4CBC, EasySM3Digest
import os
import logging# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')def sm4_encrypt(key, iv, plaintext):"""使用SM4算法進行加密:param key: 密鑰,長度為16字節:param iv: 初始化向量,長度為16字節:param plaintext: 明文數據:return: 加密后的密文"""sm4 = EasySm4CBC(key, iv, True)ciphertext = sm4.Update(plaintext) + sm4.Finish()return ciphertextdef sm4_decrypt(key, iv, ciphertext):"""使用SM4算法進行解密:param key: 密鑰,長度為16字節:param iv: 初始化向量,長度為16字節:param ciphertext: 密文數據:return: 解密后的明文"""sm4 = EasySm4CBC(key, iv, False)plaintext = sm4.Update(ciphertext) + sm4.Finish()return plaintextdef sm3_hash(data):"""使用SM3算法進行哈希計算:param data: 待哈希的數據:return: 哈希值"""sm3 = EasySM3Digest()sm3.UpdateData(data)hash_value, _, _ = sm3.GetHash()return hash_value.hex()def sm2_sign(private_key_path, password, data):"""使用SM2算法生成數字簽名:param private_key_path: 私鑰文件路徑:param password: 私鑰文件的密碼:param data: 待簽名的數據:return: 數字簽名"""sm2_signer = EasySM2SignKey(pem_private_key_file=private_key_path, password=password)sm2_signer.UpdateData(data)signature = sm2_signer.GetSignValue()return signature.hex()def sm2_verify(public_key_path, data, signature):"""使用SM2算法驗證數字簽名:param public_key_path: 公鑰文件路徑:param data: 待驗證的數據:param signature: 數字簽名:return: 驗證結果,True表示驗證通過,False表示驗證失敗"""sm2_verifier = EasySM2VerifyKey(pem_public_key_file=public_key_path)sm2_verifier.UpdateData(data)return sm2_verifier.VerifySignature(bytes.fromhex(signature))def encrypt_file(input_file_path, output_file_path, key, iv):"""加密文件:param input_file_path: 待加密文件路徑:param output_file_path: 加密后文件輸出路徑:param key: SM4算法密鑰:param iv: SM4算法初始化向量"""with open(input_file_path, 'rb') as f:plaintext = f.read()ciphertext = sm4_encrypt(key, iv, plaintext)with open(output_file_path, 'wb') as f:f.write(ciphertext)logging.info(f"文件加密完成,輸出路徑:{output_file_path}")def decrypt_file(input_file_path, output_file_path, key, iv):"""解密文件:param input_file_path: 待解密文件路徑:param output_file_path: 解密后文件輸出路徑:param key: SM4算法密鑰:param iv: SM4算法初始化向量"""with open(input_file_path, 'rb') as f:ciphertext = f.read()plaintext = sm4_decrypt(key, iv, ciphertext)with open(output_file_path, 'wb') as f:f.write(plaintext)logging.info(f"文件解密完成,輸出路徑:{output_file_path}")def sign_file(private_key_path, password, input_file_path, output_file_path):"""對文件生成數字簽名:param private_key_path: SM2算法私鑰文件路徑:param password: 私鑰文件的密碼:param input_file_path: 待簽名文件路徑:param output_file_path: 數字簽名輸出路徑"""with open(input_file_path, 'rb') as f:data = f.read()sign = sm2_sign(private_key_path, password, data)with open(output_file_path, 'w') as f:f.write(sign)logging.info(f"數字簽名生成完成,輸出路徑:{output_file_path}")def verify_file_signature(public_key_path, input_file_path, sign_file_path):"""驗證文件的數字簽名:param public_key_path: SM2算法公鑰文件路徑:param input_file_path: 待驗證文件路徑:param sign_file_path: 數字簽名文件路徑:return: 驗證結果"""with open(input_file_path, 'rb') as f:data = f.read()with open(sign_file_path, 'r') as f:sign = f.read()result = sm2_verify(public_key_path, data, sign)return resultdef check_file_integrity(input_file_path):"""檢查文件的完整性:param input_file_path: 待檢查文件路徑:return: 文件的SM3哈希值"""with open(input_file_path, 'rb') as f:data = f.read()hash_value = sm3_hash(data)return hash_valueif __name__ == "__main__":# 示例使用input_file_path = 'example.txt' # 待處理文件路徑encrypted_file_path = 'encrypted_example.enc' # 加密后文件路徑decrypted_file_path = 'decrypted_example.txt' # 解密后文件路徑sign_file_path = 'sign_example.txt' # 數字簽名文件路徑key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10' # SM4算法密鑰iv = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10' # SM4算法初始化向量private_key_path = '/root/sm2_private.pem' # SM2算法私鑰文件路徑public_key_path = '/root/sm2_public.pem' # SM2算法公鑰文件路徑password = "123456" # 私鑰文件的密碼# 加密文件encrypt_file(input_file_path, encrypted_file_path, key, iv)# 解密文件decrypt_file(encrypted_file_path, decrypted_file_path, key, iv)# 生成數字簽名sign_file(private_key_path, password, input_file_path, sign_file_path)# 驗證數字簽名verify_result = verify_file_signature(public_key_path, input_file_path, sign_file_path)logging.info(f"數字簽名驗證結果:{verify_result}")# 檢查文件完整性hash_value = check_file_integrity(input_file_path)logging.info(f"文件的SM3哈希值:{hash_value}")