⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randomchar.java

📁 struts spring hibernate 自已写的
💻 JAVA
字号:
package com.hb.gf.mycode;

/**
 * Title: 随机码图形生成程序的处理 Description:
 * 为了加强网络安全,防止黑课工具对系统的攻击,现在通用随机码来解决这个问题,因为每一次产生的随机码是随机的,所以使用穷举法破解密码的工具就无能为力了
 * Copyright: Copyright (c) 2004-2010 Company: SDLDAP
 * 
 * @author LiChanglai
 * @version 1.0
 */
import java.util.*;
import java.awt.*;
import java.awt.image.*;
public class RandomChar {
 /**
  * 给定范围获得随机颜色
  * 
  * @param fc
  *            int 参数1
  * @param bc
  *            int 参数2
  * @return Color 返回的color对象
  */
 private String strRandomString; // 生成的随机数
 /**
  * 取其生成的随机数
  * 
  * @return String
  */
 public String getRandomString() {
  return strRandomString;
 }
 /**
  * 设置其生成的随数
  * 
  * @param randomString
  *            String
  */
 public void setRandomString(String randomString) {
  this.strRandomString = randomString;
 }
 /**
  * 取其某一范围的color
  * 
  * @param fc
  *            int 范围参数1
  * @param bc
  *            int 范围参数2
  * @return Color
  */
 private 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);
 }
 /**
  * 用于生成其随机数
  * 
  * @param randomCount
  *            int 随机数总长度
  * @return String 返回的随机数
  */
 private String getRandomString(int nRandomCount) {
  /*
   * String[] a = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
   * "B", "C", "D", "E", "F", "G", "H", "I", "G", "K", "L", "M", "N", "O",
   * "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c",
   * "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
   * "r", "s", "t", "u", "v", "w", "x", "y", "z" };
   */
  String[] a = { "a", "b", "c", "d", "e", "f", "g", "h", "g", "k", "m",
    "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
  String strRand = "";
  int nRand;
  // 取其随机数的总数
  int LengthOfRandom = a.length;
  // 生成随机类
  Random random = new Random();
  // 循环取随机取其整数,并将其整数所对应的结果进行累加
  for (int i = 0; i < nRandomCount; i++) {
   nRand = random.nextInt(LengthOfRandom);
   strRand += a[nRand];
  }
  // 返回累加后的结果
  return strRand;
 }
 /**
  * 生成其随机的图形
  * 
  * @param width
  *            int 生成图形的宽度
  * @param height
  *            int 生成图形的高度
  * @param nLengthOfRandom
  *            int 随机码的个数
  * @param nDisturbLineCount
  *            int 干扰线的条数
  * @return BufferedImage
  */
 public BufferedImage getRandomImage(int nWidth, int nHeight,
   int nLengthOfRandom, int nDisturbLineCount) 
 {
  // 在内存中创建图象
  BufferedImage image = new BufferedImage(nWidth, nHeight,
    BufferedImage.TYPE_INT_RGB);
  // 获取图形上下文
  Graphics g = image.getGraphics();
  String strRand = "";
  nLengthOfRandom = (nLengthOfRandom <= 0) ? 6 : nLengthOfRandom;
  nDisturbLineCount = (nDisturbLineCount <= 0) ? 0 : nDisturbLineCount;
  // 生成随机类
  Random random = new Random();
  // 设定背景色
  g.setColor(getRandColor(200, 250));
  g.fillRect(0, 0, nWidth, nHeight);
  // 设定字体
  g.setFont(new Font("Times New Roman", Font.PLAIN, 16));
  // 画边框
  g.setColor(new Color(255, 255, 255));
  g.drawRect(0, 0, nWidth - 1, nHeight - 1);
  // 随机产生nDisturbLineCount条干扰线,使图象中的认证码不易被其它程序探测到
  g.setColor(getRandColor(160, 200));
  for (int i = 0; i < nDisturbLineCount; i++) {
   int x = random.nextInt(nWidth);
   int y = random.nextInt(nHeight);
   int xl = random.nextInt(12);
   int yl = random.nextInt(12);
   g.drawLine(x, y, x + xl, y + yl);
  }
  // 取随机产生的认证码(6位)
  strRand = this.getRandomString(nLengthOfRandom);
  // 设置其属性randomString的值
  this.strRandomString = strRand;
  for (int i = 0; i < nLengthOfRandom; i++) {
   // 将认证码显示到图象中
   g.setColor(new Color(20 + random.nextInt(110), 20 + random
     .nextInt(110), 20 + random.nextInt(110)));
   // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
   g.drawString(strRand.substring(i, i + 1), 13 * i + 5, 14);
  }
  // 图象生效
  g.dispose();
  return image;
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -