今天看啥  ›  专栏  ›  编程猫

cryptokit在python中轻松使用的密码工具

编程猫  · 掘金  ·  · 2018-01-15 10:03
cryptokit是基于cryptography的python第三方库,
旨在让大家在python中更轻松便捷的实现一些密码相关的操作

github地址

istommao/cryptokitgithub.com图标

安装cryptokit

pip install cryptokit

AES使用

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'

RSA使用

加密解密

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

签名验签

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()

生成pfx文件

from cryptokit import generate_pfx
pfx_data = generate_pfx(cert, friendly_name, private_key)

持续更新中...




原文地址:访问原文地址
快照地址: 访问文章快照