python的实用加密模块

简介: 关于MD5,SHA1,SHA256,SHA512加密 关于AES加密

说明一:关于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]

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

目录
相关文章
|
7月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
899 7
|
7月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
643 0
|
7月前
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
623 4
|
8月前
|
安全 大数据 程序员
Python operator模块的methodcaller:一行代码搞定对象方法调用的黑科技
`operator.methodcaller`是Python中处理对象方法调用的高效工具,替代冗长Lambda,提升代码可读性与性能。适用于数据过滤、排序、转换等场景,支持参数传递与链式调用,是函数式编程的隐藏利器。
255 4
|
7月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
718 0
|
7月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
1015 0
|
8月前
|
存储 数据库 开发者
Python SQLite模块:轻量级数据库的实战指南
本文深入讲解Python内置sqlite3模块的实战应用,涵盖数据库连接、CRUD操作、事务管理、性能优化及高级特性,结合完整案例,助你快速掌握SQLite在小型项目中的高效使用,是Python开发者必备的轻量级数据库指南。
720 0
|
9月前
|
存储 安全 数据处理
Python 内置模块 collections 详解
`collections` 是 Python 内置模块,提供多种高效数据类型,如 `namedtuple`、`deque`、`Counter` 等,帮助开发者优化数据处理流程,提升代码可读性与性能,适用于复杂数据结构管理与高效操作场景。
569 0
|
10月前
|
数据安全/隐私保护 Python
抖音私信脚本app,协议私信群发工具,抖音python私信模块
这个实现包含三个主要模块:抖音私信核心功能类、辅助工具类和主程序入口。核心功能包括登录
|
数据采集 前端开发 JavaScript
Python爬虫如何应对网站的反爬加密策略?
Python爬虫如何应对网站的反爬加密策略?
917 11

推荐镜像

更多