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]

本文如有幫助,敬請留言鼓勵。
本文如有錯誤,敬請留言改進。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章