cryptokit更轻松的在python中实现加密解密

栏目: Python · 发布时间: 8年前

内容简介:cryptokit更轻松的在python中实现加密解密

github 地址: https://github.com/istommao/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

pkcs12 (PFX文件相关)

from cryptokit import load_pfx, get_pubkey_from_pfx

# 加载pfx文件
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)

以上所述就是小编给大家介绍的《cryptokit更轻松的在python中实现加密解密》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Artificial Intelligence

Artificial Intelligence

Stuart Russell、Peter Norvig / Pearson / 2009-12-11 / USD 195.00

The long-anticipated revision of this #1 selling book offers the most comprehensive, state of the art introduction to the theory and practice of artificial intelligence for modern applications. Intell......一起来看看 《Artificial Intelligence》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

MD5 加密
MD5 加密

MD5 加密工具