1. To visit protected / private sections of the forum you must be connected with your user account. If you are not yet a member of our forum, you can create it now for free!.

User Tag List

Thread: [JAVA]Keygen example

Results 1 to 1 of 1

  1. #1
    T!T@N's Avatar
    Member
    Join Date Jul 2009
    Location Beyond (∞) Infinity
    Posts 91
    Like (Stats)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    0 Post(s)

    [JAVA]Keygen example

    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
    Last edited by MindfreaK; 26-01-2012 at 16:34.
    --If you are a good hacker - everyone knows who you are, if you are a gr8 hacker - nobody knows who you are-- piderman:
    http://img4.imageshack.us/img4/2082/8025.png

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 27-08-2011, 01:05
  2. Windows XP Professional SP3 Keygen
    By NaiXION in forum VB Snippets
    Replies: 3
    Last Post: 01-08-2011, 10:46
  3. Devil Childs keygen port to java
    By metric in forum General Programming Help
    Replies: 7
    Last Post: 04-06-2010, 02:16
  4. keygen example
    By cracksman in forum Delphi Help
    Replies: 6
    Last Post: 01-06-2009, 12:28
  5. Keygen For Delphi 7
    By Snowboy in forum Delphi Help
    Replies: 4
    Last Post: 27-12-2008, 14:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts