1 19 20 package net.sourceforge.jcetaglib.lib; 21 22 import net.sourceforge.jcetaglib.exceptions.CryptoException; 23 import org.bouncycastle.jce.provider.BouncyCastleProvider; 24 import org.bouncycastle.util.encoders.Base64; 25 26 import javax.crypto.*; 27 import javax.crypto.spec.PBEKeySpec; 28 import javax.crypto.spec.PBEParameterSpec; 29 import java.io.*; 30 import java.security.SecureRandom ; 31 import java.security.Security ; 32 33 41 public class PBECrypt { 42 private static int PBE_COUNT = 20; 44 45 private static int BUFFERSIZE_TEXT = 64; 47 private static int BUFFERSIZE_FILE = 8192; 48 49 58 public static StringBuffer encrypt(StringBuffer text 59 , StringBuffer passphrase 60 , String algorithm) throws CryptoException { 61 62 return encrypt(text, passphrase, null, algorithm); 63 } 64 65 75 public static StringBuffer encrypt(StringBuffer text 76 , StringBuffer passphrase 77 , byte[] seed 78 , String algorithm) throws CryptoException { 79 80 ByteArrayOutputStream bao = null; 81 DataOutputStream dao = null; 82 83 try { 84 bao = new ByteArrayOutputStream(); 85 dao = new DataOutputStream(bao); 86 87 encrypt(new ByteArrayInputStream(text.toString().getBytes()), dao, seed, passphrase, algorithm, BUFFERSIZE_TEXT); 89 return new StringBuffer (new String (Base64.encode(bao.toByteArray()))); 90 } catch (IOException ioe) { 91 ioe.printStackTrace(); 92 throw new CryptoException(ioe.getMessage()); 93 } finally { 94 if (dao != null) { 95 try { 97 dao.close(); 98 } catch (IOException e) { 99 ; 100 } 101 } 102 } 103 } 104 105 116 public static void encrypt(InputStream is 117 , DataOutputStream daos 118 , byte[] seed 119 , StringBuffer passphrase 120 , String algorithm 121 , int bufferlength) 122 throws CryptoException, IOException { 123 124 CipherOutputStream cStr = null; 125 PBEKeySpec pbeKeySpec; 126 PBEParameterSpec pbeParamSpec; 127 SecretKeyFactory keyFac; 128 SecretKey pbeKey; 129 Cipher pbeCipher; 130 131 try { 132 Security.addProvider(new BouncyCastleProvider()); 134 135 byte[] randomsalt = new byte[8]; 137 SecureRandom sr = Seed.getSecureRandom(seed); 138 sr.nextBytes(randomsalt); 139 140 pbeParamSpec = new PBEParameterSpec(randomsalt, PBE_COUNT); 142 143 pbeKeySpec = new PBEKeySpec(passphrase.toString().toCharArray()); 144 keyFac = SecretKeyFactory.getInstance(algorithm); 145 pbeKey = keyFac.generateSecret(pbeKeySpec); 146 147 pbeCipher = Cipher.getInstance(algorithm); 149 150 pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); 152 153 cStr = new CipherOutputStream(daos, pbeCipher); 155 156 daos.write(randomsalt); 158 159 byte[] buffer = new byte[bufferlength]; 161 int length = 0; 162 while ((length = is.read(buffer)) != -1) { 163 cStr.write(buffer, 0, length); 164 } 165 } catch (IOException ioe) { 166 ioe.printStackTrace(); 167 throw new IOException(ioe.getMessage()); 168 } catch (Exception ex) { 169 ex.printStackTrace(); 170 throw new CryptoException(ex.getMessage()); 171 } finally { 172 if (cStr != null) { 173 try { 174 cStr.close(); 175 } catch (IOException ioe) { 176 ; 177 } 178 } 179 } 180 } 181 182 192 public static void encryptFile(String file 193 , String newfile 194 , StringBuffer passphrase 195 , String algorithm) throws CryptoException, IOException { 196 encryptFile(file, newfile, passphrase, null, algorithm); 197 } 198 199 210 public static void encryptFile(String file 211 , String newfile 212 , StringBuffer passphrase 213 , byte[] seed 214 , String algorithm) throws CryptoException, IOException { 215 216 FileInputStream fis = null; 217 FileOutputStream fos = null; 218 DataOutputStream dao = null; 219 220 try { 221 fis = new FileInputStream(file); 222 223 fos = new FileOutputStream(newfile); 224 dao = new DataOutputStream(fos); 225 226 encrypt(fis, dao, seed, passphrase, algorithm, BUFFERSIZE_FILE); 228 229 } catch (IOException ioe) { 230 ioe.printStackTrace(); 231 throw new IOException(ioe.getMessage()); 232 } finally { 233 if (dao != null) { 234 try { 236 dao.close(); 237 } catch (IOException e) { 238 ; 239 } 240 } 241 if (fis != null) { 242 try { 244 fis.close(); 245 } catch (IOException e) { 246 ; 247 } 248 } 249 } 250 } 251 252 261 public static StringBuffer decrypt(StringBuffer text 262 , StringBuffer passphrase 263 , String algorithm) throws CryptoException { 264 265 ByteArrayOutputStream bao = null; 266 DataOutputStream dao = null; 267 268 try { 269 bao = new ByteArrayOutputStream(); 270 dao = new DataOutputStream(bao); 271 272 decrypt(new ByteArrayInputStream(Base64.decode(text.toString())), dao, passphrase, algorithm, BUFFERSIZE_TEXT); 274 275 return new StringBuffer (new String (bao.toByteArray())); 276 277 } catch (IOException ioe) { 278 ioe.printStackTrace(); 279 throw new CryptoException(ioe.getMessage()); 280 } finally { 281 if (dao != null) { 282 try { 284 dao.close(); 285 } catch (IOException e) { 286 ; 287 } 288 } 289 } 290 } 291 292 303 public static void decrypt(InputStream is 304 , DataOutputStream daos 305 , StringBuffer passphrase 306 , String algorithm 307 , int bufferlength) 308 throws CryptoException, IOException { 309 310 CipherInputStream ciStr = null; 311 PBEKeySpec pbeKeySpec; 312 PBEParameterSpec pbeParamSpec; 313 SecretKeyFactory keyFac; 314 SecretKey pbeKey; 315 Cipher pbeCipher; 316 317 try { 318 Security.addProvider(new BouncyCastleProvider()); 320 321 byte[] randomsalt = new byte[8]; 323 is.read(randomsalt); 324 325 pbeParamSpec = new PBEParameterSpec(randomsalt, PBE_COUNT); 327 328 pbeKeySpec = new PBEKeySpec(passphrase.toString().toCharArray()); 329 keyFac = SecretKeyFactory.getInstance(algorithm); 330 pbeKey = keyFac.generateSecret(pbeKeySpec); 331 332 pbeCipher = Cipher.getInstance(algorithm); 334 335 pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); 337 338 ciStr = new CipherInputStream(is, pbeCipher); 340 341 byte[] buffer = new byte[bufferlength]; 343 int length = 0; 344 while ((length = ciStr.read(buffer)) != -1) { 345 daos.write(buffer, 0, length); 346 } 347 } catch (IOException ioe) { 348 ioe.printStackTrace(); 349 throw new IOException(ioe.getMessage()); 350 } catch (Exception ex) { 351 ex.printStackTrace(); 352 throw new CryptoException(ex.getMessage()); 353 } finally { 354 if (ciStr != null) { 355 try { 356 ciStr.close(); 357 } catch (IOException ioe) { 358 ; 359 } 360 } 361 } 362 } 363 364 374 public static void decryptFile(String file 375 , String newfile 376 , StringBuffer passphrase 377 , String algorithm) throws CryptoException, IOException { 378 379 FileInputStream fis = null; 380 FileOutputStream fos = null; 381 DataOutputStream dao = null; 382 383 try { 384 fis = new FileInputStream(file); 385 386 fos = new FileOutputStream(newfile); 387 dao = new DataOutputStream(fos); 388 389 decrypt(fis, dao, passphrase, algorithm, BUFFERSIZE_FILE); 391 392 } catch (IOException ioe) { 393 ioe.printStackTrace(); 394 throw new IOException(ioe.getMessage()); 395 } finally { 396 if (dao != null) { 397 try { 399 dao.close(); 400 } catch (IOException e) { 401 ; 402 } 403 } 404 if (fis != null) { 405 try { 407 fis.close(); 408 } catch (IOException e) { 409 ; 410 } 411 } 412 } 413 } 414 } 415 | Popular Tags |