KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > codec > CodecSecurity


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.util.codec;
37
38 import java.text.*;
39 import java.security.*;
40 import javax.crypto.*;
41 import javax.crypto.spec.*;
42
43 import com.micronova.util.*;
44
45 /** Security-related codecs */
46
47 public class CodecSecurity extends Codec
48 {
49     /** create message digest as binary string */
50
51     public static final Object JavaDoc digest(Object JavaDoc object, Object JavaDoc algorithm) throws Exception JavaDoc
52     {
53         if (object != null)
54         {
55             MessageDigest messageDigest = MessageDigest.getInstance(algorithm.toString());
56
57             byte[] input;
58
59             if (object instanceof byte[])
60             {
61                 input = (byte[])object;
62             }
63             else
64             {
65                 input = StringUtil.fromBinaryString(object.toString());
66             }
67
68             return StringUtil.toBinaryString(messageDigest.digest(input));
69         }
70
71         return object;
72     }
73
74     /** create and return a secure random of given size */
75
76     public static final Object JavaDoc secureRandom(Object JavaDoc object, Object JavaDoc algorithm) throws Exception JavaDoc
77     {
78         if (object != null)
79         {
80             SecureRandom secureRandom = SecureRandom.getInstance(algorithm.toString());
81             String JavaDoc objectString = object.toString();
82
83             int size = 0;
84
85             try
86             {
87                 size = Integer.parseInt(objectString);
88             }
89             catch (Exception JavaDoc e)
90             {
91             }
92             
93             if (size <= 0)
94             {
95                 size = StringUtil.fromBinaryString(objectString).length;
96             }
97
98             byte[] b = new byte[size];
99
100             secureRandom.nextBytes(b);
101
102             object = StringUtil.toBinaryString(b);
103         }
104
105         return object;
106     }
107
108     /** generate a secret key as binary string for given algorithm */
109
110     public static Object JavaDoc generateSecretKey(Object JavaDoc object) throws Exception JavaDoc
111     {
112         if (object != null)
113         {
114             String JavaDoc algorithm = object.toString();
115
116             KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
117
118             SecretKey secretKey = keyGenerator.generateKey();
119
120             object = StringUtil.toBinaryString(secretKey.getEncoded());
121         }
122
123         return object;
124     }
125
126     private static Object JavaDoc crypt(Object JavaDoc object, Object JavaDoc keySpec, Object JavaDoc algorithmSpec, Object JavaDoc ivSpec, int mode) throws Exception JavaDoc
127     {
128         if (object != null)
129         {
130             String JavaDoc algorithm = algorithmSpec.toString();
131
132             byte[] source = StringUtil.fromBinaryString(object.toString());
133
134             Cipher cipher = Cipher.getInstance(algorithm);
135
136             byte[] keyBytes = StringUtil.fromBinaryString(keySpec.toString());
137
138             SecretKey secretKey = new SecretKeySpec(keyBytes, algorithm.replaceFirst("/.*$", ""));
139
140             if (ivSpec != null)
141             {
142                 cipher.init(mode, secretKey, new IvParameterSpec(ivSpec.toString().getBytes("iso-8859-1")));
143             }
144             else
145             {
146                 cipher.init(mode, secretKey);
147             }
148
149             object = StringUtil.toBinaryString(cipher.doFinal(source));
150         }
151
152         return object;
153     }
154     
155     public static Object JavaDoc encrypt(Object JavaDoc object, Object JavaDoc keySpec, Object JavaDoc algorithmSpec, Object JavaDoc ivSpec) throws Exception JavaDoc
156     {
157         return crypt(object, keySpec, algorithmSpec, ivSpec, Cipher.ENCRYPT_MODE);
158     }
159
160     public static Object JavaDoc encrypt(Object JavaDoc object, Object JavaDoc keySpec, Object JavaDoc algorithmSpec) throws Exception JavaDoc
161     {
162         return encrypt(object, keySpec, algorithmSpec, null);
163     }
164
165     public static Object JavaDoc decrypt(Object JavaDoc object, Object JavaDoc keySpec, Object JavaDoc algorithmSpec, Object JavaDoc ivSpec) throws Exception JavaDoc
166     {
167         return crypt(object, keySpec, algorithmSpec, ivSpec, Cipher.DECRYPT_MODE);
168     }
169
170     public static Object JavaDoc decrypt(Object JavaDoc object, Object JavaDoc keySpec, Object JavaDoc algorithmSpec) throws Exception JavaDoc
171     {
172         return decrypt(object, keySpec, algorithmSpec, null);
173     }
174 }
175
176
Popular Tags