00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00059
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
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
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
00147
00148
00149
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 }