KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > security > crypto > EncryptionEngine


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ext.security.crypto;
32
33 import org.bouncycastle.crypto.AsymmetricBlockCipher;
34 import org.bouncycastle.crypto.engines.RSAEngine;
35
36 import java.io.IOException JavaDoc;
37 import java.io.Serializable JavaDoc;
38
39 import java.security.Key JavaDoc;
40 import java.security.PrivateKey JavaDoc;
41 import java.security.Provider JavaDoc;
42 import java.security.PublicKey JavaDoc;
43 import java.security.SecureRandom JavaDoc;
44 import java.security.Security JavaDoc;
45
46 import java.util.Enumeration JavaDoc;
47
48 import javax.crypto.Cipher;
49 import javax.crypto.SealedObject;
50
51
52 public class EncryptionEngine implements Serializable JavaDoc {
53     private SecureRandom JavaDoc rand = new FixedSecureRandom();
54     private transient Cipher symmetricCipher;
55     private transient Cipher asymmetricCipher;
56     private transient AsymmetricBlockCipher eng;
57
58     public EncryptionEngine() {
59         try {
60             // symmetricCipher = Cipher.getInstance("Rijndael/ECB/WithCTS");
61
// "RSA"
62
eng = new RSAEngine();
63             symmetricCipher = Cipher.getInstance("RIJNDAEL/ECB/WithCTS", "BC");
64             asymmetricCipher = Cipher.getInstance("RSA", "BC");
65         } catch (Exception JavaDoc e) {
66             System.out.println("Exception in cipher creation : " + e);
67             e.printStackTrace();
68         }
69     }
70
71     public Object JavaDoc encrypt(Serializable JavaDoc object, Key JavaDoc sessionKey) {
72         try {
73             symmetricCipher.init(Cipher.ENCRYPT_MODE, sessionKey, rand);
74
75             return new SealedObject(object, symmetricCipher);
76         } catch (Exception JavaDoc e) {
77             System.out.println("Exception in encryption :" + e);
78             e.printStackTrace();
79         }
80
81         return null;
82     }
83
84     private void listProvider() {
85         try {
86             Provider JavaDoc[] p = Security.getProviders();
87
88             for (int i = 0; i < p.length; i++) {
89                 System.out.println(p[i]);
90
91                 for (Enumeration JavaDoc e = p[i].keys(); e.hasMoreElements();) {
92                     System.out.println("\t" + e.nextElement());
93                 }
94             }
95         } catch (Exception JavaDoc e) {
96             e.printStackTrace();
97         }
98     }
99
100     public Object JavaDoc decrypt(Object JavaDoc object, Key JavaDoc sessionKey) {
101         try {
102             symmetricCipher.init(Cipher.DECRYPT_MODE, sessionKey, rand);
103
104             return ((SealedObject) object).getObject(symmetricCipher);
105         } catch (Exception JavaDoc e) {
106             System.out.println("Exception in decryption :" + e);
107             e.printStackTrace();
108         }
109
110         return null;
111     }
112
113     public Object JavaDoc asymmetric_encrypt(Serializable JavaDoc object, PublicKey JavaDoc key) {
114         try {
115             // asymmetricCipher.init(Cipher.ENCRYPT_MODE, key, rand);
116
return new SealedObject(object, asymmetricCipher);
117         } catch (Exception JavaDoc e) {
118             System.out.println("Exception in encryption :" + e);
119             e.printStackTrace();
120         }
121
122         return null;
123     }
124
125     public Object JavaDoc asymmetric_decrypt(Object JavaDoc object, PrivateKey JavaDoc key) {
126         try {
127             asymmetricCipher.init(Cipher.DECRYPT_MODE, key, rand);
128
129             return ((SealedObject) object).getObject(asymmetricCipher);
130         } catch (Exception JavaDoc e) {
131             System.out.println("Exception in decryption :" + e);
132             e.printStackTrace();
133         }
134
135         return null;
136     }
137
138     // implements Serializable
139
private void writeObject(java.io.ObjectOutputStream JavaDoc out)
140         throws IOException JavaDoc {
141         out.defaultWriteObject();
142     }
143
144     private void readObject(java.io.ObjectInputStream JavaDoc in)
145         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
146         in.defaultReadObject();
147
148         // Add bouncycastle security provider
149
// Provider myProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
150
// Security.addProvider(myProvider);
151
// randomLongGenerator = new RandomLongGenerator();
152
listProvider();
153
154         try {
155             symmetricCipher = Cipher.getInstance("RIJNDAEL/ECB/WithCTS", "BC");
156             asymmetricCipher = Cipher.getInstance("RSA", "BC");
157         } catch (Exception JavaDoc e) {
158             System.out.println("Exception in cipher creation : " + e);
159             e.printStackTrace();
160         }
161     }
162 }
163
Popular Tags