1 20 21 package net.sourceforge.lightcrypto; 22 23 import org.bouncycastle.crypto.BufferedBlockCipher; 24 import org.bouncycastle.crypto.engines.AESLightEngine; 25 import org.bouncycastle.crypto.modes.CBCBlockCipher; 26 import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; 27 import org.bouncycastle.crypto.params.KeyParameter; 28 import org.bouncycastle.crypto.params.ParametersWithIV; 29 import org.bouncycastle.util.encoders.Base64; 30 31 import java.io.*; 32 import java.security.SecureRandom ; 33 34 42 43 public class Crypt { 44 private static int BUFFERSIZE_TEXT = 64; 45 private static int BUFFERSIZE_FILE = 8192; 46 47 57 public static StringBuffer encrypt( 58 StringBuffer text 59 , SafeObject keybytes 60 ) throws CryptoException, IOException { 61 62 return encrypt(text, keybytes, null); 63 } 64 65 76 public static StringBuffer encrypt( 77 StringBuffer text 78 , SafeObject keybytes 79 , StringBuffer seed 80 ) throws CryptoException, IOException { 81 82 ByteArrayOutputStream bao = new ByteArrayOutputStream(); 83 DataOutputStream dao = new DataOutputStream(bao); 84 85 encrypt(new ByteArrayInputStream(text.toString().getBytes()), dao, keybytes, seed, BUFFERSIZE_TEXT); 87 88 StringBuffer result = new StringBuffer (new String (Base64.encode(bao.toByteArray()))); 89 90 dao.flush(); 92 dao.close(); 93 94 return result; 95 } 96 97 107 public static void encryptFile( 108 String file 109 , String newfile 110 , SafeObject keybytes 111 ) throws CryptoException, IOException { 112 encryptFile(file, newfile, keybytes, null); 113 } 114 115 126 public static void encryptFile( 127 String file 128 , String newfile 129 , SafeObject keybytes 130 , StringBuffer seed 131 ) throws CryptoException, IOException { 132 133 FileInputStream fis = new FileInputStream(file); 134 FileOutputStream fos = new FileOutputStream(newfile); 135 136 DataOutputStream dao = new DataOutputStream(fos); 137 138 encrypt(fis, dao, keybytes, seed, BUFFERSIZE_FILE); 140 141 dao.flush(); 143 dao.close(); 144 145 fis.close(); 147 fos.close(); 148 } 149 150 161 public static void encrypt( 162 InputStream is 163 , DataOutputStream daos 164 , SafeObject keybytes 165 , StringBuffer seed 166 , int bufferlength 167 ) throws CryptoException { 168 169 KeyParameter key = null; 170 171 try { 172 SecureRandom sr = new SecureRandom (); 173 174 if (seed != null && !seed.equals("")) { 176 sr.setSeed(seed.toString().getBytes()); 177 } 178 179 AESLightEngine blockCipher = new AESLightEngine(); 181 CBCBlockCipher cbcCipher = new CBCBlockCipher(blockCipher); 182 BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbcCipher); 183 184 byte[] iv = new byte[blockCipher.getBlockSize()]; 186 sr.nextBytes(iv); 187 188 key = new KeyParameter(keybytes.getBytes()); 190 ParametersWithIV ivparam = new ParametersWithIV(key, iv); 192 193 daos.write(iv, 0, iv.length); 195 196 byte[] buffer = new byte[bufferlength]; 198 int length = cipher.getOutputSize(bufferlength); 199 byte[] result = new byte[length]; 200 int outputLen = 0; 201 202 cipher.init(true, ivparam); 204 205 while ((length = is.read(buffer)) != -1) { 207 outputLen = cipher.processBytes(buffer, 0, length, result, 0); 208 209 if (outputLen > 0) { 210 daos.write(result, 0, outputLen); 211 } 212 } 213 214 outputLen = cipher.doFinal(result, 0); 216 if (outputLen > 0) { 217 daos.write(result, 0, outputLen); 218 } 219 220 } catch (Exception ex) { 221 ex.printStackTrace(); 222 throw new CryptoException(ex.getMessage()); 223 } finally { 224 key = null; 226 if (seed != null) { 227 Clean.blank(seed); 228 seed = null; 229 } 230 } 231 } 232 233 242 public static StringBuffer decrypt( 243 StringBuffer text 244 , SafeObject keybytes 245 ) throws CryptoException, IOException { 246 247 ByteArrayOutputStream bao = new ByteArrayOutputStream(); 248 DataOutputStream dao = new DataOutputStream(bao); 249 250 decrypt(new ByteArrayInputStream(Base64.decode(text.toString())), dao, keybytes, BUFFERSIZE_TEXT); 252 253 dao.flush(); 255 dao.close(); 256 257 return new StringBuffer (new String (bao.toByteArray())); 258 } 259 260 269 public static void decryptFile( 270 String file 271 , String newfile 272 , SafeObject keybytes 273 ) throws CryptoException, IOException { 274 275 FileInputStream fis = new FileInputStream(file); 276 FileOutputStream fos = new FileOutputStream(newfile); 277 DataOutputStream dao = new DataOutputStream(fos); 278 279 decrypt(fis, dao, keybytes, BUFFERSIZE_FILE); 281 282 dao.flush(); 284 dao.close(); 285 286 fis.close(); 288 fos.close(); 289 } 290 291 300 public static void decrypt( 301 InputStream is 302 , DataOutputStream daos 303 , SafeObject keybytes 304 , int bufferlength 305 ) throws CryptoException { 306 KeyParameter key = null; 307 308 try { 309 key = new KeyParameter(keybytes.getBytes()); 311 312 AESLightEngine blockCipher = new AESLightEngine(); 313 CBCBlockCipher cbcCipher = new CBCBlockCipher(blockCipher); 314 BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbcCipher); 315 316 byte[] iv = new byte[blockCipher.getBlockSize()]; 318 is.read(iv); 319 320 ParametersWithIV ivparam = new ParametersWithIV(key, iv); 322 323 byte[] buffer = new byte[bufferlength]; 324 int length = cipher.getOutputSize(buffer.length); 325 byte[] result = new byte[length]; 326 int outputLen = 0; 327 328 cipher.init(false, ivparam); 330 331 while ((length = is.read(buffer)) != -1) { 333 outputLen = cipher.processBytes(buffer, 0, length, result, 0); 334 335 if (outputLen > 0) { 336 daos.write(result, 0, outputLen); 337 } 338 } 339 340 outputLen = cipher.doFinal(result, 0); 342 if (outputLen > 0) { 343 daos.write(result, 0, outputLen); 344 } 345 346 } catch (Exception ex) { 347 ex.printStackTrace(); 348 throw new CryptoException(ex.getMessage()); 349 350 } finally { 351 key = null; 353 } 354 } 355 } 356 | Popular Tags |