001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of jWebTk.
006: *
007: * jWebTk is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * jWebTk is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with jWebTk; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jwebtk.licensing;
020:
021: //import jpersist.Database;
022: //import jpersist.DatabaseManager;
023: import java.io.BufferedReader;
024: import java.io.InputStreamReader;
025: import java.util.Calendar;
026: import java.util.GregorianCalendar;
027:
028: /**
029: * This class is used to generate permanent and time based license keys that
030: * can be used with LicenseCheck.
031: *
032: * <p>This is not a fool proof license, only a deterence.
033: */
034:
035: public final class LicenseGenerator {
036: /**
037: * Returns a randomly generated password suitable for generating license keys.
038: *
039: * @return a randomly generated password
040: */
041:
042: public final static String generatePassword() {
043: String password = "";
044:
045: for (int i = 0; i < 8; i++)
046: password += randomCharGenerator();
047:
048: return password;
049: }
050:
051: /**
052: * Generates a time based license key that will expire after a defined number of weeks.
053: *
054: * @param weeks number of weeks before license expires
055: * @param match1 First match string
056: * @param match2 Second match string
057: * @param password a suitable password (at least 8 characters) for encryption/decryption of the license key
058: *
059: * @return a license key
060: */
061:
062: public final static String generateTimeKey(int weeks,
063: String match1, String match2, String password)
064: throws LicenseException {
065: GregorianCalendar gc = new GregorianCalendar();
066:
067: gc.add(Calendar.WEEK_OF_YEAR, weeks);
068:
069: byte enc[] = LicenseCheck.encryptDecrypt(
070: new String(match1 + "|" + match2 + "|"
071: + gc.getTimeInMillis()).getBytes(), password,
072: true);
073:
074: return "T_" + byteToHex(enc).toUpperCase() + password;
075: }
076:
077: /**
078: * Generates a permanent license key that will never expire.
079: *
080: * @param match1 First match string
081: * @param match2 Second match string
082: * @param password a suitable password (at least 8 characters) for encryption/decryption of the license key
083: *
084: * @return a license key
085: */
086:
087: public final static String generatePermanentKey(String match1,
088: String match2, String password) throws LicenseException {
089: byte enc[] = LicenseCheck.encryptDecrypt(
090: (match1 + "|" + match2).getBytes(), password, true);
091:
092: return "P_" + byteToHex(enc).toUpperCase() + password;
093: }
094:
095: private final static String byteToHex(byte[] byteArray) {
096: StringBuffer hexStrBuf = new StringBuffer();
097:
098: for (int i = 0; i < byteArray.length; i++) {
099: int ival = Math.abs(new Integer(byteArray[i]).intValue());
100:
101: if (byteArray[i] < 0)
102: ival = ival ^ 0x80;
103:
104: String hex = Integer.toHexString(ival);
105:
106: while (hex.length() < 2)
107: hex = '0' + hex;
108:
109: if (byteArray[i] == -128)
110: hexStrBuf.append('-');
111:
112: hexStrBuf.append(hex);
113: }
114:
115: return hexStrBuf.toString();
116: }
117:
118: private final static char randomCharGenerator() {
119: int r;
120:
121: do {
122: r = (int) Math.round(Math.random() * 128);
123: } while (!(Character.isDigit((char) r) || Character
124: .isUpperCase((char) r)));
125:
126: return (char) r;
127: }
128:
129: public static void main(String[] args) {
130: //Database db = null;
131: BufferedReader b = new BufferedReader(new InputStreamReader(
132: System.in));
133: String r_key = null, match1 = null, match2 = null, company = null, project = null;
134: int num_weeks = 0;
135:
136: try {
137: //db = new DatabaseManager("db",1,"com.mysql.jdbc.Driver","jdbc:mysql://localhost/licensing","licensor","rosnecil").getDatabase();
138:
139: System.out.println("Enter Company: ");
140: company = b.readLine();
141:
142: System.out.println("Enter Project: ");
143: project = b.readLine();
144:
145: System.out.println("Enter Match1: ");
146: match1 = b.readLine();
147:
148: System.out.println("Enter Match2: ");
149: match2 = b.readLine();
150:
151: System.out.println("Permanent Key (y/n): ");
152:
153: if (b.readLine().toLowerCase().equals("n")) {
154: System.out.println("Enter Time (int): ");
155: num_weeks = new Integer(b.readLine()).intValue();
156:
157: System.out.println("Time Key = "
158: + (r_key = generateTimeKey(num_weeks, match1,
159: match2, generatePassword())) + "\n");
160: System.out.println("Timed License - "
161: + LicenseCheck.checkLicense(r_key) + "\n");
162: } else {
163: System.out.println("Permanent Key = "
164: + (r_key = generatePermanentKey(match1, match2,
165: generatePassword())) + "\n");
166: System.out.println("Permanent License - "
167: + LicenseCheck.checkLicense(r_key) + "\n");
168: }
169:
170: //db.preparedUpdate("insert into licenses (license,match1,match2,company,project) values(?,?,?,?,?)",
171: // new Object[] {r_key,match1,match2,company,project});
172: } catch (Exception e) {
173: e.printStackTrace();
174: }
175: /*
176: finally
177: {
178: if (db != null)
179: try
180: {
181: db.close();
182: }
183: catch (Exception e)
184: {
185: e.printStackTrace();
186: }
187: }
188: */
189: }
190: }
|