1 21 22 package net.sourceforge.jcetaglib.lib; 23 24 import net.sourceforge.jcetaglib.exceptions.CryptoException; 25 import net.sourceforge.jcetaglib.exceptions.KeystoreException; 26 import org.bouncycastle.jce.provider.BouncyCastleProvider; 27 import org.bouncycastle.util.encoders.Base64; 28 29 import javax.crypto.Cipher; 30 import javax.crypto.CipherInputStream; 31 import javax.crypto.CipherOutputStream; 32 import javax.crypto.spec.IvParameterSpec; 33 import java.io.*; 34 import java.security.Key ; 35 import java.security.SecureRandom ; 36 import java.security.Security ; 37 38 46 public class Crypt { 47 48 private static int BUFFERSIZE_TEXT = 64; 50 private static int BUFFERSIZE_FILE = 8192; 51 52 66 public static StringBuffer encrypt(StringBuffer text 67 , String keyfile 68 , StringBuffer passphrase 69 , String algorithm 70 , String mode 71 , String padding 72 , byte[] seed) throws CryptoException, KeystoreException { 73 74 Key secretKey = Keystore.loadKey(algorithm, keyfile, passphrase); 76 77 return encrypt(text, secretKey, algorithm, mode, padding, seed); 79 } 80 81 93 public static StringBuffer encrypt(StringBuffer text 94 , Key secretKey 95 , String algorithm 96 , String mode 97 , String padding 98 , byte[] seed) throws CryptoException { 99 100 ByteArrayOutputStream bao = null; 101 DataOutputStream dao = null; 102 103 try { 104 bao = new ByteArrayOutputStream(); 105 dao = new DataOutputStream(bao); 106 107 encrypt(new ByteArrayInputStream(text.toString().getBytes()), dao, secretKey, algorithm, mode, padding, seed, BUFFERSIZE_TEXT); 109 return new StringBuffer (new String (Base64.encode(bao.toByteArray()))); 110 } catch (IOException ioe) { 111 ioe.printStackTrace(); 112 throw new CryptoException(ioe.getMessage()); 113 } finally { 114 if (dao != null) { 115 try { 117 dao.close(); 118 } catch (IOException e) { 119 ; 120 } 121 } 122 } 123 } 124 125 140 public static void encryptFile(String file 141 , String newfile 142 , String keyfile 143 , StringBuffer passphrase 144 , String algorithm 145 , String mode 146 , String padding 147 , byte[] seed) throws CryptoException, IOException, KeystoreException { 148 149 FileInputStream fis = null; 150 FileOutputStream fos = null; 151 DataOutputStream dao = null; 152 153 try { 154 fis = new FileInputStream(file); 155 156 fos = new FileOutputStream(newfile); 157 dao = new DataOutputStream(fos); 158 159 Key secretKey = Keystore.loadKey(algorithm, keyfile, passphrase); 161 162 encrypt(fis, dao, secretKey, algorithm, mode, padding, seed, BUFFERSIZE_FILE); 164 165 } catch (IOException ioe) { 166 ioe.printStackTrace(); 167 throw new IOException(ioe.getMessage()); 168 } finally { 169 if (dao != null) { 170 try { 172 dao.close(); 173 } catch (IOException e) { 174 ; 175 } 176 } 177 if (fis != null) { 178 try { 180 fis.close(); 181 } catch (IOException e) { 182 ; 183 } 184 } 185 } 186 } 187 188 202 public static void encrypt(InputStream is 203 , DataOutputStream daos 204 , Key secretKey 205 , String algorithm 206 , String mode 207 , String padding 208 , byte[] seed 209 , int bufferlength) 210 throws CryptoException, IOException { 211 212 IvParameterSpec spec = null; byte[] iv = null; Cipher cipher = null; 215 CipherOutputStream cStr = null; 216 217 try { 218 if (algorithm.equalsIgnoreCase("RC4")) { 219 cipher = Cipher.getInstance("RC4"); 221 } else { 222 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + padding, "BC"); 224 } 225 226 if (mode.equalsIgnoreCase("ECB") || algorithm.equalsIgnoreCase("RC4")) { 227 cipher.init(Cipher.ENCRYPT_MODE, secretKey); 228 } else { 229 SecureRandom sr = Seed.getSecureRandom(seed); 232 iv = new byte[cipher.getBlockSize()]; 234 sr.nextBytes(iv); 236 spec = new IvParameterSpec(iv); 238 239 cipher.init(Cipher.ENCRYPT_MODE, secretKey, spec); 240 } 241 242 cStr = new CipherOutputStream(daos, cipher); 243 244 if (iv != null) 246 daos.write(iv); 247 248 byte[] buffer = new byte[bufferlength]; 251 int length = 0; 252 while ((length = is.read(buffer)) != -1) 253 cStr.write(buffer, 0, length); 254 255 } catch (IOException ioe) { 256 ioe.printStackTrace(); 257 throw new IOException(ioe.getMessage()); 258 } catch (Exception ex) { 259 ex.printStackTrace(); 260 throw new CryptoException(ex.getMessage()); 261 } finally { 262 if (cStr != null) { 263 try { 264 cStr.close(); 265 } catch (IOException ioe) { 266 ; 267 } 268 } 269 } 270 } 271 272 285 public static StringBuffer decrypt(StringBuffer text 286 , String keyfile 287 , StringBuffer passphrase 288 , String algorithm 289 , String mode 290 , String padding) throws CryptoException, KeystoreException { 291 292 Key secretKey = Keystore.loadKey(algorithm, keyfile, passphrase); 294 295 return decrypt(text, secretKey, algorithm, mode, padding); 296 } 297 298 309 public static StringBuffer decrypt(StringBuffer text 310 , Key secretKey 311 , String algorithm 312 , String mode 313 , String padding) throws CryptoException { 314 315 ByteArrayOutputStream bao = null; 316 DataOutputStream dao = null; 317 318 try { 319 bao = new ByteArrayOutputStream(); 320 dao = new DataOutputStream(bao); 321 322 decrypt(new ByteArrayInputStream(Base64.decode(text.toString())), dao, secretKey, algorithm, mode, padding, BUFFERSIZE_TEXT); 324 325 return new StringBuffer (new String (bao.toByteArray())); 326 327 } catch (IOException ioe) { 328 ioe.printStackTrace(); 329 throw new CryptoException(ioe.getMessage()); 330 } finally { 331 if (dao != null) { 332 try { 334 dao.close(); 335 } catch (IOException e) { 336 ; 337 } 338 } 339 } 340 } 341 342 355 public static void decryptFile(String file 356 , String newfile 357 , String keyfile 358 , StringBuffer passphrase 359 , String algorithm 360 , String mode 361 , String padding) throws CryptoException, KeystoreException, IOException { 362 363 FileInputStream fis = null; 364 FileOutputStream fos = null; 365 DataOutputStream dao = null; 366 367 try { 368 fis = new FileInputStream(file); 369 370 fos = new FileOutputStream(newfile); 371 dao = new DataOutputStream(fos); 372 373 Key secretKey = Keystore.loadKey(algorithm, keyfile, passphrase); 375 376 decrypt(fis, dao, secretKey, algorithm, mode, padding, BUFFERSIZE_FILE); 378 379 } catch (IOException ioe) { 380 ioe.printStackTrace(); 381 throw new IOException(ioe.getMessage()); 382 } finally { 383 if (dao != null) { 384 try { 386 dao.close(); 387 } catch (IOException e) { 388 ; 389 } 390 } 391 if (fis != null) { 392 try { 394 fis.close(); 395 } catch (IOException e) { 396 ; 397 } 398 } 399 } 400 } 401 402 415 public static void decrypt(InputStream is 416 , DataOutputStream daos 417 , Key secretKey 418 , String algorithm 419 , String mode 420 , String padding 421 , int bufferlength) 422 throws CryptoException, IOException { 423 424 Cipher cipher = null; 425 IvParameterSpec spec = null; byte[] iv = null; CipherInputStream cStr = null; 429 430 try { 431 Security.addProvider(new BouncyCastleProvider()); 432 433 if (algorithm.equalsIgnoreCase("RC4")) { 434 cipher = Cipher.getInstance("RC4"); 436 } else { 437 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + padding, "BC"); 439 } 440 441 if (mode.equalsIgnoreCase("ECB") || algorithm.equalsIgnoreCase("RC4")) { 442 cipher.init(Cipher.DECRYPT_MODE, secretKey); 443 } else { 444 iv = new byte[cipher.getBlockSize()]; 446 is.read(iv); 447 448 spec = new IvParameterSpec(iv); 449 cipher.init(Cipher.DECRYPT_MODE, secretKey, spec); 450 } 451 452 cStr = new CipherInputStream(is, cipher); 453 454 byte[] buffer = new byte[bufferlength]; 457 int length = 0; 458 while ((length = cStr.read(buffer)) != -1) 459 daos.write(buffer, 0, length); 460 461 } catch (IOException ioe) { 462 ioe.printStackTrace(); 463 throw new IOException(ioe.getMessage()); 464 } catch (Exception ex) { 465 ex.printStackTrace(); 466 throw new CryptoException(ex.getMessage()); 467 } finally { 468 if (cStr != null) { 469 try { 470 cStr.close(); 471 } catch (IOException ioe) { 472 ; 473 } 474 } 475 } 476 } 477 } 478 | Popular Tags |