Source code: com/sourcetap/license/KeyGenerator.java
1 /*
2 * $Id$
3 *
4 * Copyright (c) 2003 SourceTap - www.sourcetap.com
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 package com.sourcetap.license;
25
26 import java.security.*;
27
28 /**
29 * Generate Public/Private Key pairs and read/write them to files
30 *
31 * @author Steve Fowler
32 * @version $Revision$
33 */
34 public class KeyGenerator
35 {
36 private PublicKey publicKey;
37 private PrivateKey privateKey;
38 EncryptionUtil keygen;
39
40 public KeyGenerator()
41 {
42 keygen = new EncryptionUtil();
43 }
44
45 /**
46 * Create a public/private key pair and save the keys to the specified files
47 *
48 * @param publicURI file name to store public key in
49 * @param privateURI file name to store private key in
50 * @throws LicenseException
51 */
52 public void createKeys(String publicURI, String privateURI)
53 throws LicenseException
54 {
55 try
56 {
57 keygen.generateKeys( publicURI, privateURI );
58 publicKey = keygen.getPublic();
59 privateKey = keygen.getPrivate();
60 }
61 catch (Exception e)
62 {
63 throw new LicenseException ( e.getMessage() );
64 }
65 }
66
67 /**
68 * Read public/private keys from specified files
69 *
70 * @param publicURI name of file containing the public key
71 * @param privateURI name of file containing the private key
72 * @throws LicenseException
73 */
74 public void readKeys( String publicURI, String privateURI ) throws LicenseException
75 {
76 try
77 {
78 keygen.readKeys( publicURI, privateURI );
79 publicKey = keygen.getPublic();
80 privateKey = keygen.getPrivate();
81 }
82 catch (Exception e)
83 {
84 throw new LicenseException("Unable to read Key Files: " + e.getMessage());
85 }
86 }
87
88 /**
89 * read a public key from a file
90 *
91 * @param URI name of file containing the key
92 * @throws LicenseException
93 */
94 public void readPublicKey( String URI ) throws LicenseException
95 {
96 try
97 {
98 publicKey = keygen.readPublicKey( URI );
99 }
100 catch (Exception e)
101 {
102 throw new LicenseException("Unable to read Key File:" + e.getMessage());
103 }
104 }
105
106 /**
107 * read a private key from a file
108 *
109 * @param URI name of file containing the key
110 * @throws LicenseException
111 */
112 public void readPrivateKey( String URI ) throws LicenseException
113 {
114 try
115 {
116 privateKey = keygen.readPrivateKey( URI );
117 }
118 catch (Exception e)
119 {
120 throw new LicenseException("Unable to read Key File:" + e.getMessage());
121 }
122 }
123
124 public PublicKey getPublic()
125 {
126 return publicKey;
127 }
128
129 public PrivateKey getPrivate()
130 {
131 return privateKey;
132 }
133
134 public String getPublicString()
135 {
136 return publicKey.toString();
137 }
138 public String getPrivateString()
139 {
140 return privateKey.toString();
141 }
142
143 public static void main(String args[])
144 {
145 try {
146 if ( args.length < 3 )
147 {
148 System.out.println("usage: KeyGenerator publicFileName privateFileName");
149 return;
150 }
151 KeyGenerator keygen = new KeyGenerator();
152 keygen.createKeys( args[1], args[2] );
153 }
154 catch ( Exception e)
155 {
156 System.out.println("Error:" + e.getMessage());
157 e.printStackTrace();
158 }
159 }
160 }