一. 正常表单提交如图,可以看见输入的用户名密码等
二. 使用crypto-js.min.js进行前端加密处理
js资源地址链接:https://pan.baidu.com/s/1kfQZ1SzP6DUz50D–g_ziQ
提取码:1234
前端代码
<script type="text/javascript" src="/js/crypto-js.min.js"></script>
<script type="text/javascript">
function encrypt(str, key, iv) {
//密钥16位
var key = CryptoJS.enc.Utf8.parse(key);
//加密向量16位
var iv = CryptoJS.enc.Utf8.parse(iv);
var encrypted = CryptoJS.AES.encrypt(str, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
return encrypted;
}
</script>
<script>
layui.use('form', function () {
var form = layui.form;
form.render();
form.on('submit(LAY-user-login-submit)', function (data) {
//表单可使用var data = $(“form”).serialize();
var field = data.field;
var jsonstr=JSON.stringify(field);
var enjsonstr=encrypt(jsonstr,'abcdefgh12345678','abcdefgh12345678');
var enstr="'"+enjsonstr+"'";
$.post("{:url('admin/site/login')}",{key:enstr}, function (ret) {
//成功
if (ret.code == 0) {
layer.msg(ret.msg, { time: 1000 }, function () {
window.location.href = ret.url;
})
} else {
layer.msg(ret.msg, { time: 1000 });
setTimeout(function (){
window.location.reload();
}, 2000);
}
});
});
});
</script>
后端使用php获取数据
$input = input("request.", '', 'trim');
$obj=get_object_vars(decrypt($input['key'],'abcdefgh12345678','abcdefgh12345678'));
unset($input['key']);
$input=array_merge($input,$obj);
后端解密方法如下
function decrypt($data, $key, $iv) {
$decrypted = openssl_decrypt(base64_decode($data), 'AES-128-CBC', $key, true, $iv);
$json_str = rtrim($decrypted, "\0");
return json_decode($json_str);
}