• Base64与MD5(数据加密)与ValidateCode(验证码)


    Base64与MD5

    Base64

    作用:对数据进行加密解密的类,由JDK提供

    加密

    public static String encode(String str){
        String s =Base64.getEncoder().encodeToString(str.getBytes());
        return s;
    }
    
    • 1
    • 2
    • 3
    • 4

    解密

    public static String decode(String str){
        byte[] bytes = Base64.getDecoder().decode(str);
        String s = new String(bytes);
        return s;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    封装成工具类

    import java.util.Base64;
    public class Base64Utils {
        public static String encode(String str){
            String s = Base64.getEncoder().encodeToString(str.getBytes());
            return s;
        }
        public static String decode(String str){
            byte[] bytes = Base64.getDecoder().decode(str);
            String s = new String(bytes);
            return s;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    MD5

    作用:对数据进行加密,由JDK提供

    加密

    public static String encode(String str){
        try {
            MessageDigest messageDigest=MessageDigest.getInstance("MD5");
            messageDigest.update(str.getBytes("utf-8"));
            byte[] digest = messageDigest.digest();
            BigInteger bigInteger=new BigInteger(1, digest);
            String secret=bigInteger.toString(16);
            return secret;
        } catch (Exception e) {
        e.printStackTrace();
        }
        return null;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    其封装成工具类

    import java.math.BigInteger;
    import java.security.MessageDigest;
    
        public class MD5Utils {
        public static String encode(String str){
            try {
                MessageDigest messageDigest=MessageDigest.getInstance("MD5");
                messageDigest.update(str.getBytes("utf-8"));
                byte[] digest = messageDigest.digest();
                BigInteger bigInteger=new BigInteger(1, digest);
                String secret=bigInteger.toString(16);
                return secret;
            } catch (Exception e) {
            e.printStackTrace();
            }
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Beas64与MD5区别

    Base64:

    1、可逆性。

    2、可以将图片等二进制文件转换为文本文件。

    3、可以把非ASCII字符的数据转换成ASCII字符,避免不可见字符。

    MD5:

    1、不可逆性。

    2、任意长度的明文字符串,加密后得到的密文字符串是长度固定的

    验证码

    因为验证码的编写比较复杂,使用jar包(需要自行导入jar包)

    ValidateCode.jar

    生成验证码:

    //步骤1,创建ValidateCode对象
    //1参:生成图片宽度
    //2参:生成图片高度
    //3参:验证码位数
    //4参:影响线数量
    ValidateCode validateCode = new ValidateCode(width, height, codeCoun,
    lineCount);
    //步骤2,获取生产的验证码
    String 变量名 = validateCode.getCode();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    并通过response输出验证码

    validateCode.write(response.getOutputStream());
    
    • 1

    示例

    import cn.dsna.util.images.ValidateCode;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    @WebServlet("/code")
    public class CodeServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse
            resp) throws ServletException, IOException {
            doPost(req,resp);
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse
            resp) throws ServletException, IOException {
            ValidateCode validateCode = new ValidateCode(100, 30, 4, 10);
            String code = validateCode.getCode();
            req.getSession().setAttribute("code",code);
            validateCode.write(resp.getOutputStream());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    前端代码

    <img src="code" id="btn_getcode">
    
    • 1

    切换验证码

    注意:引入浏览器缓存的原因,如果我们访问的地址没有发生改变浏览器不会为我们在此发起请求.

    所以我们在请求地址后加一个可变的参数(可以加一个随机数),该参数无须被服务器获取.只是防止因浏览器缓存请求被拦截

    $("#btn_getcode").click(function () {
        // $("#btn_getcode").attr("src","code?d="+new Date().getTime());
        //或
        $("#btn_getcode").attr("src","code?d="+Math.random());
    });
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    linux常用命令(2):tar命令(压缩文件/解压缩文件)
    开源酒店管理系统(C/S模式)
    计算机毕业设计——农产品资源展示平台
    漏刻有时数据可视化Echarts组件开发(32):温度计2
    单片机练习题3
    Python 的字符串
    一、认识STM32
    HelloGitHub 社区动态,开启新的篇章!
    网工内推 | 应届、大专可投,IE认证优先,有年终奖、带薪年假
    python基于PHP+MySQL读书分享平台
  • 原文地址:https://blog.csdn.net/qq761358496/article/details/128161885