1 64 65 package com.jcorporate.expresso.core.security; 66 67 import com.jcorporate.expresso.core.misc.StringUtil; 68 import com.jcorporate.expresso.kernel.exception.ChainedException; 69 import org.apache.log4j.Logger; 70 71 72 77 abstract public class AbstractStringEncryption { 78 private static final String thisClass = "com.jcorporate.expresso.core.security.AbstracStringEncryption"; 79 private static final String defaultPW = "Expresso Rocks"; 80 private byte[] passKey = null; 81 boolean initialized = false; 82 static Logger logCat = Logger.getLogger(AbstractStringEncryption.class); 83 84 87 private CryptoManager cryptoManager; 88 89 92 public AbstractStringEncryption() { 93 initialized = true; 94 } 95 96 public synchronized void init() throws ChainedException { 97 preparePassKey(); 98 } 99 100 105 protected byte[] getPreparedPassKey() { 106 return passKey; 107 } 108 109 112 public synchronized void destroy() { 113 } 114 115 123 abstract public byte[] decrypt(byte[] inputData) 124 throws ChainedException; 125 126 127 135 public String decryptString(byte[] inputData) 136 throws IllegalStateException { 137 if (initialized == false) { 138 throw new IllegalStateException ("StringEncryptionClass is not initialized!"); 139 } 140 try { 141 if (inputData == null) { 142 throw new IllegalArgumentException ("inputData should not be null"); 143 } 144 if (inputData.length == 0) { 145 throw new IllegalArgumentException ("inputData should be of length > 0"); 146 } 147 148 return new String (decrypt(inputData)); 149 } catch (Exception e) { 150 151 logCat.error(thisClass + ".decryptString(byte)", e); 153 154 return ""; 155 } 156 } 157 158 159 166 abstract public byte[] encrypt(byte[] inputData) 167 throws ChainedException; 168 169 170 177 public byte[] encryptString(String inputData) 178 throws IllegalStateException , 179 IllegalArgumentException , ChainedException { 180 181 if (initialized == false) { 182 final String myName = thisClass + ".encryptString"; 183 throw new IllegalStateException (myName + 184 ":StringEncryptionClass is not initialized!"); 185 } 186 if (inputData == null || inputData.length() == 0) { 187 return "".getBytes(); 188 } 189 190 return encrypt(inputData.getBytes()); 191 } 192 193 194 198 public void preparePassKey() 199 throws ChainedException { 200 String pw = StringUtil.notNull(this.getCryptoManager().getCryptoKey()); 201 202 204 if (pw.length() == 0) { 207 pw = defaultPW; 208 logCat.warn("Didn't find cryptoKey in config file, using default key."); 209 } 210 211 StringHash sha = new StringHash(); 212 213 passKey = new byte[256 / 8]; 215 216 byte[] hash = sha.produceHash(pw.getBytes()); 217 218 for (int i = 0; i < hash.length; i++) { 223 passKey[i] = hash[i]; 224 } 225 for (int i = hash.length; i < passKey.length; i++) { 231 passKey[i] = hash[i - hash.length]; 232 } 233 } 234 235 public void setCryptoManager(CryptoManager cryptoManager) { 236 this.cryptoManager = cryptoManager; 237 } 238 239 public CryptoManager getCryptoManager() { 240 return cryptoManager; 241 } 242 243 244 } 245 | Popular Tags |