基於python3和Vue實現AES數據加密

**高級加密標準(AES,Advanced Encryption Standard)爲最常見的對稱加密算法(微信小程序加密傳輸就是用這個加密算法的)。對稱加密算法也就是加密和解密用相同的密鑰,具有以下幾個特點:

1、最常用的對稱加密算法
2、密鑰建立時間短、靈敏性好、內存需求低
3、實際使用中,使用工作模式爲CTR(最好用BC去實現),此工作模式需要引入IV參數(16位的字節數組)
4、密鑰長度128/192/256,其中192與256需要配置無政策限制權限文件(JDK6)
5、填充模式最常用的兩種PKCS5Padding和PKCS7Padding,其中後者只有BC獨有。
6、加密和解密用到的密鑰是相同的,這種加密方式加密速度非常快,適合經常發送數據的場合。
python (ECB)應用
安裝:
Windows : pip install pycryptodome

Linux : pip install pycrypto

1 import base64
2 import json
3 import re
4
5 from Crypto.Cipher import AES
6 from Crypto.Util.Padding import pad
7
8
9 class AesCrypt(object):
10 """
11 AES 加密組件
12 """
13
14 def init(self, user, isjson=True):
15
16 # 這裏的密鑰長度必須是 16 24 32
17 key = 'suiyi
' + user.get('Auth')
18 self.is_json = isjson
19 self.encode
= 'utf-8'
20 self.key = self.add_32(key)
21 print(self.key)
22 self.aes = AES.new(self.key, AES.MODE_ECB) # 創建一個aes對象
23
24 def add32(self, key):
25 """
26 key 補齊32位
27 :param key:
28 :return:
29 """
30 # 字符串 a 不要小於32位
31 a = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
32 key += a
33 key = key[0:32]
34 return key.encode(self.encode
)
35
36 def aes_encrypt(self, text):
37 """
38 加密 支持 json 需在實例中制動 is_json = True
39 :param text:
40 :return:
41 """
42 if self.is_json:
43 text = json.dumps(text, ensure_ascii=False)
44 text = pad(text.encode('utf-8'), AES.block_size, style='pkcs7')
45 encrypt_text = self.aes.encrypt(text)
46 return base64.encodebytes(encrypt_text).decode().strip()
47
48 def aes_decrypt(self, text):
49 """
50 解密 支持 json 需在實例中制動 isjson = True
51 :param text:
52 :return:
53 """
54 text = base64.decodebytes(text.encode(self.encode
))
55 decrypt_bytes = self.aes.decrypt(text)
56 decrypt_text = re.compile('[\x00-\x08\x0b-\x0c\x0e-\x1f\n\r\t]').sub('', decryptbytes.decode(
57 self.encode
))
58 if self.is_json:
59 decrypt_text = json.loads(decrypt_text)
60 return decrypt_text
61 if name == 'main':
62 user = {'Auth': '0000_zhangziyi'}
63 pr = AesCrypt(user, is_json=True)
64 data = {"unit": 1, "theme": "cur", "look_detail": True, "zero_empty": True, "zero_hide": True, "data_type": "sum"}
65 en_text = pr.aes_encrypt(data)
66 print('密文:', en_text)
67 pr2 = AesCrypt(user, is_json=True)
68 print('明文:', pr2.aes_decrypt(en_text))

Vue (ECB)應用
安裝:
cnpm install crypto-js --save

1 import store from '@/store'
2 import CryptoJS from 'crypto-js/crypto-js'
3
4 function add_secretkey (userAuth) {
5 let key = 'suiyi
' + userAuth
6 if (key.length < 32) {
7 let a = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
8 key += a.slice(0, 32 - key.length)
9 } else if (key.length > 32) {
10 key = key.slice(0, 32)
11 }
12 console.log(key)
13 return key
14
15 }
16
17 /
18 加密
19
@param wordimport { aes_encrypt, aes_decrypt } from '../../libs/crypto'
20
21 aes_encrypt(this.data)
22 aes_decrypt(this.AES_data)
23 @param userAuth代碼
24
@param is_json
25 @returns {string}
26
/
27 export const aes_encrypt = (word, userAuth, is_json = true) => {
28 if (is_json) {
29 word = JSON.stringify(word)
30 }
31 var key = CryptoJS.enc.Utf8.parse(add_secret_key(userAuth)) // s/iqSaaE0F3tsLgMCkCZjvqptKKzqD9/pMUnMkCwNjg= Set
32 var srcs = CryptoJS.enc.Utf8.parse(word)
33 var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
34 return encrypted.toString()
35 }
36 /
37 解密
38
@param word
39 @param userAuth
40
@param is_json
41 @returns {string}
42
/
43 export const aes_decrypt = (word, userAuth, is_json = true) => {
44 var key = CryptoJS.enc.Utf8.parse(add_secret_key(userAuth))// s/iqSaaE0F3tsLgMCkCZjvqptKKzqD9/pMUnMkCwNjg= Set
45 var decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
46 let decrypt_text = CryptoJS.enc.Utf8.stringify(decrypt).toString()
47 if (is_json) {
48 decrypt_text = JSON.parse(decrypt_text)
49 }****
50 return decrypt_text
51 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章