Python AES加密 与 JS AES加密

import execjs
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
 
def js_aes(text):
    jscode = """
    function encryptByAES(pwd) {
        var cryptoJS = require("crypto-js");
        
        let i = cryptoJS.enc.Utf8.parse("12345678901234561234567890123456");
        let t = cryptoJS.enc.Utf8.parse(pwd);
        let o = cryptoJS.enc.Utf8.parse("1234567890123456");
        return cryptoJS.AES.encrypt(t, i, {
                    iv: o,
                    mode: cryptoJS.mode.CBC,
                    padding: cryptoJS.pad.Pkcs7
                }).ciphertext.toString()
    }
    """
    ctx = execjs.compile(jscode)
    encrypto = ctx.call("encryptByAES",text)
 
    return encrypto
    
 
 
def py_aes(text):
    key = b"12345678901234561234567890123456"   #长度必须为16
    text = text.encode("utf-8")
 
    cryptor = AES.new(key,AES.MODE_CBC,iv = b"1234567890123456")  
    pad = 16 - len(text) % 16
    text = text + (chr(pad) * pad).encode("utf-8")   #相当于JS里面的 padding: cryptoJS.pad.Pkcs7
    ciphertext = cryptor.encrypt(text)
 
    return b2a_hex(ciphertext).decode("utf-8")
 
 
text = "!abc123你好"
js_res = js_aes(text)
py_res = py_aes(text)
 
print (js_res ==py_res)
print (js_res)
print (js_res)
 
const CryptoJS = require("crypto-js");

// let value = '123456';//待加密字符串加密
// let secrt_value = 'af25-87hk-a35v-5';//秘钥16位
// let iv_value = 'af25-87hk-a35v-5';//初始向量 initial vector 16 位

var e = "2F9fs3z84c36a8d1";
var t = "DnEL5EDzhqWqceY9";
let secret = CryptoJS.enc.Utf8.parse(e);
let iv = CryptoJS.enc.Utf8.parse(t);
var n = {
    "param1":"427191e6bf70a64752cf549ef887194e",
    "param2":"3deaf552d50988f51b5f5698a01e5eab",
    "param3":"e9a3f4f751e2a5f2f127c2ee6ec0bcc9",
    "param4":"feaef5bea7a46a8b34133505268cfae1"
}
let value = CryptoJS.enc.Utf8.parse(n);

//加密
let encryted = CryptoJS.AES.encrypt(value,secret,{
    iv :iv,
    //mode 支持 CBC、CFB、CTR、ECB、OFB,默认CBC
    mode: CryptoJS.mode.CBC,
    //NoPadding 、ZeroPadding, 默认 PKcs7 即PKcs5
    padding :CryptoJS.pad.Pkcs7
});
// 将加密结果转换为字符串
// encryted = encryted.ciphertext.toString();
// console.log(encryted);
encryted2 = encryted.toString();
console.log(encryted2);


// //解密,传入密文、秘钥和向量并设置加密与填充模式
// let decrypted = CryptoJS.AES.decrypt(encryted,secret,{
//     iv :iv,
//     //mode 支持 CBC、CFB、CTR、ECB、OFB,默认CBC
//     mode: CryptoJS.mode.CBC,
//     //NoPadding 、ZeroPadding, 默认 PKcs7 即PKcs5
//     padding :CryptoJS.pad.Pkcs7
// });

// //将解密结果转换为utf8字符串
// decrypted = CryptoJS.enc.Utf8.stringify(decrypted);
// /*
// 打印明文、密文和解密结果
// xLilCRGWQ/CGqBUVya+4LQ==
// 123456

//  */

// console.log(value);
// console.log(encryted);
// console.log(decrypted);

from Crypto.Cipher import AES
import hashlib
import json
from binascii import b2a_hex, a2b_hex

def encrypt(key, iv, data):
    # AES/ECB/PKCS5Padding
    mode = AES.MODE_ECB
    cryptos = AES.new(key, mode, iv)
    # padding = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16)
    # cipher_text = cryptos.encrypt(padding(data).encode("utf-8"))
    # return b2a_hex(cipher_text).decode("utf-8")
    pad = 16 - len(data) % 16
    text = data.encode("utf-8") + (chr(pad) * pad).encode("utf-8")
    ciphertext = cryptos.encrypt(text)
    return b2a_hex(ciphertext).decode("utf-8")

key = b"2F9fs3z84c36a8d1"
iv = b"DnEL5EDzhqWqceY9"
toke_params = {
    "param1": "34bb3a3d9d1d345d6a109ba472182510",
    "param2": "b005e79050cd89c12fafd6fbad24e3a0",
    "param3": "dab9aecd93be0c34229fce41851ec658",
    "param4": "8b92838f14894506f2ed1c34559f17a3"
}
# toke_params = str(toke_params)
# print(toke_params)
toke_params = json.dumps(toke_params)
token = encrypt(key, iv, toke_params)
print(token)

 

你可能感兴趣的:(Python基础知识)