public void encrypt(File srcFile, String password, File trgtFile) throws FileNotFoundException, IOException { FileInputStream fis = null; FileWriter fw = null; BufferedInputStream bis = null; BufferedWriter bw = null; byte[] buffer = null; if (passwordEncoder.checkPassword(password)) { StandardPBEByteEncryptor cipher = new StandardPBEByteEncryptor(); cipher.setAlgorithm(algorithm); cipher.setPassword(password); buffer = new byte[blockSize]; LOGGER.debug("blockSize = {}", blockSize); try { fis = new FileInputStream(srcFile); bis = new BufferedInputStream(fis); fw = new FileWriter(trgtFile); bw = new BufferedWriter(fw); byte[] encrypted = null; int length = 0; long size = 0L; while ((length = bis.read(buffer)) >= 0) { if (length < blockSize) { byte[] tmp = new byte[length]; System.arraycopy(buffer, 0, tmp, 0, length); encrypted = cipher.encrypt(tmp); } else { encrypted = cipher.encrypt(buffer); } String line; try { line = new String(base64.encode(encrypted), "US-ASCII"); } catch (Exception e) { throw new RuntimeException(e); } bw.write(line); bw.newLine(); size += length; } bw.flush(); LOGGER.debug("processed bytes = {}", size); } finally { EgovResourceReleaser.close(fw, bw, fis, bis); } } else { LOGGER.error("password not matched!!!"); throw new IllegalArgumentException("password not matched!!!"); } }
public byte[] encrypt(byte[] data, String password) { if (passwordEncoder.checkPassword(password)) { StandardPBEByteEncryptor cipher = new StandardPBEByteEncryptor(); cipher.setAlgorithm(algorithm); cipher.setPassword(password); return cipher.encrypt(data); } else { LOGGER.error("password not matched!!!"); throw new IllegalArgumentException("password not matched!!!"); } }
public void decrypt(File encryptedFile, String password, File trgtFile) throws FileNotFoundException, IOException { FileReader fr = null; FileOutputStream fos = null; BufferedReader br = null; BufferedOutputStream bos = null; if (passwordEncoder.checkPassword(password)) { StandardPBEByteEncryptor cipher = new StandardPBEByteEncryptor(); cipher.setAlgorithm(algorithm); cipher.setPassword(password); try { fr = new FileReader(encryptedFile); br = new BufferedReader(fr); fos = new FileOutputStream(trgtFile); bos = new BufferedOutputStream(fos); byte[] encrypted = null; byte[] decrypted = null; String line = null; while ((line = br.readLine()) != null) { try { encrypted = base64.decode(line.getBytes("US-ASCII")); } catch (Exception e) { throw new RuntimeException(e); } decrypted = cipher.decrypt(encrypted); bos.write(decrypted); } bos.flush(); } finally { EgovResourceReleaser.close(fos, bos, fr, br); } } else { LOGGER.error("password not matched!!!"); throw new IllegalArgumentException("password not matched!!!"); } }