案头见蠹鱼,犹胜凡俦侣。这篇文章主要讲述python——rsa和aes加解密相关的知识,希望能为你提供帮助。
python操作rsa和aes加解密
封装了一个python的操作rsa和aes的加解密类
首先pip装库
pip install pycrypto
由于pycrypto多年未更新维护,也可以安装 PyCryptodome
【python——rsa和aes加解密】代码:
-- coding:utf-8 --import base64
from Crypto import Random
from Crypto.Hash import SHA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
#AES加解密,用于本地加密数据库密码和rsa private key
aes_salt = "
opquweoijuqowieh"
class Aes():
def __init__(self, key=aes_salt):
self.key = key
self.mode = AES.MODE_CBCdef encrypt(self, text):
cryptor = AES.new(self.key, self.mode, b\'0000000000000000\')
length = 16
count = len(text)
if count <
length: add = (length-count) text = text + (\'\\0\' * add) elif count >
length:
add = (length-(count % length))
text = text + (\'\\0\' * add)
self.ciphertext = cryptor.encrypt(text)
return b2a_hex(self.ciphertext)def decrypt(self, text):
cryptor = AES.new(self.key, self.mode, b\'0000000000000000\')
plain_text = cryptor.decrypt(a2b_hex(text))
return plain_text.rstrip(\'\\0\')
#rsa加解密,用于cookie的加解密
class Rsa():
def __init__(self):
self.random_generator = Random.new().readdef gen_rsa_keys(self):
rsa = RSA.generate(1024, self.random_generator)
private_pem = rsa.exportKey()
public_pem = rsa.publickey().exportKey()
return (private_pem, public_pem)def crypto(self, public_key, text):
rsakey = RSA.importKey(public_key)
cipher = Cipher_pkcs1_v1_5.new(rsakey)
crypto_text = base64.b64encode(cipher.encrypt(text))
return crypto_textdef decrypt(self, private_key, text):
rsakey = RSA.importKey(private_key)
cipher = Cipher_pkcs1_v1_5.new(rsakey)
decrypt_text = cipher.decrypt(base64.b64decode(
text), self.random_generator)
return decrypt_text
if name == \'main\':
rsa = Rsa()
aes = Aes()
(pri,pub)=rsa.gen_rsa_keys()
jm = rsa.crypto(pub, " fsagjdak***1343129" )
print rsa.decrypt(pri, jm)
推荐阅读
- 一文搞懂如何使用Node.js进行TCP网络通信
- 技术栈(springboot2.x,vue,activiti5.22,mysql,带工作流系统)
- Kubernetes应用Pod固定IP之kruise
- CentOS7使用ansible部署Ceph(Nautilus)
- 网络--链路聚合,vtp
- 如何在wordpress org主题目录下批准wordpress主题()
- 如何修改WordPress网站的URL()
- 如何使用Ajax在WordPress中添加分页(我有需要调试的代码)()
- 如何使用WordPress Customizer API在自定义WordPress主题中添加图像滑块