手机app网站模板下载,网页视频怎么下载高清,上海建设摩托车科技有限公司官网,亿寻跨境外贸人才网AES-CBC HMAC-SHA256 加密验证方案#xff0c;下面是该方案二等 优点 与 缺点 表格#xff0c;适用于文档、评审或技术选型说明。
✅ 优点表格#xff1a;AES-CBC HMAC-SHA256 加密验证方案
类别优点说明#x1f510; 安全性使用 AES-CBC 对称加密使用 AES-128-CBC 是可…AES-CBC HMAC-SHA256 加密验证方案下面是该方案二等 优点 与 缺点 表格适用于文档、评审或技术选型说明。
✅ 优点表格AES-CBC HMAC-SHA256 加密验证方案
类别优点说明 安全性使用 AES-CBC 对称加密使用 AES-128-CBC 是可靠且广泛接受的对称加密方式。随机生成 IV每次加密生成新的 IV有效防止密文重放与模式识别。HMAC-SHA256 签名增强完整性校验防止中间人篡改密文。加密前先签名验证防止不合法密文触发解密报错或被利用。 灵活性签名算法可切换支持从 HMAC-SHA256 切换为其他如 SHA-512。密钥可由 token 派生动态生成密钥便于用户级安全控制。前端跨平台适用适用于 Web、小程序、移动端等多平台前端环境。 可部署性可嵌入代理层Nginx Lua 可提前拦截无效请求节省后端资源。 多语言兼容Node.js、Python、Lua 等实现简单支持常见语言和平台易于团队协作与系统整合。 ❌ 缺点表格AES-CBC HMAC 签名方案的局限
类别缺点说明⚙️ 实现复杂度实现逻辑较多需要额外编码 IV 管理、HMAC 签名、前后端一致性等细节。 重放防护默认无时间戳或 nonce重放攻击不可防需要自行引入 timestamp nonce 参数。 密钥依赖密钥动态性带来兼容问题一旦 token 失效或更换旧数据将无法解密。 数据随机访问不支持局部解密AES-CBC 是块加密不能随机访问或解密数据片段。 不适合长期缓存密文随机性增加校验复杂度每次加密结果不同不适合用于长期静态存储的校验场景。 补充建议可选扩展
增强点建议防重放在签名前加上时间戳 nonce 字段防止多次使用旧数据加密模式升级可考虑迁移到 AES-GCM天然支持认证AEAD秘钥管理密钥建议动态派生如基于用户会话、JWT claims 等
下面是该方案的实现详细代码 ✅ 前端 JavaScriptfrontend.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.js
const 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);// 验证 HMACconst 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.py
from Crypto.Cipher import AES
from Crypto.Hash import HMAC, SHA256
from 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:])# 验证 HMACh HMAC.new(key, digestmodSHA256)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 ... # 前端 data
mac ... # 前端 mactry:print(解密成功:, verify_encrypted_data(token, data, mac))
except Exception as e:print(失败:, e)✅ Nginx Lua (OpenResty)aes_verify.lua
-- aes_verify.lua
local aes require resty.aes
local hmac require resty.hmac
local str require resty.string
local cjson require cjsonngx.req.read_body()
local body ngx.req.get_body_data()
local json cjson.decode(body)
local data json.data
local mac json.mac
local 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 thenngx.status 401ngx.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;
}