码迷,mamicode.com
首页 > 编程语言 > 详细

基于python3和Vue实现AES数据加密

时间:2020-03-28 13:20:08      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:加密   npm   zhang   rip   aaa   传输   crypto   detail   zha   

**高级加密标准(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 }

基于python3和Vue实现AES数据加密

标签:加密   npm   zhang   rip   aaa   传输   crypto   detail   zha   

原文地址:https://blog.51cto.com/11395518/2482607

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!