1 10 11 package org.mule.impl.security; 12 13 import org.mule.config.i18n.Message; 14 import org.mule.config.i18n.Messages; 15 import org.mule.umo.lifecycle.InitialisationException; 16 17 import javax.crypto.SecretKey; 18 import javax.crypto.SecretKeyFactory; 19 import javax.crypto.spec.PBEKeySpec; 20 import javax.crypto.spec.PBEParameterSpec; 21 22 import java.security.GeneralSecurityException ; 23 import java.security.spec.AlgorithmParameterSpec ; 24 import java.security.spec.KeySpec ; 25 26 34 public class PasswordBasedEncryptionStrategy extends AbstractJCEEncryptionStrategy 35 { 36 37 public static final String DEFAULT_ALGORITHM = "PBEWithMD5AndDES"; 38 private byte[] salt = null; 39 40 private int iterationCount = 20; 41 private char[] password; 42 43 public PasswordBasedEncryptionStrategy() 44 { 45 algorithm = DEFAULT_ALGORITHM; 46 } 47 48 public void initialise() throws InitialisationException 49 { 50 if (salt == null) 51 { 52 salt = new byte[]{(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c, (byte)0x7e, (byte)0xc8, 53 (byte)0xee, (byte)0x99}; 54 logger.debug("Salt is not set. Using default salt"); 55 } 56 57 if (password == null) 58 { 59 throw new InitialisationException(new Message(Messages.X_IS_NULL, "Password"), this); 60 } 61 super.initialise(); 62 } 63 64 protected KeySpec createKeySpec() 65 { 66 return new PBEKeySpec(password); 67 } 68 69 protected AlgorithmParameterSpec createAlgorithmParameterSpec() 70 { 71 return new PBEParameterSpec(salt, iterationCount); 72 } 73 74 public byte[] getSalt() 75 { 76 return salt; 77 } 78 79 public void setSalt(byte[] salt) 80 { 81 this.salt = salt; 82 } 83 84 public int getIterationCount() 85 { 86 return iterationCount; 87 } 88 89 public void setIterationCount(int iterationCount) 90 { 91 this.iterationCount = iterationCount; 92 } 93 94 public void setPassword(String password) 95 { 96 this.password = password.toCharArray(); 97 } 98 99 protected SecretKey getSecretKey() throws GeneralSecurityException 100 { 101 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(getAlgorithm()); 102 return keyFactory.generateSecret(keySpec); 103 } 104 } 105 | Popular Tags |