cryptokit 是基于 cryptography 的 python 第三方库,
2018-01-15
zhuanlan.zhihu.com
旨在让大家在 python 中更轻松便捷的实现一些密码相关的操作
github 地址
istommao/cryptokitgithub.com
安装 cryptokit
pip install cryptokit
AES 使用
RSA 使用
from cryptokit import AESCrypto
message = "hello cryptokit"
crypto = AESCrypto('WDMG1e38igW53YuxkE0SsKUDeLbULAtL', 'm2VYHdx41zRgvg6f')
data = crypto.encrypt(message, mode='cbc')
# b'\xaa<\x9d\xe9\xde\x0b\xd7\xe9\xfd\xac\xfc\xdd\x9f\xe2V\xd4'
crypto.decrypt(data)
# 'hello cryptokit'
加密解密
签名验签
from cryptokit import RSACrypto
private_key = RSACrypto.generate_private_key(2048)
public_key = private_key.public_key()
message = 'Hello cryptokit'
ciphertext = RSACrypto.encrypt(message, public_key, algorithm='sha256')
plaintext = RSACrypto.decrypt(ciphertext, private_key, algorithm='sha256')
plaintext == message
# True
(PFX 文件相关使用)
from cryptokit import RSACrypto
private_key = RSACrypto.generate_private_key(2048)
public_key = private_key.public_key()
message = 'Hello cryptokit'
signature = RSACrypto.sign(message, private_key, algorithm='sha256')
result = RSACrypto.verify(message, signature, public_key, algorithm='sha256')
# result = True
加载 pfx 文件
生成 pfx 文件
from cryptokit import load_pfx
pfx_file = 'pfx file path'
pkcs12 = load_pfx(pfx_file, password='password')
# 获取证书
cert = pkcs12.get_certificate()
# 获取公钥
pubkey = get_pubkey_from_pfx(pfx_file, password='password')
# or use cert get pubkey
pubkey = cert.get_pubkey().to_cryptography_key()
持续更新中...
from cryptokit import generate_pfx
pfx_data = generate_pfx(cert, friendly_name, private_key)
来源: https://juejin.im/entry/5a5c65855188257332019907