Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.*;
- import javax.swing.*;
- //Credits to Dr0ax@leetcoders.org, for coding the keygen example
- @SuppressWarnings("serial")
- public class Keygen extends JPanel implements ActionListener{
- JTextField key_field = new JTextField(19);
- JButton generate = new JButton("Generate");
- JButton chk = new JButton("Check key");
- Random r = new Random();
- public Keygen() {
- this.add(key_field);
- this.add(chk);
- this.add(generate);
- generate.addActionListener(this);
- chk.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String chkStr = key_field.getText();
- String[] parts = chkStr.split("-");
- boolean valid = false;
- //first check: if there are 4 parts total
- if(parts.length != 4)
- valid = false;
- else{
- //loop through the four parts of the string
- outloop: for (int i = 0; i < parts.length; i++)
- {
- //second check: break if the part size is not 4 characters total
- if(parts[i].length() != 4){
- valid = false;
- break;
- }
- else{
- //loop through each character of the string
- for(int x = 0; x < parts[i].length(); x++) {
- //get the integer value of the char
- int cv = (int)parts[i].charAt(x);
- //substract 48 from the number
- int rndVal = cv - 48;
- //check the number against the algorithm
- if (((rndVal >= 0 && rndVal <= 9) || (rndVal >= 17 && rndVal <= 42)) /*|| (rndVal >= 49 && rndVal <= 74)*/ && isPrime(rndVal)) {
- valid = true;
- }
- else{
- //if not valid, set the flag and exit the loop
- //you have to use a label (in my case "outloop") because you can't break a nested loop with just the break command
- valid = false;
- break outloop;
- }
- }
- }
- }
- }
- if(valid == true)
- key_field.setText("Valid Key!");
- else{key_field.setText("Not a valid key");}
- }
- });
- }
- public static void main(String[] args){
- JFrame f = new JFrame("Keygen Example");
- f.setLocation(100,100);
- f.setDefaultCloseOperation(3);
- f.setSize(250,100);
- f.add(new Keygen());
- f.setVisible(true);
- }
- boolean isPrime(int num){
- int i;
- boolean xD = false;
- for (i=2; i < num ;i++ ){
- int n = num%i;
- if (n==0){
- xD = false;
- break;
- }
- }
- if(i == num)
- xD = true;
- return xD;
- }
- @Override
- public void actionPerformed(ActionEvent arg0) {
- int x = 0;
- int math = 48;
- String nStr = "";
- boolean valid;
- for (int i = 0; i < 19; i++) {
- if(i == 4 || i == 9 || i ==14) {
- nStr += "-";
- }
- else{
- valid = false;
- while(!valid){
- x = r.nextInt(74) +1;
- 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 =)*/
- ((x >= 0 && x <= 9) || (x >= 17 && x <= 42)) && isPrime(x)) {
- valid = true;
- math += x;
- nStr += (char)math;
- System.out.println(x);
- break;
- }
- }
- math = 48;
- }
- }
- key_field.setText(nStr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement