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

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

内容简介: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中实现加密解密》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Pro Django

Pro Django

Marty Alchin / Apress / 2008-11-24 / USD 49.99

Django is the leading Python web application development framework. Learn how to leverage the Django web framework to its full potential in this advanced tutorial and reference. Endorsed by Django, Pr......一起来看看 《Pro Django》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器