当前位置:   article > 正文

前后端项目怎么进行MD5加密?_前后端md5加密

前后端md5加密

MD5加密

在前后端分离项目中

如果后端数据不进行Md5加密,那数据库中存放非密码就是明文,如果攻击者去攻击数据库,就会获得用户密码

如果前端不进行Md5加密,那在异步传输的过程中,攻击者就有可能获取传输的明文密码,也会导致密码泄露。

所以,我们要对前后端都进行md5加密



后端

spring 自带 md5 加密

req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
  • 1

image-20210408161354119



前端

  1. 在 public 中,新建一个 js 文件夹,再新建一个 md5.js 工具类:

image-20210408161609835

  1. md5.js 内容如下:
var KEY = "!@#QWERT";
/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hexMd5(s) {
   
    return hex_md5(s);
}
function hex_md5(s){
    return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){
    return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){
    return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) {
    return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) {
    return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) {
    return binl2str(core_hmac_md5(key, data)); }

/*
 * Perform a simple self-test to see if the VM is working
 */
function md5_vm_test()
{
   
    return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length
 */
function core_md5(x, len)
{
   
    /* append padding */
    x[len >> 5] |= 0x80 << ((len) % 32);
    x[(((len + 64) >>> 9) << 4) + 14] = len;

    var a =  1732584193;
    var b = -271733879;
    var c = -1732584194;
    var d =  271733878;

    for(var i = 0; i < x.length; i += 16)
    {
   
        var olda = a;
        var oldb = b;
        var oldc = c;
        var oldd = d;

        a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
        d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
        c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
        b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
        a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
        d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
        c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
        b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/974781
推荐阅读
相关标签
  

闽ICP备14008679号