import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
import javax.imageio.*;
import java.awt.font.*;
import java.awt.geom.*;
/**
* JAVASecret security card generates
* @author Administrator
* 包含,随机获得的64位字母与数字组成的密保卡,并生成图片(图片地址:c盘)
* 输入密保值,是否与生成的密保卡中的值对应
*/
public class CreatePasswordProtection {
public static void main(String[] args) throws Exception {
int width = 400;
int height = 300;
String title = "唯一密保卡";
String index = "密保卡序列号 : ";
String[] xindex = {"1","2","3","4","5","6","7","8"};
String[] yindex = {"A","B","C","D","E","F","G","H"};
String[] xyindex = {"—","|"};
String[] strs = new String[]{"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","0","1","2","3","4","5","6","7","8","9"};
String[] strs_y = new String[]{"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[] strs_x = new String[]{"0","1","2","3","4","5","6","7","8","9"};//数字字符串数组
String str[][] = new String[8][8];//存储密保卡字符串的二维数组
String strs2 = "";
String card = "";//密保卡字符串
String inputStr = "";//输入的索引标示
String[] inputArr = new String[4];//得到输入的索引标示数组
int[] yinput = new int[4];//得到输入的索引对应的数字数组
int[] xinput = new int[4];
boolean flag = false;
/////////////////////////////////////////////////////////////////
Random random = new Random();
File file = new File("c:/image.jpg");
Font font = new Font("Serif", Font.BOLD, 14);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.RED);
/////////////////////////////////////////////////////////////////
FontRenderContext context0 = g2.getFontRenderContext();
Rectangle2D bounds0 = font.getStringBounds(index, context0);
double x0 = 2;
double y0 = 2;
double ascent0 = -bounds0.getY();
double baseY0 = y0 + ascent0;
g2.drawString(title, (int)x0, (int)baseY0);
/////////////////////////////////////////////////////////////////
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(index, context);
double x = (width - 350);
double y = (height - 280);
double ascent = -bounds.getY();
double baseY = y + ascent;
index = index+(random.nextInt(8999)+1000)+" "+(random.nextInt(8999)+1000)+" "+(random.nextInt(8999)+1000)+" "+(random.nextInt(8999)+1000);
g2.drawString(index, (int)x, (int)baseY);
/////////////////////////////////////////////////////////////////
for (int i = 0; i < xindex.length; i++) {
double xx = (width - 350) + 40*(i+1);
double yx = (height - 280) + 30;
g2.drawString(xindex[i], (int)xx, (int)yx);
}
/////////////////////////////////////////////////////////////////
for (int i = 0; i < yindex.length; i++) {
double xy = (width - 350) - 10;
double yy = (height - 280) + 30 + 30*(i+1);
g2.drawString(yindex[i], (int)xy, (int)yy);
}
/////////////////////////////////////////////////////////////////
for (int j = 0; j < 9; j++) {
for (int i = 0; i < 32; i++) {
double xx = (width - 350) + 9+ 10*(i+1);
double yx = (height - 280) + 40 + 30*j;
g2.drawString(xyindex[0], (int)xx, (int)yx);
}
}
/////////////////////////////////////////////////////////////////
for (int j = 0; j < 9; j++) {
for (int i = 0; i < 24; i++) {
double xx = (width - 350) + 18 + 40*j;
double yx = (height - 280) + 34 + 10*(i+1);
g2.drawString(xyindex[1], (int)xx, (int)yx);
}
}
/////////////////////////////////////////////////////////////////
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
int in = (int)(Math.random()*strs.length);
strs2 = strs[in];
card = card + strs2 + ",";
double xx = (width - 350) - 10 + 40*(i+1);
double yx = (height - 280) + 50 + 30*j;
g2.drawString(strs2, (int)xx, (int)yx);
str[j][i] = strs2;
}
card = card + "|";
}
System.out.println(card);
ImageIO.write(bi, "jpg", file);
for(int i = 0 ; i < 4 ; i++){
inputStr = getInputStr(xindex, yindex);
inputArr[i] = inputStr;
}
Scanner input = new Scanner(System.in);
for(int i = 0 ; i < inputArr.length ; i++){
System.out.println("----------------- 第"+(i+1)+"个密保下标:"+inputArr[i]+" -----------------");
String letter1 = getYindex(inputArr[i]);
String letter2 = getXindex(inputArr[i]);
String outStr = input.next();
System.out.println("letter1:"+letter1+"letter2:"+letter2+"outStr:"+outStr);
int y1 = 0;
int x1 = 0;
for(int j = 0 ; j < yindex.length ; j++){
if(letter1.equals(yindex[j])){
yinput[i] = j;
y1 = j;
}
if(letter2.equals(xindex[j])){
xinput[i] = j;
x1 = j;
}
}
System.out.println("y1:"+y1+"x1:"+x1);
boolean tof = getBoolean(str,x1,y1,outStr);
if(tof){
flag = true;
System.out.println("第"+(i+1)+"个密保验证输入正确!");
}else{
flag = false;
System.out.println("第"+(i+1)+"个密保验证输入错误!");
}
}
if(flag){
System.out.println("您的密保验证全部正确!");
}else{
System.out.println("您的密保验证有错误!");
}
}
/**
* 得到随机一个英文和一个数字搭配的字符串
* @param xindex 英文字母组成的数组(A~Z)
* @param yindex 数字组成的数组(0~9)
* @return
*/
public static String getInputStr(String[] xindex,String[] yindex){
String inputStr = "";
int in1 = (int)(Math.random()*xindex.length);
int in2 = (int)(Math.random()*yindex.length);
inputStr = xindex[in1] + yindex[in2];
return inputStr;
}
/**
* 得到随机一个英文和一个数字搭配的字符串中的英文字母
* @param str 随机一个英文和一个数字搭配的字符串
* @return
*/
public static String getYindex(String str){
String letter = str.charAt(1)+"";
return letter;
}
/**
* 得到随机一个英文和一个数字搭配的字符串中的数字
* @param str 随机一个英文和一个数字搭配的字符串
* @return
*/
public static String getXindex(String str){
String letter = str.charAt(0)+"";
return letter;
}
/**
* 判断输入的密保卡验证值是否与密保卡中的验证值一一对应
* @param card 密保卡字符串二维数组
* @param xinput 随机英文与数字搭配的字符串中的数字在数字字符串数组中的下标
* @param yinput 随机英文与数字搭配的字符串中的英文字母在英文字母字符串数组中的下标
* @param outStr 输入的密保值
* @return
*/
public static boolean getBoolean(String[][] card,int xinput,int yinput,String outStr){
boolean flag = false;
for(int x = 0 ; x < 64 ; x++){
if(outStr.equals(card[yinput][xinput])){
flag = true;
break;
}else{
flag = false;
}
}
return flag;
}
}
- 1
- 2
前往页