使用AES-CBC + HMAC-SHA256实现前后端请求安全验证_aes cbc256
AES-CBC + HMAC-SHA256 加密验证方案,下面是该方案二等 优点 与 缺点 表格,适用于文档、评审或技术选型说明。
✅ 优点表格:AES-CBC + HMAC-SHA256 加密验证方案
❌ 缺点表格:AES-CBC + HMAC 签名方案的局限
timestamp + nonce
参数。🧭 补充建议(可选扩展)
下面是该方案的实现详细代码:
✅ 前端 JavaScript:frontend.js
// === frontend.js ===// 前端:使用 AES-CBC 加密 + HMAC-SHA256 签名import aesjs from \'aes-js\';import CryptoJS from \'crypto-js\';function aaa(token) { return aesjs.utils.utf8.toBytes(token.padEnd(16, \'0\').slice(0, 16));}function generateRandomIV() { let iv = new Uint8Array(16); window.crypto.getRandomValues(iv); return iv;}function getHmacSHA256(keyBytes, messageHex) { const keyHex = CryptoJS.enc.Hex.parse(aesjs.utils.hex.fromBytes(keyBytes)); const hmac = CryptoJS.HmacSHA256(messageHex, keyHex); return hmac.toString(CryptoJS.enc.Hex);}function encryptWithMac(token, plaintext) { const key = aaa(token); const iv = generateRandomIV(); const textBytes = aesjs.utils.utf8.toBytes(plaintext); const padded = aesjs.padding.pkcs7.pad(textBytes); const aesCbc = new aesjs.ModeOfOperation.cbc(key, iv); const encryptedBytes = aesCbc.encrypt(padded); const ivHex = aesjs.utils.hex.fromBytes(iv); const ciphertextHex = aesjs.utils.hex.fromBytes(encryptedBytes); const fullDataHex = ivHex + ciphertextHex; const mac = getHmacSHA256(key, fullDataHex); return { data: fullDataHex, mac: mac };}const token = \'abc123\';const msg = \'hello world\';const result = encryptWithMac(token, msg);console.log(JSON.stringify(result));
✅ Node.js 后端验证:backend_node.js
// backend_node.jsconst crypto = require(\'crypto\');function deriveKey(token) { return Buffer.from(token.padEnd(16, \'0\').slice(0, 16));}function verifyEncryptedData(token, dataHex, macHex) { const key = deriveKey(token); const iv = Buffer.from(dataHex.slice(0, 32), \'hex\'); const ciphertext = Buffer.from(dataHex.slice(32), \'hex\'); // 验证 HMAC const hmac = crypto.createHmac(\'sha256\', key); hmac.update(dataHex); const expectedMac = hmac.digest(\'hex\'); if (expectedMac !== macHex) { throw new Error(\'MAC 验证失败\'); } // 解密 const decipher = crypto.createDecipheriv(\'aes-128-cbc\', key, iv); decipher.setAutoPadding(true); let decrypted = decipher.update(ciphertext, null, \'utf8\'); decrypted += decipher.final(\'utf8\'); return decrypted;}// 示例const token = \'abc123\';const { data, mac } = JSON.parse(/* 前端结果 */ \'{\"data\": \"...\", \"mac\": \"...\"}\');try { const plaintext = verifyEncryptedData(token, data, mac); console.log(\'解密成功:\', plaintext);} catch (e) { console.error(e.message);}
✅ Python 后端验证:backend_python.py
# backend_python.pyfrom Crypto.Cipher import AESfrom Crypto.Hash import HMAC, SHA256from binascii import unhexlifydef derive_key(token: str) -> bytes: return token.ljust(16, \'0\')[:16].encode()def verify_encrypted_data(token, data_hex, mac_hex): key = derive_key(token) iv = unhexlify(data_hex[:32]) ciphertext = unhexlify(data_hex[32:]) # 验证 HMAC h = HMAC.new(key, digestmod=SHA256) h.update(data_hex.encode()) try: h.hexverify(mac_hex) except ValueError: raise ValueError(\'MAC 验证失败\') # 解密 cipher = AES.new(key, AES.MODE_CBC, iv) padded = cipher.decrypt(ciphertext) pad_len = padded[-1] return padded[:-pad_len].decode()# 示例token = \'abc123\'data = \'...\' # 前端 datamac = \'...\' # 前端 mactry: print(\'解密成功:\', verify_encrypted_data(token, data, mac))except Exception as e: print(\'失败:\', e)
✅ Nginx + Lua (OpenResty):aes_verify.lua
-- aes_verify.lualocal aes = require \"resty.aes\"local hmac = require \"resty.hmac\"local str = require \"resty.string\"local cjson = require \"cjson\"ngx.req.read_body()local body = ngx.req.get_body_data()local json = cjson.decode(body)local data = json.datalocal mac = json.maclocal token = ngx.var.http_authorization:sub(8)local key = token .. string.rep(\"0\", 16 - #token)key = key:sub(1, 16)local hmac_obj = hmac:new(key, hmac.ALGOS.SHA256)hmac_obj:update(data)local expected_mac = str.to_hex(hmac_obj:final())if expected_mac ~= mac then ngx.status = 401 ngx.say(\"MAC 验证失败\") return ngx.exit(401)endlocal iv = str.to_hex(data:sub(1, 32))local cipher = data:sub(33)local aes_cbc = aes:new(key, nil, aes.cipher(128, \"cbc\"), { iv = iv })local decrypted = aes_cbc:decrypt(str.from_hex(cipher))local pad = string.byte(decrypted:sub(-1))decrypted = decrypted:sub(1, -pad-1)ngx.say(\"验证通过,原文: \", decrypted)
配置片段:
location /api/secure-data { content_by_lua_file /etc/nginx/lua/aes_verify.lua; proxy_pass http://backend_service;}