Source Code Cross Referenced for LicenseGenerator.java in  » Web-Framework » jWebApp » jwebtk » licensing » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » jWebApp » jwebtk.licensing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.