python加密解密(AES、DES、RSA)

一、RSA

python3

# -*-coding:utf-8 -*-

import rsa
from binascii import b2a_hex, a2b_hex

class rsacrypt():
    def __init__(self, pubkey, prikey):
        self.pubkey = pubkey
        self.prikey = prikey

    def encrypt(self, text):
        self.ciphertext = rsa.encrypt(text.encode("utf-8"), self.pubkey)
        # 把加密后的字符串转化为16进制字符串
        return b2a_hex(self.ciphertext)

    def decrypt(self, text):
        decrypt_text = rsa.decrypt(a2b_hex(text), prikey)
        return decrypt_text.decode()


if __name__ == '__main__':
    pubkey, prikey = rsa.newkeys(256)
    rs_obj = rsacrypt(pubkey, prikey)
    text='张三'
    ency_text = rs_obj.encrypt(text)
    print("加密后:%s" % ency_text)
    print("解密后:%s" % rs_obj.decrypt(ency_text))

二、AES

python2,使用python3时,中文会报错,暂未解决,并且python3导包路径为

from Cryptodome.Cipher import AES
# -*-coding:utf-8 -*-
import sys

from Crypto.Cipher import AES

reload(sys)
sys.setdefaultencoding("utf-8")

class AesCrypto():
    def __init__(self, key, IV):
        self.key = key
        self.iv = IV
        self.mode = AES.MODE_CBC

    # 加密函数,text参数的bytes类型必须位16的倍数,不够的话,在末尾添加"\0"(函数内以帮你实现)
    def encrypt(self, text):
        cryptor = AES.new(self.key, self.mode, self.iv)  # self.key的bytes长度是16的倍数即可, self.iv必须是16位
        length = 16
        count = len(text)
        if (count % length != 0):
            add = length - (count % length)
        else:
            add = 0

        text = text + ("\0".encode() * add)  # 这里的"\0"必须编码成bytes,不然无法和text拼接

        self.ciphertext = cryptor.encrypt(text)
        return (self.ciphertext)

    def decrypt(self, text):
        cryptor = AES.new(self.key, self.mode, self.iv)
        decrypt_text = cryptor.decrypt((text)).decode()
        return decrypt_text


pc = AesCrypto(key="keyskeyskeyskeyskeyskeyskeyskeys", IV="keyskeyskeyskeys")
e = pc.encrypt("金毛狮王!!!".encode("utf-8"))  # 加密数据
print(e)
d = pc.decrypt(e).decode()  # 解密数据
print(d)

三、DES

python2,使用python3时,中文会报错,暂未解决

# -*-coding:utf-8 -*-
import sys

from Crypto.Cipher import DES

reload(sys)
sys.setdefaultencoding("utf-8")

# python3使用此模块
# from Cryptodome.Cipher import DES
# from Cryptodome.Cipher import AES



class DesUtil():
    """
    DES加密解密
    """

    key = '12345678'  # 密钥 8位或16位,必须为bytes

    def __init__(self):
        self.des = DES.new(self.key, DES.MODE_ECB)  # 创建一个DES实例

    def pad(self, text):
        """
        加密函数,如果text不是8的倍数【加密文本text必须为8的倍数!】,那就补足为8的倍数
        :param text:
        :return:
        """
        while len(text) % 8 != 0:
            text += ' '
        return text

    def encrypt(self, text):
        padded_text = self.pad(text)
        encrypted_text = self.des.encrypt(padded_text.encode('utf-8'))  # 加密
        return encrypted_text

    def decrypt(self, text):
        # rstrip(' ')返回从字符串末尾删除所有字符串的字符串(默认空白字符)的副本
        decrypted_text = self.des.decrypt(text).decode().rstrip(' ')  # 解密
        return decrypted_text


des = DesUtil()
enc = des.encrypt("我是一个兵!")
print("加密后:%s" % enc)
dec = des.decrypt(enc)
print("解密后:%s" % dec)

四、django内部加密

暂时使用到make_password、check_password,Signer模块

你可能感兴趣的:(python基础,python,开发语言,后端)