EDIT: Mods, sorry for posting in the wrong section, my intention was to post in the Android section, but it seems that I have mistakenly clicked the wrong sub-section... Please move the thread where it is appropriate
I wondered a long time how the hell do keygens work, but now it's all clear.
The source I am going to post in this thread is just a modified version of Dr0ax's (@LeetCoders), with a little addition.
There is 1 more function, which enables the user to check if the key he enters, is valid and generated by the algorithm in the keygen.
This is just a basic example, with a weak algorithm, but can be improved much much more, and easily translated to C# (for the .NET lovers)
The code is commented a lot, so nobody should have trouble understanding the commands.
Anyways, here is a pastebin link http://pastebin.com/ZiYH83rk
And attached to the board... All comments are welcomed =)
Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; import javax.swing.*; //Credits to Dr0ax@leetcoders.org, for coding the original 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); } }
// Thread Moved but comments are disabled in the right section ~ Mind


3Likes
LinkBack URL
About LinkBacks


