python加密库Crypto AES和rsa
标签搜索
侧边栏壁纸
  • 累计撰写 25 篇文章
  • 累计收到 31 条评论

python加密库Crypto AES和rsa

z
z
2021-12-07 / 0 评论 / 18 阅读 / 正在检测是否收录...

先安装Crypto

pip install pycryptodome

演示mode模式为ecb的
key要16位或者
key和data都要byte类型

import base64

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad


def encrypt_data(text):
    cry = AES.new(key=("227V2xYeHTARSh1R").encode("utf-8"), mode=AES.MODE_ECB)
    res=(cry.encrypt(pad(text.encode("utf-8"),AES.block_size, style='pkcs7')))
    print(base64.b64encode(res).decode("utf-8"))

def decrypt_data(text):
    key = ("aiding6666666666").encode("utf-8")
    cry = AES.new(key=key, mode=AES.MODE_ECB)
    result = str(cry.decrypt(base64.b64decode(text)),"utf-8")
    return result

Aes key长度不固定加密实现-库推荐-aes-everywhere
安装
pip install aes-everywhere
用法


from AesEverywhere import aes256
# encryption
encrypted = aes256.encrypt('TEXT', 'key')
print(encrypted)
# decryption
print(aes256.decrypt(encrypted, 'key'))

rsa


from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
pk="-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAy5R1R2yM5jPPvkO2F47qVqMkYj7o92DF8y1yMkCSxY1WwqG0\ndCdUZTnaoBuAz99wGt55oGLcdalV71nPUiGWs/b6GzVN5v72baz/Q2OxHtkrFKqL\nVX16LW31cW9hAntN84RCbvTeB0MNV+SHmXjIf17OQLCtDKHBZWZ5NKyqFstO+KOd\nu32d2jsw+DT5lOBzDUBk/wUw2KyFJVx7eK6sSXEyWqBk2nxMRDNYixIEN1V1EBSq\nf+OwKK5Mxi04r38+Qog8z03/t/u6CfAOWVmi+MdrD1VHXv/P7bnFlgRcLzKwK1QL\nTSLBE1PrMmNNj0oRjByhMoI9tY5X6mRBqLyDhwIDAQABAoIBAGO++RmGO6D9CNAJ\n4Bm52eKaK5UBiubOIR8NiNL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p3Cnp4oL+7\nBI4AlXlKItV37rJIjZxQgLWhGoTZPplZaW4ooJCFJbazce5ua5fnsFS0oXhDN7uw\njaq+v5t8G6gFS09hEa4kz9O53t/7UGuQqh0Bxb0cJ9iNeAlhagvBDQ==\n-----END RSA PRIVATE KEY-----"

# rsa长加密
def rsa_long_encrypt(msg, pub_key_str, length=100):
    """
    单次加密串的长度最大为 (key_size/8)-11
    1024bit的证书用100, 2048bit的证书用 200
    """
    pubobj = RSA.importKey(pub_key_str)
    pubobj = Cipher_pkcs1_v1_5.new(pubobj)
    res = []
    for i in range(0, len(msg), length):
        res.append(
            str(
                base64.b64encode(pubobj.encrypt(
                    msg[i:i + length].encode(encoding="utf-8"))), 'utf-8'
            )
        )

    return "".join(res)


if __name__ == '__main__':
    str1='{"currency":"USD","departureDate":"2022-09-01","daysBeforeDeparture":0,"daysAfterDeparture":0,"departurePlace":"BKK","arrival":"FUK","oneway":1,"adultCount":1,"childCount":0,"infantCount":0,"returnDate":"","requestId":"1GOL7USNECUC-1660567920922","sessionId":"","_signature":"d5f6afe8c65d58543e3baf55518939a4c4d509ae2d6ee97a74f0d7059f9b9416"}'
    pubkey = '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAok58IrYXjeFjb6hPgrcv\nKis43ARDVIqowS2AJKivDp4+8uKDCWnjzBZTsuVvwKPzvVCxBzON2/DPpHU3wnRt\ndKSVzWju7HMKhuLxe04FsVw8+xvZTmguBj4jTczNLSLjK13lQr46J8j7JrmVUlPq\nGxIL/Bd3HNAIFuarZQkDsgx5fvdNrMbmT4edr1b3A8wRkhfo9tuE5Tmlx0YVUwyb\nzcI6hgLggCfNwwaClXyBt08NbGSIBcKYKjiQErND0EOnWcGyto7EhkpgGRfAeESo\n3hbmsiabThLd4t9iOWVHFSl+7B0q+1IGFjSo9qkvNdMUI4ZYdIKq+nCHufpuFMl7\nSwIDAQAB\n-----END PUBLIC KEY-----'
    print(str1)
    cipher_text = rsa_long_encrypt(str1,pubkey)
    print(cipher_text)

# rsa长解密
  def rsa_long_decrypt(msg,priv_key_str,  length=172):
        """
        1024bit的证书用128,2048bit证书用256位
        """
        privobj = RSA.importKey(priv_key_str)
        privobj = Cipher_pkcs1_v1_5.new(privobj)
        res = []
        for i in range(0, len(msg), length):
            res.append(
                str(
                    privobj.decrypt(
                        base64.b64decode(msg[i:i + length])
                        , 'xyz'), 'utf-8'
                )
            )
        print(res)
        return "".join(res)
  
# 解密
def rsaDecrypt(text):
    decodeStr = base64.b64decode(text)  # cipher_text是上面rsa加密的内容
    prikey = Cipher_pkcs1_v1_5.new(RSA.importKey(pk))
    encry_text = prikey.decrypt(decodeStr, b'rsa')
    encry_value = encry_text.decode('utf8')
    print(encry_value)
0

评论 (0)

取消