开发手册 欢迎您!
软件开发者资料库

Python、aes.js和node.js实现AES(Crypto)加密与解密实现代码

AES算法可以实现字符串的加解密,本文主要介绍Python、Js(JavaScript)和node.js中实现AES(Crypto)相互加解密,分别通过Python和aes.js实现方法及示例代码。

Python中实现AES(CBC)加密和解密:

安装Crypto:pip install pycrypto

windows中pip install pycryptodome

from Crypto.Cipher  import AESimport base64BLOCK_SIZE = 16  # Bytespad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * \                chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)unpad = lambda s: s[:-ord(s[len(s) - 1:])]#python3中#加密def AES_Encrypt(key, iv,data):    data = pad(data)    # 字符串补位    cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))    encryptedbytes = cipher.encrypt(data.encode('utf8'))    # 加密后得到的是bytes类型的数据,使用Base64进行编码,返回byte字符串    encodestrs = base64.b64encode(encryptedbytes)    # 对byte字符串按utf-8进行解码    return encodestrs#解密def AES_Decrypt(key,iv, data):    data = data.encode('utf8')    encodebytes = base64.decodebytes(data)    # 将加密数据转换位bytes类型数据    cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))    text_decrypted = cipher.decrypt(encodebytes)    # 去补位    text_decrypted = unpad(text_decrypted)    text_decrypted = text_decrypted.decode('utf8')    print(text_decrypted)    return text_decrypted#python2中#加密def AES_Encrypt(key, iv,data):    data = pad(data)    # 字符串补位    cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))    encryptedbytes = cipher.encrypt(data.encode('utf8'))    # 加密后得到的是bytes类型的数据,使用Base64进行编码,返回byte字符串    encodestrs = base64.standard_b64encode(encryptedbytes)    # 对byte字符串按utf-8进行解码    return encodestrs#解密def AES_Decrypt(key,iv, data):    data = data.encode('utf8')    encodebytes = base64.standard_b64decode(data)    # 将加密数据转换位bytes类型数据    cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))    text_decrypted = cipher.decrypt(encodebytes)    # 去补位    text_decrypted = unpad(text_decrypted)    text_decrypted = text_decrypted.decode('utf8')    print(text_decrypted)    return text_decrypted

测试:

AES_Encrypt("CJAVAPmXcuAksWmF","V33CQ1428SI8ZNMT","137") 
AES_Decrypt("CJAVAPmXcuAksWmF","V33CQ1428SI8ZNMT","v9BxRJX+ojPDhyJFrglcbA==")

注意:CBC加密需要一个十六位的key(密钥)和一个十六位iv(偏移量)ECB加密不需要iv。

JS中实现AES(CBC)加密和解密:

aes.js下载引用地址https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js

//加密function encrypt(e,key,iv) {       var r =  CryptoJS.enc.Utf8.parse(key),       var n =  CryptoJS.enc.Utf8.parse(iv),       var  o =  CryptoJS.enc.Utf8.parse(e),       var u = {            iv: n,            mode:  CryptoJS.mode.CBC,            padding:  CryptoJS.pad.Pkcs7        },        i =  CryptoJS.AES.encrypt(o, r, u);        return i.toString() }//解密function decrypt(str, key, iv) {   var r = CryptoJS.enc.Utf8.parse(iv),   var  n = CryptoJS.enc.Utf8.parse(key),   var o = CryptoJS.AES.decrypt(e, n, {        iv: r,        mode: CryptoJS.mode.CBC,        padding: CryptoJS.pad.Pkcs7    });    return CryptoJS.enc.Utf8.stringify(o).toString()}

Node.js中实现AES(CBC)加密和解密:

安装cryptonpm install crypto

//加密function encrypt (data, key, iv) {    iv = iv || "";    var clearEncoding = 'utf8';    var cipherEncoding = 'base64';    var cipherChunks = [];    var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);    cipher.setAutoPadding(true);    cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));    cipherChunks.push(cipher.final(cipherEncoding));    return cipherChunks.join('');}//解密function decrypt (data, key, iv) {    if (!data) {        return "";    }    iv = iv || "";    var clearEncoding = 'utf8';    var cipherEncoding = 'base64';    var cipherChunks = [];    var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);    decipher.setAutoPadding(true);    cipherChunks.push(decipher.update(data, cipherEncoding, clearEncoding));    cipherChunks.push(decipher.final(clearEncoding));    return cipherChunks.join('');}