两年前使用python时,碰到2.x下AES加密解密算法代码无法在3.x下顺利运行,花点时间解决了兼容问题,在2.7、3.6、3.7下运行良好。

Linux系统安装依赖库比较简单,Windows下稍嫌繁琐,装完必须的库也可以正常运行。

代码整理如下:

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

import base64

import sys

from Crypto import Random

from Crypto.Cipher import AES

# Author: areful

# Date: 2017-07-06

class AESCipher:

def __init__(self, key, iv=Random.new().read(AES.block_size)):

self.key = key

self.iv = iv

self.mode = AES.MODE_CBC

if sys.version > '3':

self.PY3 = True

else:

self.PY3 = False

def encrypt(self, text):

if self.PY3:

return self.encrypt36(text)

else:

return self.encrypt27(text)

def encrypt27(self, text):

cipher = AES.new(self.key, AES.MODE_CBC, self.iv)

bs = AES.block_size

text_length = len(text)

amount_to_pad = bs - (text_length % bs)

if amount_to_pad == 0:

amount_to_pad = bs

pad = chr(amount_to_pad)

text1 = text + pad * amount_to_pad

cipher_text = cipher.encrypt(text1)

return base64.b64encode(cipher_text)

def encrypt36(self, text):

cipher = AES.new(self.key, AES.MODE_CBC, self.iv)

bs = AES.block_size

text_length = len(text.encode('utf-8'))

amount_to_pad = bs - (text_length % bs)

if amount_to_pad == 0:

amount_to_pad = bs

pad = chr(amount_to_pad)

text1 = text + pad * amount_to_pad

cipher_text = cipher.encrypt(text1)

encrypted_str = str(base64.b64encode(cipher_text), encoding='utf-8')

return encrypted_str

def decrypt(self, text):

import base64

base_text = base64.b64decode(text)

cipher = AES.new(self.key, self.mode, self.iv)

plain_text = cipher.decrypt(base_text).decode('utf-8')

pad = ord(plain_text[-1])

ne = plain_text[:-pad]

return ne

@staticmethod

def pad(s):

bs = AES.block_size

return s + (bs - len(s) % bs) * chr(bs - len(s) % bs)

@staticmethod

def unpad(s):

return s[0:-ord(s[-1])]

@staticmethod

def __pad(text):

text_length = len(text)

amount_to_pad = AES.block_size - (text_length % AES.block_size)

if amount_to_pad == 0:

amount_to_pad = AES.block_size

pad = chr(amount_to_pad)

return text + pad * amount_to_pad

@staticmethod

def __unpad(text):

pad = ord(text[-1])

return text[:-pad]

@staticmethod

def test(key, iv, text):

cipher = AESCipher(key, iv)

encrypted_msg = cipher.encrypt(text)

print(encrypted_msg)

msg = cipher.decrypt(encrypted_msg)

print(msg)

if __name__ == '__main__':

AESCipher.test('ABCDEFGHICKLMNOP',

bytes(bytearray(b'x01x02x03x04x05x06x07x08x41x42x43x44x45x46x47x48')),

'areful.测试文本')

