Coverage Report - org.mule.security.AbstractJCEEncryptionStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractJCEEncryptionStrategy
67%
28/42
62%
5/8
2.273
 
 1  
 /*
 2  
  * $Id: AbstractJCEEncryptionStrategy.java 11517 2008-03-31 21:34:19Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.security;
 12  
 
 13  
 import org.mule.api.lifecycle.InitialisationException;
 14  
 import org.mule.api.security.CryptoFailureException;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 import org.mule.util.Base64;
 17  
 
 18  
 import java.security.GeneralSecurityException;
 19  
 import java.security.spec.AlgorithmParameterSpec;
 20  
 import java.security.spec.KeySpec;
 21  
 
 22  
 import javax.crypto.Cipher;
 23  
 import javax.crypto.SecretKey;
 24  
 
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 
 28  
 /**
 29  
  * A JCE based encryption strategy. It also provides base64 encoding of
 30  
  * encrypted/decrypted data by setting the base64encoding attribute.
 31  
  */
 32  18
 public abstract class AbstractJCEEncryptionStrategy extends AbstractNamedEncryptionStrategy
 33  
 {
 34  
     /**
 35  
      * logger used by this class
 36  
      */
 37  18
     protected transient Log logger = LogFactory.getLog(getClass());
 38  
 
 39  
     protected KeySpec keySpec;
 40  
     protected SecretKey secretKey;
 41  
     protected Cipher encryptCipher;
 42  
     protected Cipher decryptCipher;
 43  
 
 44  18
     protected String algorithm = null;
 45  
 
 46  18
     protected boolean base64Encoding = true;
 47  
 
 48  
     public void initialise() throws InitialisationException
 49  
     {
 50  18
         if (algorithm == null)
 51  
         {
 52  0
             throw new InitialisationException(CoreMessages.objectIsNull("Algorithm"), this);
 53  
         }
 54  
         else
 55  
         {
 56  18
             logger.debug("Using encryption algorithm: " + algorithm);
 57  
         }
 58  
 
 59  18
         keySpec = createKeySpec();
 60  
 
 61  
         try
 62  
         {
 63  18
             secretKey = getSecretKey();
 64  
             // Create Ciphers
 65  18
             encryptCipher = Cipher.getInstance(getAlgorithm());
 66  18
             decryptCipher = Cipher.getInstance(getAlgorithm());
 67  
 
 68  18
             AlgorithmParameterSpec paramSpec = createAlgorithmParameterSpec();
 69  18
             if (paramSpec != null)
 70  
             {
 71  12
                 encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
 72  12
                 decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
 73  
             }
 74  
             else
 75  
             {
 76  6
                 encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
 77  6
                 decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
 78  
             }
 79  
 
 80  
         }
 81  0
         catch (Exception e)
 82  
         {
 83  0
             throw new InitialisationException(CoreMessages.failedToCreate("encryption ciphers"),
 84  
                 e, this);
 85  18
         }
 86  18
     }
 87  
 
 88  
     protected abstract SecretKey getSecretKey() throws GeneralSecurityException;
 89  
 
 90  
     public byte[] encrypt(byte[] data, Object info) throws CryptoFailureException
 91  
     {
 92  
         try
 93  
         {
 94  20
             byte[] buf = encryptCipher.doFinal(data);
 95  20
             if (base64Encoding)
 96  
             {
 97  20
                 return Base64.encodeBytes(buf).getBytes();
 98  
             }
 99  
             else
 100  
             {
 101  0
                 return buf;
 102  
             }
 103  
         }
 104  0
         catch (Exception e)
 105  
         {
 106  0
             throw new CryptoFailureException(this, e);
 107  
         }
 108  
     }
 109  
 
 110  
     public byte[] decrypt(byte[] data, Object info) throws CryptoFailureException
 111  
     {
 112  
         try
 113  
         {
 114  14
             byte[] dec = data;
 115  14
             if (base64Encoding)
 116  
             {
 117  14
                 dec = Base64.decode(new String(data));
 118  
             }
 119  14
             return decryptCipher.doFinal(dec);
 120  
         }
 121  0
         catch (Exception e)
 122  
         {
 123  0
             throw new CryptoFailureException(this, e);
 124  
         }
 125  
     }
 126  
 
 127  
     public String getAlgorithm()
 128  
     {
 129  48
         return algorithm;
 130  
     }
 131  
 
 132  
     public void setAlgorithm(String algorithm)
 133  
     {
 134  6
         this.algorithm = algorithm;
 135  6
     }
 136  
 
 137  
     public String toString()
 138  
     {
 139  0
         StringBuffer buf = new StringBuffer();
 140  0
         buf.append("Algorithm=").append(algorithm);
 141  0
         return buf.toString();
 142  
     }
 143  
 
 144  
     public boolean isBase64Encoding()
 145  
     {
 146  0
         return base64Encoding;
 147  
     }
 148  
 
 149  
     public void setBase64Encoding(boolean base64Encoding)
 150  
     {
 151  0
         this.base64Encoding = base64Encoding;
 152  0
     }
 153  
 
 154  
     protected abstract KeySpec createKeySpec();
 155  
 
 156  
     protected abstract AlgorithmParameterSpec createAlgorithmParameterSpec();
 157  
 
 158  
 }