Jump to content
Encod.es Forum
Sign in to follow this  
Yuuki-chan

[Java] Custom keygen / serial generator

Recommended Posts

import java.util.Random;

public class Test1
{
	static int kl = 1337;

	public static void main(String[] args)
	{
		if (args.length == 1)
		{
			int sum = 0;
			for (int i = 0; i < args[0].length(); i++)
				sum += (int) args[0].charAt(i);

			if (sum == kl)
				System.out.println("Correct key!");
			else
				System.out.println("Wrong key!");

			System.exit(0);
		}
		else if (args.length == 2)
		{
			if (args[0].equalsIgnoreCase("gen"))
				genKey(Integer.parseInt(args[1]));
		}
		else
			System.exit(0);
	}

	private static void genKey(int keys)
	{
		int i = 0;
		String a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
		String k = "";

		while (true)
		{
			if (i < keys)
			{
				k += a.charAt(new Random().nextInt(a.length()));
				int s = chkKey(k);

				if (s > kl)
					k = "";
				else if (s == kl)
				{
					System.out.println("Valid key: " + k);
					i++;
				}
			}
			else
				break;
		}
	}

	private static int chkKey(String k)
	{
		int char_sum = 0;

		for (char c : k.toCharArray())
			char_sum += (int) c;

		return char_sum;
	}
}

 

Run with:

java Test1 IjDGNZPlaXAnHvq

Returns:

Correct key!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×