Advertisement
T1T4N

Keygen Example

Nov 9th, 2011
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.util.*;
  4. import javax.swing.*;
  5.  
  6. //Credits to Dr0ax@leetcoders.org, for coding the keygen example
  7. @SuppressWarnings("serial")
  8. public class Keygen extends JPanel implements ActionListener{
  9.     JTextField key_field = new JTextField(19);
  10.     JButton generate = new JButton("Generate");
  11.     JButton chk = new JButton("Check key");
  12.     Random r = new Random();
  13.  
  14.     public Keygen() {
  15.         this.add(key_field);
  16.         this.add(chk);
  17.         this.add(generate);
  18.         generate.addActionListener(this);
  19.        
  20.         chk.addActionListener(new ActionListener() {
  21.             public void actionPerformed(ActionEvent arg0) {
  22.                 String chkStr = key_field.getText();
  23.                 String[] parts = chkStr.split("-");
  24.                 boolean valid = false;
  25.                 //first check: if there are 4 parts total
  26.                 if(parts.length != 4)
  27.                     valid = false;
  28.                 else{
  29.                    
  30.                     //loop through the four parts of the string
  31.                     outloop: for (int i = 0; i < parts.length; i++)
  32.                     {
  33.                         //second check: break if the part size is not 4 characters total
  34.                         if(parts[i].length() != 4){
  35.                             valid = false;
  36.                             break;
  37.                         }
  38.                        
  39.                         else{
  40.                             //loop through each character of the string
  41.                             for(int x = 0; x < parts[i].length(); x++) {
  42.                                 //get the integer value of the char
  43.                                 int cv = (int)parts[i].charAt(x);
  44.                                
  45.                                 //substract 48 from the number
  46.                                 int rndVal = cv - 48;
  47.                                
  48.                                 //check the number against the algorithm
  49.                                 if (((rndVal >= 0 && rndVal <= 9) || (rndVal >= 17 && rndVal <= 42)) /*|| (rndVal >= 49 && rndVal <= 74)*/  && isPrime(rndVal)) {
  50.                                     valid = true;
  51.                                 }
  52.                                 else{
  53.                                     //if not valid, set the flag and exit the loop
  54.                                     //you have to use a label (in my case "outloop") because you can't break a nested loop with just the break command
  55.                                     valid = false;
  56.                                     break outloop;
  57.                                 }
  58.                             }
  59.                         }
  60.                     }
  61.                 }
  62.                
  63.                 if(valid == true)
  64.                 key_field.setText("Valid Key!");
  65.                 else{key_field.setText("Not a valid key");}
  66.             }
  67.         });
  68.         }
  69.    
  70.    
  71.     public static void main(String[] args){
  72.         JFrame f = new JFrame("Keygen Example");
  73.         f.setLocation(100,100);
  74.         f.setDefaultCloseOperation(3);
  75.         f.setSize(250,100);
  76.         f.add(new Keygen());
  77.         f.setVisible(true);
  78.     }
  79.    
  80.     boolean isPrime(int num){
  81.         int i;
  82.         boolean xD = false;
  83.         for (i=2; i < num ;i++ ){
  84.             int n = num%i;
  85.             if (n==0){
  86.                 xD = false;
  87.                 break;
  88.             }
  89.           }
  90.           if(i == num)
  91.               xD = true;
  92.           return xD;
  93.     }
  94.          
  95.     @Override
  96.     public void actionPerformed(ActionEvent arg0) {
  97.         int x = 0;
  98.         int math = 48;
  99.         String nStr = "";
  100.         boolean valid;
  101.         for (int i = 0; i < 19; i++) {
  102.             if(i == 4 || i == 9 || i ==14) {
  103.                 nStr += "-";
  104.             }
  105.             else{
  106.                 valid = false;
  107.                 while(!valid){
  108.                     x = r.nextInt(74) +1;
  109.                     if (/*this makes x only available to hold the ASCII values of the numbers, and uppercase characters ... after you add 48 to it, of course =)*/
  110.                         ((x >= 0 && x <= 9) || (x >= 17 && x <= 42)) && isPrime(x)) {
  111.                         valid = true;
  112.                         math += x;
  113.                         nStr += (char)math;
  114.                         System.out.println(x);
  115.                         break;
  116.                     }
  117.                 }
  118.                 math = 48;
  119.             }
  120.         }
  121.         key_field.setText(nStr);
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement