项目背景#
在日常工作和学习过程中,经常需要快速进行数据加解密操作。然而,现有的加密工具如openssl虽然功能全面,但使用起来并不直观,特别是对于非专业人士来说。
传统openssl使用示例:
1
| echo -n "hello,world" | openssl enc -aes-256-cbc -salt -a -pass pass:yourpassword
|
这样的操作不仅容易出错,而且需要记忆大量命令和参数,增加了学习成本和使用难度。

因此,我设计了easy_encryption_tool——一款直观易用的加密命令行工具,旨在简化加密操作,降低使用门槛。


项目地址#
easy_encryption_tool
pip install easy-encryption-tool
工具特点#
设计理念#
- 易用性:简化命令和参数,降低学习成本
- 扩展性:支持未来功能扩展和算法更新
- 安全性:确保加密过程的安全性和数据完整性
核心功能#
- 对称加解密(AES-CBC、AES-GCM)
- 非对称加解密(RSA、ECC)
- 数字签名与验签
- 证书解析与验证
- HMAC计算
- 随机字符串生成
- 密钥对生成
输入输出支持#
- 输入:字符串、base64编码数据、文件
- 输出:文件、base64编码字节流
- 密钥格式:PEM、DER
安装与配置#
基本安装#
1
| pip install easy_encryption_tool
|
自动补全设置(zsh示例)#
1
2
3
4
5
6
| # 生成补全脚本
_EASY_ENCRYPTION_TOOL_COMPLETE=zsh_source easy_encryption_tool > ~/.easy_encryption_tool_complete.sh
# 添加到.zshrc
echo ". ~/.easy_encryption_tool_complete.sh" >> ~/.zshrc
source ~/.zshrc
|
功能详解#
帮助系统#
1
| easy_encryption_tool --help
|
支持的命令:
aes- AES加解密工具rsa- RSA加解密和签名验签ecc- ECC椭圆曲线操作hmac- HMAC消息验证码random-str- 随机字符串生成器cert-parse- 证书解析工具version- 版本信息
随机字符串生成#
基本用法:
1
2
3
4
5
| # 生成32字节随机字符串
easy_encryption_tool random-str -l 32
# 输出到文件
easy_encryption_tool random-str -l 37 -o test_random
|
输出示例:
1
2
3
| ------ 71a2d32b0816349f begin@2024-04-04_15:24:22.476 ------
write to test_random success
------ 71a2d32b0816349f took 0.299 milli-seconds to execute ------
|
AES对称加密#
加密模式支持#
- CBC模式 - 密码分组链接模式
- GCM模式 - 伽罗瓦/计数器模式,支持认证加密
基本加密示例#
1
2
3
4
5
6
7
8
| # 使用默认密钥加密字符串
easy_encryption_tool aes -m cbc -a encrypt -i "hello,world"
# 使用随机生成的密钥
easy_encryption_tool aes -m cbc -a encrypt -i "hello,world" -r
# 指定密钥和IV
easy_encryption_tool aes -m cbc -a encrypt -i "hello,world" -k "mykey" -v "myiv"
|
文件加解密#
1
2
3
4
5
| # 加密文件
easy_encryption_tool aes -m gcm -a encrypt -i input.txt -f -o encrypted.bin
# 解密文件
easy_encryption_tool aes -m gcm -a decrypt -i encrypted.bin -f -o decrypted.txt -t "auth_tag"
|
GCM模式特殊参数#
GCM模式需要额外的认证标签参数:
1
2
3
4
5
6
| # 加密
easy_encryption_tool aes -m gcm -a encrypt -i "hello,world"
# 输出包含auth_tag,解密时需要提供
# 解密(必须提供正确的auth_tag)
easy_encryption_tool aes -m gcm -a decrypt -i "cipher_text" -t "auth_tag_value"
|
HMAC计算#
基本用法:
1
2
3
4
5
6
7
8
| # 使用默认密钥
easy_encryption_tool hmac -i "hello,world"
# 指定密钥和哈希算法
easy_encryption_tool hmac -i "hello,world" -k "mykey" -h sha512
# 处理文件
easy加密_tool hmac -i data.txt -f
|
支持的哈希算法:
- sha224, sha256, sha384, sha512
- sha3_224, sha3_256, sha3_384, sha3_512
RSA非对称加密#
密钥对生成#
1
2
3
4
5
6
7
8
| # 生成2048位RSA密钥对
easy_encryption_tool rsa generate -f mykey
# 指定密钥长度和密码
easy_encryption_tool rsa generate -f securekey -s 4096 -p "mypassword"
# DER格式密钥
easy_encryption_tool rsa generate -f derkey -e der -s 3072
|
加密解密#
1
2
3
4
5
| # 加密(OAEP模式)
easy_encryption_tool rsa encrypt -f public.pem -i "secret message"
# 解密
easy_encryption_tool rsa decrypt -f private.pem -i "encrypted_data" -p "keypassword"
|
签名验签#
1
2
3
4
5
| # 创建签名(PSS模式)
easy_encryption_tool rsa sign -f private.pem -i "data to sign" -p "password"
# 验证签名
easy_encryption_tool rsa verify -f public.pem -i "original data" -s "signature"
|
ECC椭圆曲线加密#
密钥生成#
1
2
3
4
5
| # 生成ECC密钥对
easy_encryption_tool ecc generate -f myecckey -c secp256r1
# 指定曲线类型和密码
easy_encryption_tool ecc generate -f securekey -c secp384r1 -p "password"
|
ECDH密钥交换#
1
2
3
4
5
| # Alice生成共享密钥
easy_encryption_tool ecc ecdh -a alice_pub.pem -k alice_priv.pem -b bob_pub.pem
# Bob生成相同的共享密钥
easy_encryption_tool ecc ecdh -a bob_pub.pem -k bob_priv.pem -b alice_pub.pem
|
ECC签名验签#
1
2
3
4
5
| # 签名
easy_encryption_tool ecc sign -f private.pem -i "data" -h sha256
# 验签
easy_encryption_tool ecc verify -f public.pem -i "data" -s "signature"
|
证书解析#
1
2
3
4
5
| # 解析证书基本信息
easy_encryption_tool cert-parse -f certificate.cer
# 详细解析(包含扩展信息)
easy_encryption_tool cert-parse -f certificate.cer -v
|
错误处理#
工具提供了详细的错误信息,帮助用户快速定位问题:
常见错误示例#
1
2
3
4
5
6
7
8
9
10
11
| # 无效的加密模式
easy_encryption_tool aes -m invalid_mode -a encrypt -i "data"
# 错误:Invalid value for '-m' / '--mode'
# 文件权限问题
easy_encryption_tool aes -a encrypt -i "/root/protected.txt" -f
# 错误:file may not exist or may be unreadable
# Base64编码错误
easy_encryption_tool aes -a encrypt -i "invalid@base64" -e
# 错误:invalid b64 encoded data
|