RSA应用(web登录加密解密)

javaweb项目加密js资源:

百度云盘地址:链接:https://pan.baidu.com/s/1fqZ5H0ToOwq2-om31p5eMQ
提取码:zt2j

数据加密:

//公钥
        var PUBLIC_KEY = '你的公钥';
        //使用公钥加密
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(PUBLIC_KEY);
        username = encrypt.encrypt(username);
        password = encrypt.encrypt(password);
        verifyCode = encrypt.encrypt(verifyCode);
        

注:这里主要对用户名、密码、验证码三组数据进行了单独加密,也可以将数据拼成json字符串进行加密。

java后台解密:

/**
     * 用户登录
     *
     * @param userName(用户名)
     * @param password(密码)
     * @param verifyCode(验证码)
     * @return String
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public void index(String userName, String password, String verifyCode,HttpServletRequest httpServletRequest) {
        userName = decipheringLoginStr(userName);
        password = decipheringLoginStr(password);
        verifyCode = decipheringLoginStr(verifyCode);
        System.out.println("userName:" + userName + ",password:" + ",verifyCode:" + verifyCode);
    }

private String decipheringLoginStr(String byte2Base64) {
        String privateDecryptStr ="";
        try {
            //将Base64编码后的私钥转换成PrivateKey对象
            PrivateKey privateKey = RSAUtil.string2PrivateKey("你的私钥");
            //加密后的内容Base64解码
            byte[] base642Byte = RSAUtil.base642Byte(byte2Base64);
            //用私钥解密
            byte[] privateDecrypt = RSAUtil.privateDecrypt(base642Byte, privateKey);
            //解密后的明文
            privateDecryptStr = new String(privateDecrypt);
            System.out.println("解密后的明文: " + privateDecryptStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(privateDecryptStr);
    }

注:RSAUtil见(https://blog.csdn.net/weixin_40277684/article/details/102371997)

你可能感兴趣的:(RSA应用)