博客
关于我
用C#生成随机中文汉字验证码的基本原理
阅读量:345 次
发布时间:2019-03-04

本文共 4675 字,大约阅读时间需要 15 分钟。

???????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????

1980??????????????????????GB2312-80?????????????????????GB18030-2000????????????????????????GB2312-1980?GB13000-1993?????????????????????????????GB18030?????Windows?????????????????????????????GB2312?????????????????????????????????????????????GB2312?????????????????????????????????????????????????????

??????????????????????????????????????????????????GB2312???????????????????ba c3???ba?????c3?????????????????????15??AF??????????????????????16??B0???????GB2312?????????

?.NET??????????????System.Text??????Encoding???????????????????????????????Encoding.GetBytes()????????????????Encoding.GetString()??????????????????????GB2312?????????????????????

using System;using System.Text;namespace ConsoleApplication{    class ChineseCode    {        public static object[] CreateRegionCode(int strlength)        {            string[] rBase = new string[16];            Random rnd = new Random((DateTime.Now.Ticks) + i);                        object[] bytes = new object[strlength];                        for (int i = 0; i < strlength; i++)            {                int r1 = rnd.Next(11, 14);                string str_r1 = rBase[r1].Trim();                int r2 = rnd.Next(0, 7);                if (r1 == 13)                {                    r2 = rnd.Next(0, 16);                }                else if (r1 == 15)                {                    r2 = rnd.Next(0, 16);                }                else                {                    r2 = rnd.Next(0, 16);                }                string str_r2 = rBase[r2].Trim();                                byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);                byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);                                byte[] str_r = new byte[] { byte1, byte2 };                bytes.SetValue(str_r, i);            }            return bytes;        }                public static void Main()        {            Encoding gb = Encoding.GetEncoding("gb2312");            object[] bytes = CreateRegionCode(4);                        string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));            string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));            string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));            string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));                        Console.WriteLine(str1 + str2 + str3 + str4);        }    }}

??????????????????????????????GetString()???????????????????????????????????????????

?????????C#????????

using System;using System.Text;namespace ConsoleApplication{    class ChineseCode    {        public static object[] CreateRegionCode(int strlength)        {            string[] rBase = new string[16];            Random rnd = new Random((DateTime.Now.Ticks) + i);                        object[] bytes = new object[strlength];                        for (int i = 0; i < strlength; i++)            {                int r1 = rnd.Next(11, 14);                string str_r1 = rBase[r1].Trim();                int r2 = rnd.Next(0, 7);                if (r1 == 13)                {                    r2 = rnd.Next(0, 16);                }                else if (r1 == 15)                {                    r2 = rnd.Next(0, 16);                }                else                {                    r2 = rnd.Next(0, 16);                }                string str_r2 = rBase[r2].Trim();                                byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);                byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);                                byte[] str_r = new byte[] { byte1, byte2 };                bytes.SetValue(str_r, i);            }            return bytes;        }                public static void Main()        {            Encoding gb = Encoding.GetEncoding("gb2312");            object[] bytes = CreateRegionCode(4);                        string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));            string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));            string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));            string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));                        Console.WriteLine(str1 + str2 + str3 + str4);        }    }}

????????????????????????????????????????????????GB2312?????????????????????????B?C?D???????????????????

????????????????????????????????????????????????

转载地址:http://wfje.baihongyu.com/

你可能感兴趣的文章
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>