第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

python的實(shí)用加密模塊

標(biāo)簽:
Python

说明一:关于MD5,SHA1,SHA256,SHA512加密

这几个哈希算法的加密,都在python的内建模块hashlib里有支持。
本模块的该部分主要参考廖雪峰的python3教程编写,大家根据教程可以进一步了解下。

说明二:关于AES加密

AES加密,用的是第三方模块 pycryptodome。

模块安装命令:pip install pycryptodome

AES有好几种模式,本模块列了ECB,CFB,CBC三种模式。据说,CBC模式是其中公认的安全性最好的模式。至于它们的加密原理,本人精力有限,也没深入研究,大家自行了解下。

本模块的该部分主要参考python3 AES 加密这篇文章编写。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import hashlib
import base64
from Crypto.Cipher import AES

##############################################
the_salt = "my_salt"

the_key = "my_key"
######################################################

class HashManager():

    #######MD5加密#######
    def get_md5(self,the_string):
        the_string_with_salt =the_string + the_salt
        the_md5 = hashlib.md5()
        the_md5.update(the_string_with_salt.encode('utf-8'))
        the_string_md5 = the_md5.hexdigest()
        return the_string_md5

    #######SHA1加密#######
    def get_sha1(self, the_string):
        the_string_with_salt =the_string + the_salt
        the_sha1 = hashlib.sha1()
        the_sha1.update(the_string_with_salt.encode('utf-8'))
        the_string_sha1 = the_sha1.hexdigest()
        return the_string_sha1

    #######SHA256加密#######
    def get_sha256(self, the_string):
        the_string_with_salt =the_string + the_salt
        the_sha256 = hashlib.sha256()
        the_sha256.update(the_string_with_salt.encode('utf-8'))
        the_string_sha1 = the_sha256.hexdigest()
        return the_string_sha1

    #######SHA512加密#######
    def get_sha512(self, the_string):
        the_string_with_salt =the_string + the_salt
        the_sha512 = hashlib.sha512()
        the_sha512.update(the_string_with_salt.encode('utf-8'))
        the_string_sha1 = the_sha512.hexdigest()
        return the_string_sha1




    #######AES加密,ECB模式#######
    def get_aes_ecb(self, the_string):
        aes = AES.new(self.pkcs7padding_tobytes(the_key), AES.MODE_ECB)           # 初始化加密器
        encrypt_aes = aes.encrypt(self.pkcs7padding_tobytes(the_string))          # 进行aes加密
        encrypted_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8')   # 用base64转成字符串形式
        return encrypted_text


    #######AES解密,ECB模式#######
    def back_aes_ecb(self, the_string):
        aes = AES.new(self.pkcs7padding_tobytes(the_key), AES.MODE_ECB)             # 初始化加密器
        decrypted_base64 = base64.decodebytes(the_string.encode(encoding='utf-8'))  # 逆向解密base64成bytes
        decrypted_text = str(aes.decrypt(decrypted_base64), encoding='utf-8')       # 执行解密密并转码返回str
        decrypted_text_last = self.pkcs7unpadding(decrypted_text)                   # 去除填充处理
        return decrypted_text_last



    #######AES加密,CFB模式#######
    def get_aes_cfb(self, the_string):
        key_bytes = self.pkcs7padding_tobytes(the_key)
        iv = key_bytes
        aes = AES.new(key_bytes, AES.MODE_CFB, iv)                              # 初始化加密器,key,iv使用同一个
        encrypt_aes = iv + aes.encrypt(the_string.encode())                     # 进行aes加密
        encrypted_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8') # 用base64转成字符串形式
        return encrypted_text


    #######AES解密,CFB模式#######
    def back_aes_cfb(self, the_string):
        key_bytes = self.pkcs7padding_tobytes(the_key)
        iv = key_bytes
        aes = AES.new(key_bytes, AES.MODE_CFB, iv)                                 # 初始化加密器,key,iv使用同一个
        decrypted_base64 = base64.decodebytes(the_string.encode(encoding='utf-8')) # 逆向解密base64成bytes
        decrypted_text = str(aes.decrypt(decrypted_base64[16:]), encoding='utf-8') # 执行解密密并转码返回str
        return decrypted_text




    #######AES加密,CBC模式#######
    def get_aes_cbc(self, the_string):
        key_bytes = self.pkcs7padding_tobytes(the_key)
        iv = key_bytes
        aes = AES.new(key_bytes, AES.MODE_CBC, iv)                              # 初始化加密器,key,iv使用同一个
        encrypt_bytes = aes.encrypt(self.pkcs7padding_tobytes(the_string))      # 进行aes加密
        encrypted_text = str(base64.b64encode(encrypt_bytes), encoding='utf-8') # 用base64转成字符串形式
        return encrypted_text


    #######AES解密,CBC模式#######
    def back_aes_cbc(self, the_string):
        key_bytes = self.pkcs7padding_tobytes(the_key)
        iv = key_bytes
        aes = AES.new(key_bytes, AES.MODE_CBC, iv)                              # 初始化加密器,key,iv使用同一个
        decrypted_base64 = base64.b64decode(the_string)                         # 逆向解密base64成bytes
        decrypted_text = str(aes.decrypt(decrypted_base64), encoding='utf-8')   # 执行解密密并转码返回str
        decrypted_text_last = self.pkcs7unpadding(decrypted_text)               # 去除填充处理
        return decrypted_text_last





    #######填充相关函数#######
    def pkcs7padding_tobytes(self, text):
        return bytes(self.pkcs7padding(text), encoding='utf-8')

    def pkcs7padding(self,text):
        bs = AES.block_size
        ####tips:utf-8编码时,英文占1个byte,而中文占3个byte####
        length = len(text)
        bytes_length = len(bytes(text, encoding='utf-8'))
        padding_size = length if (bytes_length == length) else bytes_length
        ####################################################
        padding = bs - padding_size % bs
        padding_text = chr(padding) * padding    # tips:chr(padding)看与其它语言的约定,有的会使用'\0'
        return text + padding_text


    def pkcs7unpadding(self,text):
        length = len(text)
        unpadding = ord(text[length - 1])
        return text[0:length - unpadding]

本文如有帮助,敬请留言鼓励。
本文如有错误,敬请留言改进。

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消