1 10 11 package org.mule.impl.security; 12 13 import java.security.GeneralSecurityException ; 14 import java.security.spec.AlgorithmParameterSpec ; 15 import java.security.spec.KeySpec ; 16 17 import javax.crypto.Cipher; 18 import javax.crypto.SecretKey; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.mule.config.i18n.Message; 23 import org.mule.config.i18n.Messages; 24 import org.mule.umo.UMOEncryptionStrategy; 25 import org.mule.umo.lifecycle.InitialisationException; 26 import org.mule.umo.security.CryptoFailureException; 27 import org.mule.util.Base64; 28 29 33 public abstract class AbstractJCEEncryptionStrategy implements UMOEncryptionStrategy 34 { 35 38 protected transient Log logger = LogFactory.getLog(getClass()); 39 40 protected KeySpec keySpec; 41 protected SecretKey secretKey; 42 protected Cipher encryptCipher; 43 protected Cipher decryptCipher; 44 45 protected String algorithm = null; 46 47 protected boolean base64Encoding = true; 48 49 public void initialise() throws InitialisationException 50 { 51 if (algorithm == null) 52 { 53 throw new InitialisationException(new Message(Messages.X_IS_NULL, "Algorithm"), this); 54 } 55 else 56 { 57 logger.debug("Using encryption algorithm: " + algorithm); 58 } 59 60 keySpec = createKeySpec(); 61 62 try 63 { 64 secretKey = getSecretKey(); 65 encryptCipher = Cipher.getInstance(getAlgorithm()); 67 decryptCipher = Cipher.getInstance(getAlgorithm()); 68 69 AlgorithmParameterSpec paramSpec = createAlgorithmParameterSpec(); 70 if (paramSpec != null) 71 { 72 encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); 73 decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); 74 } 75 else 76 { 77 encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey); 78 decryptCipher.init(Cipher.DECRYPT_MODE, secretKey); 79 } 80 81 } 82 catch (Exception e) 83 { 84 throw new InitialisationException(new Message(Messages.FAILED_TO_CREATE_X, "encryption ciphers"), 85 e, this); 86 } 87 } 88 89 protected abstract SecretKey getSecretKey() throws GeneralSecurityException ; 90 91 public byte[] encrypt(byte[] data, Object info) throws CryptoFailureException 92 { 93 try 94 { 95 byte[] buf = encryptCipher.doFinal(data); 96 if (base64Encoding) 97 { 98 return Base64.encodeBytes(buf).getBytes(); 99 } 100 else 101 { 102 return buf; 103 } 104 } 105 catch (Exception e) 106 { 107 throw new CryptoFailureException(this, e); 108 } 109 } 110 111 public byte[] decrypt(byte[] data, Object info) throws CryptoFailureException 112 { 113 try 114 { 115 byte[] dec = data; 116 if (base64Encoding) 117 { 118 dec = Base64.decode(new String (data)); 119 } 120 return decryptCipher.doFinal(dec); 121 } 122 catch (Exception e) 123 { 124 throw new CryptoFailureException(this, e); 125 } 126 } 127 128 public String getAlgorithm() 129 { 130 return algorithm; 131 } 132 133 public void setAlgorithm(String algorithm) 134 { 135 this.algorithm = algorithm; 136 } 137 138 public String toString() 139 { 140 StringBuffer buf = new StringBuffer (); 141 buf.append("Algorithm=").append(algorithm); 142 return buf.toString(); 143 } 144 145 public boolean isBase64Encoding() 146 { 147 return base64Encoding; 148 } 149 150 public void setBase64Encoding(boolean base64Encoding) 151 { 152 this.base64Encoding = base64Encoding; 153 } 154 155 protected abstract KeySpec createKeySpec(); 156 157 protected abstract AlgorithmParameterSpec createAlgorithmParameterSpec(); 158 159 } 160 | Popular Tags |