python——rsa和aes加解密

Posted xgmin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python——rsa和aes加解密相关的知识,希望对你有一定的参考价值。

python操作rsa和aes加解密
封装了一个python的操作rsa和aes的加解密类

首先pip装库
pip install pycrypto
由于pycrypto多年未更新维护,也可以安装 PyCryptodome

代码:

-- 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_CBC

def 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().read

def 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_text

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

以上是关于python——rsa和aes加解密的主要内容,如果未能解决你的问题,请参考以下文章

PHP如何实现AES加解密

python-aes(ECB)加解密-支持16/32位

java RSA加解密以及用途

autojs的AES和RSA加解密。全方位保护您的源码

des和aes 加解密算法具体步骤?有例子最好

Android Okhttp/Retrofit网络请求加解密实现方案