日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

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

發(fā)布時(shí)間:2025/3/20 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python2.x和3.x为什么不兼容_Python中使用AES算法(解决Python2.x和3.x下运行不兼容问题)... 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

兩年前使用python時(shí),碰到2.x下AES加密解密算法代碼無(wú)法在3.x下順利運(yùn)行,花點(diǎn)時(shí)間解決了兼容問(wèn)題,在2.7、3.6、3.7下運(yùn)行良好。

Linux系統(tǒng)安裝依賴庫(kù)比較簡(jiǎn)單,Windows下稍嫌繁瑣,裝完必須的庫(kù)也可以正常運(yùn)行。

代碼整理如下:

# -*- 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.測(cè)試文本')

總結(jié)

以上是生活随笔為你收集整理的python2.x和3.x为什么不兼容_Python中使用AES算法(解决Python2.x和3.x下运行不兼容问题)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。