python实现aes256加密
基础知识
# 在Linux操作系统下,Python3的默认环境编码变为了utf-8编码,所以在编写代码的时候,字符串⼤部分都是以utf-8处理
UTF-8:
1byte = 8bit
1个英⽂字符 = 1byte
1个中⽂字符 = 3byte
128bit = 16byte = 16个英⽂字符
192bit = 24byte = 24个英⽂字符
256bit = 32byte = 32个英⽂字符
AES256概念
AES是⼀种对称加密算法,对称指加密和解密使⽤同⼀个密钥; 256指密钥的长度是256bit,即32个英⽂字符的长度;密钥的长度决定了AES 加密的轮数
AES256加密参数
密钥:⼀个32byte的字符串,常被叫为key
明⽂:待加密的字符串;字节长度(按byte计算)必须是16的整数倍,因此,明⽂加密之前需要被填充
模式:加密模式,常⽤的有ECB、CBC;具体含义见参考链接
iv 偏移量: CBC模式下需要是16byte字符串; ECB下不需要
参考代码
# -------------------------------
# -*- coding: utf-8 -*-
# @Author:jianghan
# @Time:2020/11/25 14:46
# @File: crypt.py
# Python版本:3.6.8
# -------------------------------
"""
1、填充字符串和明⽂字符串最后⼀位不能相同
2、字符串编码默认是utf-8, key和iv默认为英⽂字符;字符串不⽀持其他编码或key/iv不⽀持为中⽂字符
"""
from enum import Enum, unique
from Crypto.Cipher import AES
@unique
class Mode(Enum):
CBC = AES.MODE_CBC
ECB = AES.MODE_ECB
@unique
class Padding(Enum):
""" 定义填充的字符串 """
SPACE = ' ' # 空格
class AES256Crypto:
def __init__(self, key, mode=Mode.ECB, padding=Padding.SPACE, iv=None):
"""
:param key: 密钥, 32byte 长度字符串
:param mode: 加密模式,来源 class Mode
:
param iv: 16byte 长度字符串
:param padding: 填充的字符串,来源class Padding
"""
self.padding = self.check_padding(padding)
self.key = self.padding_key(key)
self.iv = self.padding_iv(iv) if iv else None
def check_mode(self, mode):
""" 核对 mode """
if mode not in Mode.__members__.values():
raise Exception(f'mode {mode} not allowed!')
if mode == Mode.CBC and not self.iv:
raise Exception(f'iv is required')
return mode
def check_padding(self, padding):
""" 核对 padding """
if padding not in Padding.__members__.values():
raise Exception(f'mode {padding} not allowed!')
return padding
def padding_ret_byte(self, text, _len=16):
""" 填充并转成 bytes """
text = de()
remainder = len(text) % _len
remainder = _len if remainder == 0 else remainder
text += (_len - remainder) * self.de()
return text
def padding_iv(self, iv: str):
""" 补全iv 并转成 bytes"""
if de()) > 16:
raise Exception(f'iv {iv} must <= 16bytes')
return self.padding_ret_byte(iv)
def padding_key(self, key: str):
""" 补全key 并转成 bytes """
if de()) > 32:
raise Exception(f'key {key} must <= 32bytes')
return self.padding_ret_byte(key, _len=32)
def encrypt(self, text, encode=None):
"""
加密
:param text: 待加密字符串
:param encode: 传⼊base64⾥⾯的⽅法
:return: 若encode=None则不进⾏base加密处理,返回bytes类型数据
"""
text = self.padding_ret_byte(text)
# 注意:加密中的和解密中的w()不能使⽤同⼀个对象,所以在两处都使⽤了w()
text = w(key=self.key, de.value, iv=self.iv).encrypt(text)
if encode:
return encode(text).decode()
字符串长度 pythonreturn text
def decrypt(self, text, decode=None):
""" 解密 """
if decode:
if type(text) == str:
text = de()
text = decode(bytes(text))
else:
if type(text) != bytes:
raise Exception(text)
text = w(key=self.key, de.value, iv=self.iv).decrypt(text)
text = text.strip(self.de())
return text.decode()
使⽤范例
import json
# 这是⼀段待加密的字符串
text = '{"upi": "1341343", "overdue": "2020-11-26 00:00:00"}'
key = 't6LtKa3tD5X6qaJ6qOrAW3XmobFrY6ob'
iv = 'NjtP47eSECuOm3s6'
aes = AES256Crypto(key, Mode.CBC, Padding.SPACE, iv)
text_1 = pt(text)
# b' e7 1d eae ff c7 c2 d7 8c f6 e7 82u 7f 168 bc 90 ad 1e 85M cb b0 b4Ho 1b e4 ec 9d 1d f93 eb 9b e7 a3 dd$ 8cEa ab f7K~ 91H c3]5 c4 1a d4w[ 83 b2"FC 9f 9d'
text_2 = aes.decrypt(text_1)
# '{"upi": "1341343", "overdue": "2020-11-26 00:00:00"}'
import base64
text_3 = pt(text, encode=base64.b16encode)
# 'E71DEA65FFC7C2D78CF6E782757F1638BC90AD1E854DCBB0B4486F1BE4EC9D1DF933EB9BE7A3DD248C4561ABF74B7E9148C35D35C41AD4775B83B22246439F9D' text_4 = aes.decrypt(text_3, decode=base64.b16decode)
# '{"upi": "1341343", "overdue": "2020-11-26 00:00:00"}'
以上就是python 实现aes256加密的详细内容,更多关于python aes256加密的资料请关注其它相关⽂章!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。