A-A+

python 加密解密 AES _ ECB _ PKCS7Padding

2021年06月12日 21:16 汪洋大海 暂无评论 阅读 91 views 次

python脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#AES/ECB/PKCS7Padding 加密解密
#环境需求:
#pip3 install pycryptodome
 
from Crypto.Cipher import AES
from Crypto.Util import Padding
from base64 import b64encode, b64decode
 
 
def aes_ecb_pkcs7_b64_encrypt(data, key):
    data = Padding.pad(data.encode('utf-8'), AES.block_size, 'pkcs7')
    aes = AES.new(key.encode('utf-8'), AES.MODE_ECB)
    return b64encode(aes.encrypt(data)).decode('utf-8')
 
 
def aes_ecb_pkcs7_b64_decrypt(data, key):
    data = b64decode(data.encode('utf-8'))
    aes = AES.new(key.encode('utf-8'), AES.MODE_ECB)
    return Padding.unpad(aes.decrypt(data), AES.block_size, 'pkcs7').decode('utf-8')

脚本来源:https://github.com/vcheckzen/FODI/blob/f3c1d5265802d1242ad5e50eb741d63b4ea5bbcc/back-end-py/main/util/crypto.py

javascript 脚本
需要引入:

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//调用方法
var userName="admin"
ENC.GetEnc(userName)
var ENC = {
    GetEnc: function(str) {
        try {
            var key = CryptoJS.enc.Utf8.parse("12345678");  #key的值12345678
            var srcs = CryptoJS.enc.Utf8.parse(str);
            var encrypted = CryptoJS.AES.encrypt(srcs, key, {
                mode: CryptoJS.mode.ECB,
                padding: CryptoJS.pad.Pkcs7
            });
            return encrypted
        } catch (e) {
            return str
        }
    }
}

代码来源:

布施恩德可便相知重

微信扫一扫打赏

支付宝扫一扫打赏

×

给我留言