org/objectweb/proactive/ext/security/crypto/EncryptionEngine.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.ext.security.crypto;
00032 
00033 import java.io.IOException;
00034 import java.io.Serializable;
00035 import java.security.Key;
00036 import java.security.PrivateKey;
00037 import java.security.Provider;
00038 import java.security.PublicKey;
00039 import java.security.SecureRandom;
00040 import java.security.Security;
00041 import java.util.Enumeration;
00042 
00043 import javax.crypto.Cipher;
00044 import javax.crypto.SealedObject;
00045 
00046 import org.bouncycastle.crypto.AsymmetricBlockCipher;
00047 import org.bouncycastle.crypto.engines.RSAEngine;
00048 
00049 
00050 public class EncryptionEngine implements Serializable {
00051     private SecureRandom rand = new FixedSecureRandom();
00052     private transient Cipher symmetricCipher;
00053     private transient Cipher asymmetricCipher;
00054     private transient AsymmetricBlockCipher eng;
00055 
00056     public EncryptionEngine() {
00057         try {
00058             //         symmetricCipher = Cipher.getInstance("Rijndael/ECB/WithCTS");
00059             //                                                                   "RSA"
00060             eng = new RSAEngine();
00061             symmetricCipher = Cipher.getInstance("RIJNDAEL/ECB/WithCTS", "BC");
00062             asymmetricCipher = Cipher.getInstance("RSA", "BC");
00063         } catch (Exception e) {
00064             System.out.println("Exception in cipher creation : " + e);
00065             e.printStackTrace();
00066         }
00067     }
00068 
00069     public Object encrypt(Serializable object, Key sessionKey) {
00070         try {
00071             symmetricCipher.init(Cipher.ENCRYPT_MODE, sessionKey, rand);
00072 
00073             return new SealedObject(object, symmetricCipher);
00074         } catch (Exception e) {
00075             System.out.println("Exception in encryption :" + e);
00076             e.printStackTrace();
00077         }
00078 
00079         return null;
00080     }
00081 
00082     private void listProvider() {
00083         try {
00084             Provider[] p = Security.getProviders();
00085 
00086             for (int i = 0; i < p.length; i++) {
00087                 System.out.println(p[i]);
00088 
00089                 for (Enumeration e = p[i].keys(); e.hasMoreElements();) {
00090                     System.out.println("\t" + e.nextElement());
00091                 }
00092             }
00093         } catch (Exception e) {
00094             e.printStackTrace();
00095         }
00096     }
00097 
00098     public Object decrypt(Object object, Key sessionKey) {
00099         try {
00100             symmetricCipher.init(Cipher.DECRYPT_MODE, sessionKey, rand);
00101 
00102             return ((SealedObject) object).getObject(symmetricCipher);
00103         } catch (Exception e) {
00104             System.out.println("Exception in decryption :" + e);
00105             e.printStackTrace();
00106         }
00107 
00108         return null;
00109     }
00110 
00111     public Object asymmetric_encrypt(Serializable object, PublicKey key) {
00112         try {
00113             //         asymmetricCipher.init(Cipher.ENCRYPT_MODE, key, rand);
00114             return new SealedObject(object, asymmetricCipher);
00115         } catch (Exception e) {
00116             System.out.println("Exception in encryption :" + e);
00117             e.printStackTrace();
00118         }
00119 
00120         return null;
00121     }
00122 
00123     public Object asymmetric_decrypt(Object object, PrivateKey key) {
00124         try {
00125             asymmetricCipher.init(Cipher.DECRYPT_MODE, key, rand);
00126 
00127             return ((SealedObject) object).getObject(asymmetricCipher);
00128         } catch (Exception e) {
00129             System.out.println("Exception in decryption :" + e);
00130             e.printStackTrace();
00131         }
00132 
00133         return null;
00134     }
00135 
00136     // implements Serializable
00137     private void writeObject(java.io.ObjectOutputStream out)
00138         throws IOException {
00139         out.defaultWriteObject();
00140     }
00141 
00142     private void readObject(java.io.ObjectInputStream in)
00143         throws IOException, ClassNotFoundException {
00144         in.defaultReadObject();
00145 
00146         // Add bouncycastle security provider
00147         //   Provider myProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
00148         // Security.addProvider(myProvider);
00149         //    randomLongGenerator = new RandomLongGenerator();
00150         listProvider();
00151 
00152         try {
00153             symmetricCipher = Cipher.getInstance("RIJNDAEL/ECB/WithCTS", "BC");
00154             asymmetricCipher = Cipher.getInstance("RSA", "BC");
00155         } catch (Exception e) {
00156             System.out.println("Exception in cipher creation : " + e);
00157             e.printStackTrace();
00158         }
00159     }
00160 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1