1 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 ; 37 import java.io.Serializable ; 38 39 import java.security.Key ; 40 import java.security.PrivateKey ; 41 import java.security.Provider ; 42 import java.security.PublicKey ; 43 import java.security.SecureRandom ; 44 import java.security.Security ; 45 46 import java.util.Enumeration ; 47 48 import javax.crypto.Cipher; 49 import javax.crypto.SealedObject; 50 51 52 public class EncryptionEngine implements Serializable { 53 private SecureRandom 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 eng = new RSAEngine(); 63 symmetricCipher = Cipher.getInstance("RIJNDAEL/ECB/WithCTS", "BC"); 64 asymmetricCipher = Cipher.getInstance("RSA", "BC"); 65 } catch (Exception e) { 66 System.out.println("Exception in cipher creation : " + e); 67 e.printStackTrace(); 68 } 69 } 70 71 public Object encrypt(Serializable object, Key sessionKey) { 72 try { 73 symmetricCipher.init(Cipher.ENCRYPT_MODE, sessionKey, rand); 74 75 return new SealedObject(object, symmetricCipher); 76 } catch (Exception 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 [] p = Security.getProviders(); 87 88 for (int i = 0; i < p.length; i++) { 89 System.out.println(p[i]); 90 91 for (Enumeration e = p[i].keys(); e.hasMoreElements();) { 92 System.out.println("\t" + e.nextElement()); 93 } 94 } 95 } catch (Exception e) { 96 e.printStackTrace(); 97 } 98 } 99 100 public Object decrypt(Object object, Key sessionKey) { 101 try { 102 symmetricCipher.init(Cipher.DECRYPT_MODE, sessionKey, rand); 103 104 return ((SealedObject) object).getObject(symmetricCipher); 105 } catch (Exception e) { 106 System.out.println("Exception in decryption :" + e); 107 e.printStackTrace(); 108 } 109 110 return null; 111 } 112 113 public Object asymmetric_encrypt(Serializable object, PublicKey key) { 114 try { 115 return new SealedObject(object, asymmetricCipher); 117 } catch (Exception e) { 118 System.out.println("Exception in encryption :" + e); 119 e.printStackTrace(); 120 } 121 122 return null; 123 } 124 125 public Object asymmetric_decrypt(Object object, PrivateKey key) { 126 try { 127 asymmetricCipher.init(Cipher.DECRYPT_MODE, key, rand); 128 129 return ((SealedObject) object).getObject(asymmetricCipher); 130 } catch (Exception e) { 131 System.out.println("Exception in decryption :" + e); 132 e.printStackTrace(); 133 } 134 135 return null; 136 } 137 138 private void writeObject(java.io.ObjectOutputStream out) 140 throws IOException { 141 out.defaultWriteObject(); 142 } 143 144 private void readObject(java.io.ObjectInputStream in) 145 throws IOException , ClassNotFoundException { 146 in.defaultReadObject(); 147 148 listProvider(); 153 154 try { 155 symmetricCipher = Cipher.getInstance("RIJNDAEL/ECB/WithCTS", "BC"); 156 asymmetricCipher = Cipher.getInstance("RSA", "BC"); 157 } catch (Exception e) { 158 System.out.println("Exception in cipher creation : " + e); 159 e.printStackTrace(); 160 } 161 } 162 } 163 | Popular Tags |