每次登录系统的时候总是要输入烦人的验证码,那么我们今天就思考这个问题,为什么要有验证码这个功能?很多伙伴应该都知道:
验证码的种类
下面以三种不同的编程语言,通过代码生成验证码。
先看下Java
代码是如何生成验证码的。手动创建下面这个类,就可以生成验证码了。代码如下:
public class GenVerifyCodeUtils { private static char mapTable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; public static void main(String[] args) { OutputStream outputStream = new BufferedOutputStream(new ByteArrayOutputStream()); System.out.println(getImageCode(100,80,outputStream )); } public static Map<String, Object> getImageCode(int width, int height, OutputStream os) { Map<String,Object> returnMap = new HashMap<String, Object>(); if (width <= 0) width = 60; if (height <= 0) height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics g = image.getGraphics(); //生成随机类 Random random = new Random(); // 设定背景色 g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); //设定字体 g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); // 随机产生168条干扰线,使图像中的认证码不易被其它程序探测到 g.setColor(getRandColor(160, 200)); for (int i = 0; i < 168; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } //取随机产生的码 String strEnsure = ""; //4代表4位验证码,如果要生成更多位的认证码,则加大数值 for (int i = 0; i < 4; ++i) { strEnsure += mapTable[(int) (mapTable.length * Math.random())]; // 将认证码显示到图像中 g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); // 直接生成 String str = strEnsure.substring(i, i + 1); // 设置随便码在背景图图片上的位置 g.drawString(str, 13 * i + 20, 25); } // 释放图形上下文 g.dispose(); returnMap.put("image",image); returnMap.put("strEnsure",strEnsure); return returnMap; } static Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } }
这里我也用原生Js
写了一个生成验证码的工具,代码如下:
<form action="#"> <input type="text" id="input1" onblur="inputBlur()"/> <input type="text" onclick="createCode()" readonly="readonly" id="checkCode" class="unchanged" style="width: 80px;background: #660099"/><br /> </form> <script language="javascript" type="text/javascript"> var code; //在全局 定义验证码 var code2; //在全局 定义验证码 function createCode() { code = ""; var checkCode = document.getElementById("checkCode"); function RndNum(n) { var rnd = ""; for (var i = 0; i < n; i++) rnd += Math.floor(Math.random() * 10); return rnd; } var num = RndNum(2); var num2 = RndNum(2); code = num + "+" + num2 + "="; code2 = parseInt(num) + parseInt(num2) if (checkCode) { checkCode.className = "code"; checkCode.value = code; } } function inputBlur(){ var inputCode = document.getElementById("input1").value; if (inputCode.length <= 0) { alert("请输入验证码!"); } else if (inputCode != code2) { alert("验证码输入错误!"); createCode(); } else { alert("^-^ OK"); } } </script> <style type="text/css"> .code { font-family: Arial; font-style: italic; color: Red; border: 0; padding: 2px 3px; letter-spacing: 3px; font-weight: bolder; } .unchanged { border: 0; } </style>
效果如下:
代码如下:
# -*- coding: utf-8 -* from PIL import Image, ImageDraw, ImageFont, ImageFilter import random # 随机字母: def rndChar(): return chr(random.randint(65, 90)) # 随机颜色1: def rndColor(): return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) # 随机颜色2: def rndColor2(): return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)) # 240 x 60: width = 60 * 4 height = 60 image = Image.new('RGB', (width, height), (255, 255, 255)) # 创建Font对象: font = ImageFont.truetype('C:\Windows\Fonts\Arial.ttf', 36) # 创建Draw对象: draw = ImageDraw.Draw(image) # 填充每个像素: for x in range(width): for y in range(height): draw.point((x, y), fill=rndColor()) # 输出文字: for t in range(4): draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2()) # 模糊: image = image.filter(ImageFilter.BLUR) image.save('code.jpg', 'jpeg') image.show()
运行效果如下图:
本篇讲了为什么会有验证码这个东东,和市面上现在验证码的种类,简单给大家做了一下科普,最后分别以不同的编程语言,展示了生成验证码的过程。现在网络安全尤为重要,验证码这个功能虽小,但是不可不做!
本文分享自微信公众号 - 不安分的猿人(Restless-man),作者:不安分的猿人
原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。
原始发表时间:2020-06-08
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句