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.KeyGenerator; 18 import javax.crypto.SecretKey; 19 import javax.crypto.spec.SecretKeySpec; 20 21 import org.mule.config.i18n.Message; 22 import org.mule.config.i18n.Messages; 23 import org.mule.umo.lifecycle.InitialisationException; 24 import org.mule.util.ObjectFactory; 25 import org.mule.util.StringMessageUtils; 26 27 36 public class SecretKeyEncryptionStrategy extends AbstractJCEEncryptionStrategy 37 { 38 39 public static final String DEFAULT_ALGORITHM = "Blowfish"; 40 41 private byte[] key; 42 private ObjectFactory keyFactory; 43 44 public SecretKeyEncryptionStrategy() 45 { 46 algorithm = DEFAULT_ALGORITHM; 47 } 48 49 public void initialise() throws InitialisationException 50 { 51 if (key == null) 52 { 53 if (keyFactory == null) 54 { 55 throw new InitialisationException(new Message(Messages.X_IS_NULL, "Key / KeyFactory"), this); 56 } 57 else 58 { 59 try 60 { 61 key = (byte[])keyFactory.create(); 62 } 63 catch (Exception e) 64 { 65 throw new InitialisationException(e, this); 66 } 67 } 68 } 69 super.initialise(); 70 } 71 72 protected KeySpec createKeySpec() 73 { 74 return new SecretKeySpec(key, algorithm); 75 } 76 77 protected AlgorithmParameterSpec createAlgorithmParameterSpec() 78 { 79 return null; 80 } 81 82 public void setKey(byte[] rawKey) 83 { 84 this.key = rawKey; 85 } 86 87 public void setKey(String rawKey) 88 { 89 this.key = StringMessageUtils.getBytes(rawKey); 90 } 91 92 public ObjectFactory getKeyFactory() 93 { 94 return keyFactory; 95 } 96 97 public void setKeyFactory(ObjectFactory keyFactory) 98 { 99 this.keyFactory = keyFactory; 100 } 101 102 protected SecretKey getSecretKey() throws GeneralSecurityException 103 { 104 return KeyGenerator.getInstance(algorithm).generateKey(); 105 } 106 107 } 108 | Popular Tags |