Source code: com/sourcetap/license/EncryptionUtil.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.io.*;
27 import java.security.*;
28 import java.security.spec.*;
29
30 /**
31 * Provides utility functions to handle public/private key encryption.
32 *
33 * @author Steve Fowler
34 * @version $Revision$
35 */
36 public class EncryptionUtil
37 {
38 private PublicKey publicKey = null;
39 private PrivateKey privateKey = null;
40
41 public EncryptionUtil()
42 {
43 }
44
45 /**
46 * Generate a public/private key pair
47 */
48 public void generateKeys()
49 throws IOException, NoSuchAlgorithmException, NoSuchProviderException
50 {
51 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
52 keyGen.initialize(1024, new SecureRandom());
53 KeyPair pair = keyGen.generateKeyPair();
54 this.privateKey = pair.getPrivate();
55 this.publicKey = pair.getPublic();
56 }
57
58 /**
59 * Generate a public/private key pair, and write the keys to the specified files
60 * @param publicURI name of file to store the public key in
61 * @param privateURI name of file to store the private key in
62 */
63 public void generateKeys( String publicURI, String privateURI )
64 throws IOException, NoSuchAlgorithmException, NoSuchProviderException
65 {
66 generateKeys();
67 writeKeys(publicURI, privateURI);
68 }
69
70 public PublicKey getPublic()
71 {
72 return publicKey;
73 }
74
75 public PrivateKey getPrivate()
76 {
77 return privateKey;
78 }
79
80 public void writeKeys(String publicURI, String privateURI) throws IOException, FileNotFoundException
81 {
82 writePublicKey(publicURI);
83 writePrivateKey(privateURI);
84 }
85
86 public void writePublicKey(String URI) throws IOException, FileNotFoundException
87 {
88 byte[] enckey = publicKey.getEncoded();
89 FileOutputStream keyfos = new FileOutputStream(URI);
90 keyfos.write(enckey);
91 keyfos.close();
92 }
93
94 public void writePrivateKey(String URI) throws IOException, FileNotFoundException
95 {
96 byte[] enckey = privateKey.getEncoded();
97 FileOutputStream keyfos = new FileOutputStream(URI);
98 keyfos.write(enckey);
99 keyfos.close();
100 }
101
102 /**
103 * read public/private keys from specified files
104 * @param publicURI name of public key file
105 * @param privateURI name of private key file
106 */
107 public void readKeys(String publicURI, String privateURI)
108 throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException
109 {
110 readPublicKey(publicURI);
111 readPrivateKey(privateURI);
112 }
113
114 /**
115 * read public key from specified file
116 * @param publicURI name of public key file
117 * @return PublicKey public key
118 */
119 public PublicKey readPublicKey(String URI)
120 throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException
121 {
122 FileInputStream keyfis = new FileInputStream(URI);
123 byte[] encKey = new byte[keyfis.available()];
124 keyfis.read(encKey);
125 keyfis.close();
126 X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
127 KeyFactory keyFactory = KeyFactory.getInstance("DSA");
128 publicKey = keyFactory.generatePublic(pubKeySpec);
129 return publicKey;
130 }
131
132 /**
133 * read private key from specified file
134 * @param privateURI name of private key file
135 * @return PrivateKey private key
136 */
137 public PrivateKey readPrivateKey(String URI)
138 throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException
139 {
140 FileInputStream keyfis = new FileInputStream(URI);
141 byte[] encKey = new byte[keyfis.available()];
142 keyfis.read(encKey);
143 keyfis.close();
144 PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(encKey);
145 KeyFactory keyFactory = KeyFactory.getInstance("DSA");
146 privateKey = keyFactory.generatePrivate(privKeySpec);
147 return privateKey;
148 }
149
150
151 /**
152 * sign a message using the private key
153 * @param message the message to be signed
154 * @return String the signed message encoded in Base64
155 */
156 public String sign(String message)
157 throws IOException, NoSuchAlgorithmException, NoSuchProviderException,
158 InvalidKeySpecException, InvalidKeyException, SignatureException
159 {
160 return sign(message, privateKey);
161 }
162
163 /**
164 * sign a message using the private key
165 * @param message the message to be signed
166 * @param message the name of the file containing the private key
167 * @return String the signed message encoded in Base64
168 */
169 public String sign(String message, String privateKeyURI)
170 throws IOException, NoSuchAlgorithmException, NoSuchProviderException,
171 InvalidKeySpecException, InvalidKeyException, SignatureException, IOException
172 {
173 PrivateKey pk = readPrivateKey(privateKeyURI);
174 return sign(message, pk);
175 }
176
177 /**
178 * sign a message using the private key
179 * @param message the message to be signed
180 * @param message the private key
181 * @return String the signed message encoded in Base64
182 */
183 public String sign(String message, PrivateKey privateKey)
184 throws IOException, NoSuchAlgorithmException, NoSuchProviderException,
185 InvalidKeySpecException, InvalidKeyException, SignatureException
186 {
187 Signature dsa = Signature.getInstance("SHA/DSA");
188 dsa.initSign(privateKey);
189 dsa.update(message.getBytes());
190 byte m1[] = dsa.sign();
191
192 String signature = new String(Base64Coder.encode(m1));
193
194 return signature;
195 }
196
197 /**
198 * verify that the message was signed by the private key by using the public key
199 * @param message the message to be verified
200 * @param signature the signature generated by the private key and encoded in Base64
201 * @param publicKeyURI the name of the file containing the public key
202 * @return boolean true if the message was signed by the private key
203 */
204 public boolean verify(String message, String signature, String publicKeyURI)
205 throws IOException, NoSuchAlgorithmException, NoSuchProviderException,
206 InvalidKeySpecException, InvalidKeyException, SignatureException
207 {
208
209 PublicKey pk = readPublicKey(publicKeyURI);
210 return verify(message, signature, pk);
211 }
212
213 /**
214 * verify that the message was signed by the private key by using the public key
215 * @param message the message to be verified
216 * @param signature the signature generated by the private key and encoded in Base64
217 * @return boolean true if the message was signed by the private key
218 */
219 public boolean verify(String message, String signature)
220 throws IOException, NoSuchAlgorithmException, NoSuchProviderException,
221 InvalidKeySpecException, InvalidKeyException, SignatureException
222 {
223 if ( publicKey == null )
224 throw new InvalidKeyException("Public Key not provided.");
225 return verify( message, signature, publicKey);
226 }
227
228 /**
229 * verify that the message was signed by the private key by using the public key
230 * @param message the message to be verified
231 * @param signature the signature generated by the private key and encoded in Base64
232 * @param publicKey the public key
233 * @return boolean true if the message was signed by the private key
234 */
235 public boolean verify(String message, String signature, PublicKey publicKey)
236 throws IOException, NoSuchAlgorithmException, NoSuchProviderException,
237 InvalidKeySpecException, InvalidKeyException, SignatureException
238 {
239 Signature dsa = Signature.getInstance("SHA/DSA");
240 dsa.initVerify(publicKey);
241 dsa.update(message.getBytes());
242
243 byte sigDec[] = Base64Coder.decode(signature.toCharArray());
244 return dsa.verify(sigDec);
245 }
246
247 }