关于nodejs中密码加密的处理

一、关于node加密模块crypto的介绍

其实就是使用MD5加密的,不太安全,在实际开发中根据自己的方案进行加盐处理

二、在路由视图中使用加密方式

  • 1、导入node自带的加密模块(不需要安装)

    //导入加密模块
    const crypto = require("crypto");
  • 2、做一个用户注册,密码加密的视图

    <div class="col-md-6">
        <h4>用户注册</h4>
        <form role="form" method="post" action="/regest">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input id="username" type="text" placeholder="请输入用户名" name="username" class="form-control"/>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input id="password" type="password" placeholder="请输入密码" name="password" class="form-control"/>
            </div>
            <div class="form-group">
                <input type="submit" value="提交" class="btn btn-success"/>
            </div>
        </form>
    </div>
    router.post("/regest",(req,res)=>{
        console.log(req.body);
        let name = req.body.username;
        let password = req.body.password;
        let md5 = crypto.createHash("md5");
        let newPas = md5.update(password).digest("hex");
        db("insert into user1(name,password) values(?,?)",[name,newPas],(err,data)=>{
            if (err){
                res.send("注册失败");
            }
            console.log(data);
            if (data){
                res.send("注册成功");
            }
        })
    });

三、用户登录进行密码校验

  • 1、把用户输入的密码用同样的方式加密处理
  • 2、把加密后的密码与数据库中匹配

    router.post("/login",(req,res)=>{
        let name = req.body.username;
        let password = req.body.password;
        let md5 = crypto.createHash("md5");
        let newPas = md5.update(password).digest("hex");
        db("select * from user1 where name = ?",[name],(err,data)=>{
            console.log(data[0].password);
            if (err){
                res.send("发生错误");
            }
            if (data){
                if (data[0].password === newPas){
                    res.send("登录成功");
                }else {
                    res.send("用户名或密码错误");
                }
            }
        })
    })
    <div class="col-md-6">
        <h4>用户登录</h4>
        <form role="form" method="post" action="/login">
            <div class="form-group">
                <label for="username2">用户名:</label>
                <input id="username2" type="text" placeholder="请输入用户名" name="username" class="form-control"/>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input id="password" type="password" placeholder="请输入密码" name="password" class="form-control"/>
            </div>
            <div class="form-group">
                <input type="submit" value="提交" class="btn btn-success" id="sub-btn2"/>
            </div>
        </form>
    </div>

四、扩展(一般我们加密处理)

  • 1、利用随机数随机生成多少位数
  • 2、利用可逆加密把第一步的生成的随机数加密
    • 可逆加密有Base64Hex加密(具体自己百度)
  • 3、将第二步加密好的随机数与我们真实密码拼接在一起
  • 4、将第三步进行加密(MD5)
  • 5、将第四步进行可逆加密
  • 6、将第二步与第五步生成的拼接成密码

五、扩展(一般我们加密的登录)

  • 1、登录时候获取密码
  • 2、从获取的密码中截取随机数加密的那段
  • 3、重复操作上面加密的方式(3,4,5,6)

六、关于正常项目中开发加密的方式代码正确的加密方式

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
凯撒密码是一种基于字母移位的代换密码,通过将字母移动一定的位数来进行加密和解密。在Node.js,可以使用crypto模块的Cipher类和Decipher类来实现凯撒密码加密和解密操作。 首先,使用Cipher类进行加密操作。可以使用crypto模块的createCipher方法创建一个Cipher对象,并指定加密算法和密钥。然后,可以使用Cipher对象的update方法传入要加密的数据,并指定编码格式,最后使用final方法获取加密完成的数据。 例如,下面的代码演示了使用凯撒密码对明文进行加密: ``` const crypto = require('crypto'); function caesarCipherEncrypt(text, shift) { const cipher = crypto.createCipher('aes192', 'secret_key'); let encrypted = cipher.update(text, 'utf8', 'hex'); encrypted += cipher.final('hex'); return encrypted; } const plaintext = 'Hello, World!'; const shift = 3; const ciphertext = caesarCipherEncrypt(plaintext, shift); console.log(ciphertext); ``` 接下来,使用Decipher类进行解密操作。同样,可以使用crypto模块的createDecipher方法创建一个Decipher对象,并指定解密算法和密钥。然后,可以使用Decipher对象的update方法传入要解密的数据,并指定编码格式,最后使用final方法获取解密完成的数据。 例如,下面的代码演示了使用凯撒密码对密文进行解密: ``` function caesarCipherDecrypt(ciphertext, shift) { const decipher = crypto.createDecipher('aes192', 'secret_key'); let decrypted = decipher.update(ciphertext, 'hex', 'utf8'); decrypted += decipher.final('utf8'); return decrypted; } const decryptedText = caesarCipherDecrypt(ciphertext, shift); console.log(decryptedText); ``` 请注意,这只是凯撒密码的一个简单示例,实际应用可能需要更复杂的加密算法和密钥管理方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水痕01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值