python2.x和3.x为什么不兼容_Python中使用AES算法(解决Python2.x和3.x下运行不兼容问题)...相关推荐

  1. 解决“错误 D8016 “/ZI”和“/Gy-”命令行选项不兼容 ”问题

    解决"错误 D8016 "/ZI"和"/Gy-"命令行选项不兼容 "问题 参考文章: (1)解决"错误 D8016 "/ ...

  2. 解决python2.x文件读写编码问题

    解决python2.x文件读写编码问题 参考文章: (1)解决python2.x文件读写编码问题 (2)https://www.cnblogs.com/nyist-xsk/p/9681064.html ...

  3. python mkl freebsd_FreeBSD:在uwsgi中使用python3而不是python2

    我有一台安装了FreeBSD 10.1的服务器.我想使用uwsgi nginx python3在其上部署一个Django站点并遇到一些问题. 该网站是为python3编写的,我安装了python3.4 ...

  4. 让你的网站在IE8的兼容模式下运行

    众所周知,微软的Internet Explorer团队一直在致力于将IE8打造为最符合业内标准的浏览器,所不幸的是,当前并非所有的网站都认同这些标准.如果你担心你的网站在IE8的标准模式下不能正常工作 ...

  5. python中文名字-完美解决Python2操作中文名文件乱码的问题

    Python2默认是不支持中文的,一般我们在程序的开头加上#-*-coding:utf-8-*-来解决这个问题,但是在我用open()方法打开文件时,中文名字却显示成了乱码. 我先给大家说说Pytho ...

  6. DIV CSS兼容性解决IE6/IE7/FF浏览器的通用方法完美兼容

    在网站设计的时候,应该注意css样式兼容不同浏览器问题,特别是对完全使用DIV CSS设计的网,就应该更注意IE6 IE7 FF对CSS样式的兼容,不然,你的网乱可能出去不想出现的效果! 所有浏览器 ...

  7. python2默认编码_解决Python2.x编码之殇

    Python编码问题一直困扰了我许久,之前有过一些总结,但并不系统,比较凌乱.当然python2.x编码问题本身,便是剪不断理还乱.本篇将系统介绍python2.x编程中会遇到的一些编码问题,并给出解 ...

  8. 解决vc 6在vista下的一些兼容问题

    解决vc 6在vista下的一些兼容问题 虽然vs系列的2008sp1版都出了,但是vs 2003以后的版本大多是都是在.Net上做修改,对我们vc程序员使用的MFC的修改很少.所以,许多VC程序员还 ...

  9. python绘图设置标题出现乱码_解决python2 绘图title,xlabel,ylabel出现中文乱码的问题...

    Python绘图如何显示中文标题一个懂得以幽默态度面对自己缺点并将缺点变成自己专属的特色-这才是真正乐观勇敢的人. 采用matplotlib作图时默认设置下是无法显示中文的,例如编写如下python脚 ...

最新文章

  1. 服务器根目录文件配置文件,在文档根目录中存储安装和配置文件
  2. python全局变量定义_Python 3 实现定义跨模块的全局变量和使用
  3. 【转】深入剖析iLBC的丢包补偿技术(PLC)
  4. 【CyberSecurityLearning 56】自动化注入
  5. java中arges.length_java中的args.length
  6. disconf mysql_disconf-web 安装
  7. 计算机网络与网站设计知识点,计算机网络技术知识点总结-20210525075410.docx-原创力文档...
  8. 无法检查指定的位置是否位于cfs上_打印机知识普及:七大原因导致的打印机无法打印及解决方法...
  9. 查找重复文件_快速查找、删除重复图片及文件!
  10. mac monterey、big sur、Catalina原生heic、jpg壁纸,并将壁纸拷贝到系统文件夹下教程
  11. 我的第一次WebService接口开发教程
  12. 居民小区变配电电力监控系统-安科瑞苏月婷
  13. [技术]使用人工智能玩微信跳一跳
  14. 华为电脑Linux进pe,华为 PE-TLOOM 开启USB调试模式
  15. 修改微信小程序单选,复选框样式
  16. 21_lua生成随机数
  17. 吕文翰 php,自己动手写一个 iOS 网络请求库(三)——降低耦合
  18. “甲流疫情死亡率”较标准程序
  19. 摄影后期软件darktable介绍、汉化、使用说明(Lightroom免费替代品)
  20. IntelliJ IDEA 电脑扩展分屏显示问题

热门文章

  1. Markdown的一些常用的语法
  2. Oracle 的基本特点,并完整描述安装过程
  3. Spring boot请求拦截
  4. .net bitmap rgb数据_在3D空间,用点云数据学行人重识别特征
  5. 【Java自顶向下】面试官:HashMap源码看过吗?我:看过!面试官:好极了,那么来扒一扒吧!
  6. 笔记本敲代码真香,包邮送一个!
  7. 不止一个人犯错,这种 Github 不要写在简历上!
  8. 信息系统项目管理知识--项目范围管理
  9. Hibernate hql 查询指定字段并获取结果集
  10. cdrx4被禁用怎么解决_双显卡怎么切换到独立显卡