注册验证及注册机.docx
《注册验证及注册机.docx》由会员分享,可在线阅读,更多相关《注册验证及注册机.docx(19页珍藏版)》请在冰豆网上搜索。
注册验证及注册机
Java实现注册验证及破解注册码的注册机
软件测试.094
谭又中
2011/09/25
(共12页)
注册验证:
界面:
源码:
packageregret;
importjava.awt.Button;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Frame;
importjava.awt.Label;
importjava.awt.Panel;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.math.BigInteger;
importjava.util.HashMap;
importjava.util.Random;
importjavax.swing.ImageIcon;
importjavax.swing.JOptionPane;
publicclassRegretVerifyextendsFrameimplementsActionListener
{
/****************成员变量*********************/
privateFontfont=newFont("宋体",Font.PLAIN,15);
privatePanelpanel1=newPanel();
privateLabellabel1=newLabel("生成码:
");
privateLabellabel2=newLabel("注册码:
");
privateTextFieldtextFiled1=newTextField();
privateTextFieldtextFiled2=newTextField();
privateButtonbutton1=newButton("注册");
privateButtonbutton2=newButton("取消");
/****************构造方法*********************/
privateRegretVerify(Stringtitle,StringiconPath)
{
this.setTitle(title);
this.setSize(420,150);
this.setBackground(Color.WHITE);
this.setLayout(null);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setIconImage(newImageIcon(iconPath).getImage());
this.panel1.setSize(420,150);
this.label1.setBounds(20,40,45,25);
this.label2.setBounds(20,75,45,25);
this.textFiled1.setBounds(70,40,320,25);
this.textFiled1.setEditable(false);
this.textFiled1.setFont(font);
this.textFiled2.setBounds(70,75,320,25);
this.textFiled2.setFont(font);
this.button1.setBounds(85,110,80,25);
this.button2.setBounds(255,110,80,25);
this.textFiled1.setText(createCode());
this.button1.addActionListener(this);
this.button2.addActionListener(this);
this.panel1.setLayout(null);
this.panel1.add(label1);
this.panel1.add(label2);
this.panel1.add(textFiled1);
this.panel1.add(textFiled2);
this.panel1.add(button1);
this.panel1.add(button2);
this.add(panel1);
//注册窗口关闭事件
this.addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
this.setVisible(true);
}
/****************按钮事件监听*********************/
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource().equals(button1))
{
System.out.println(handleString(textFiled1.getText()));
if(handleString(textFiled1.getText()).equals(textFiled2.getText()))
{
JOptionPane.showMessageDialog(null,"恭喜你!
注册成功!
","提示",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null,"注册码错误!
","提示",JOptionPane.WARNING_MESSAGE);
}
}
elseif(e.getSource().equals(button2))
{
this.dispose();
}
}
/*******************产生生成码*********************/
//生成随机的生成码
privateStringcreateCode()
{
StringmachineCode="";
StringselectionStr="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Randomrandom=newRandom();
inttimes=0;
for(inti=1;i<=32;i++)
{
if(times==4)//生成结果每四个字符用“-”分隔
{
machineCode+="-"+selectionStr.charAt(random.nextInt(36));
times=0;
}
else
{
machineCode+=selectionStr.charAt(random.nextInt(36));
}
times++;
}
returnmachineCode;
}
/*****************核心算法*********************/
//将字符串的每个字符的ASC码转换为二进制字符串
privateStringstrToBinary(Stringstr)
{
StringbinStr="";
for(inti=0;i{
charc=str.charAt(i);//遍历字符串的每个字符
if(Integer.toBinaryString(c).length()==8)
{
binStr+=Integer.toBinaryString(c);
}
switch(Integer.toBinaryString(c).length())
{
case7:
binStr+="0"+Integer.toBinaryString(c);
break;
case6:
binStr+="00"+Integer.toBinaryString(c);
break;
case5:
binStr+="000"+Integer.toBinaryString(c);
break;
case4:
binStr+="0000"+Integer.toBinaryString(c);
break;
case3:
binStr+="00000"+Integer.toBinaryString(c);
break;
case2:
binStr+="000000"+Integer.toBinaryString(c);
break;
case1:
binStr+="0000000"+Integer.toBinaryString(c);
break;
default:
binStr+=Integer.toBinaryString(c);
break;
}
}
returnbinStr;
}
//处理一行字符串方法
privateStringhandleString(Stringstr)
{
StringbinStr=strToBinary(str);//转换为对应ASC码的二进制字符串
StringfindValueInTable="";
//对二进制字符串长度处理
if(binStr.length()%24==0)
{
findValueInTable=binChangeToChar(binStr);
}
if(binStr.length()%24==8)
{
binStr+="0000";
findValueInTable=binChangeToChar(binStr)+"==";
}
if(binStr.length()%24==16)
{
binStr+="00";
findValueInTable=binChangeToChar(binStr)+"=";
}
returnfindValueInTable;//调用核心处理方法返回最终结果
}
//将二进制字符串转换为十进制数据,并拆分为6位一段进行查参照表,将查找到的字符累加返回
privateStringbinChangeToChar(StringbinStr)
{
StringfindValueInTable="";
HashMaphashTable=createTable();//创建参照表
String[]binStrArr=binStrToArr(binStr);
//将二进制字符串转换为十进制字符串
inttimes=0;
for(inti=0;i{
BigIntegerbigInt=newBigInteger(binStrArr[i],2);//大数整型对象
StringdecStr=bigInt.toString(10);//转换为指定进制的字符串
//将最终查表返回的字符串进行拆分为长度为4的字串,并以“_”分隔
if(times==4)
{
findValueInTable+="-"+hashTable.get(Integer.parseInt(decStr));
times=0;
}
else
{
findValueInTable+=""+hashTable.get(Integer.parseInt(decStr));
}
times++;
}
returnfindValueInTable.substring(0,39);//由于返回长度大于生成码,截取和生成码长度相同的字符
}
//将二进制串转换为每个元素长度为6个字符的数组
privateString[]binStrToArr(StringbinStr)
{
String[]binStrArr=newString[binStr.length()/6];
for(inti=0,j=0;i{
if(i{
binStrArr[j]=binStr.substring(i,i+6);
}
else
{
binStrArr[j]=binStr.substring(i);
}
}
returnbinStrArr;
}
//建立映射表(用哈希Map)
privateHashMapcreateTable()
{
HashMaphashTable=newHashMap();
charvalue='A';
for(inti=0;i<=9;i++)//0-9存储A-J
{
hashTable.put(i,value);
value++;
}
value='0';
for(inti=10;i<=19;i++)//10-19存储0-9
{
hashTable.put(i,value);
value++;
}
value='K';
for(inti=20;i<=34;i++)//20-34存储K-Y
{
hashTable.put(i,value);
value++;
}
value='Z';
for(inti=35;i<=60;i++)//35-60存储Z-A
{
hashTable.put(i,value);
value--;
}
//61-63存储B-D
hashTable.put(61,'B');
hashTable.put(62,'C');
hashTable.put(63,'D');
returnhashTable;
}
/***************供外界直接调用的方法*********************/
publicstaticvoidcreate(Stringtitle,StringiconPath)
{
newRegretVerify(title,iconPath);
}
/***************用于测试的主方法*********************/
publicstaticvoidmain(String[]args)
{
create("注册","regret/1.jpg");
}
}
注册机(破解上面注册码的):
界面:
源码:
packageregret;
importjava.awt.Button;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Frame;
importjava.awt.Label;
importjava.awt.Panel;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.math.BigInteger;
importjava.util.HashMap;
importjavax.swing.ImageIcon;
importjavax.swing.JOptionPane;
publicclassRegretMachineextendsFrameimplementsActionListener
{
/**************成员变量*********************/
privateFontfont=newFont("宋体",Font.PLAIN,15);
privatePanelpanel1=newPanel();
privateLabellabel1=newLabel("生成码:
");
privateLabellabel2=newLabel("注册码:
");
privateTextFieldtextFiled1=newTextField();
privateTextFieldtextFiled2=newTextField();
privateButtonbutton1=newButton("生成注册码");
privateButtonbutton2=newButton("关闭");
/*************构造方法*********************/
privateRegretMachine(StringiconPath)
{
this.setTitle("注册机");
this.setSize(420,150);
this.setBackground(Color.WHITE);
this.setLayout(null);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setIconImage(newImageIcon(iconPath).getImage());
this.panel1.setSize(420,150);
this.label1.setBounds(20,40,45,25);
this.label2.setBounds(20,75,45,25);
this.textFiled1.setBounds(70,40,320,25);
this.textFiled1.setFont(font);
this.textFiled2.setBounds(70,75,320,25);
this.textFiled2.setEditable(false);
this.textFiled2.setFont(font);
this.button1.setBounds(85,110,80,25);
this.button2.setBounds(255,110,80,25);
this.button1.addActionListener(this);
this.button2.addActionListener(this);
this.panel1.setLayout(null);
this.panel1.add(label1);
this.panel1.add(label2);
this.panel1.add(textFiled1);
this.panel1.add(textFiled2);
this.panel1.add(button1);
this.panel1.add(button2);
this.add(panel1);
//注册窗口关闭事件
this.addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
this.setVisible(true);
}
/***************按钮事件监听*********************/
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource().equals(button1))
{
if(textFiled1.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"请输入生成码!
","提示",JOptionPane.WARNING_MESSAGE);
}