Python中使用AES算法(解决Python2.x和3.x下运行不兼容问题)

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

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

代码整理如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- 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'\x01\x02\x03\x04\x05\x06\x07\x08\x41\x42\x43\x44\x45\x46\x47\x48')),
                   'areful.测试文本')

  

  

posted on   areful  阅读(930)  评论(0编辑  收藏  举报

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示