List of usage examples for javax.crypto CipherInputStream close
public void close() throws IOException
From source file:Main.java
public static void decrypt(String fileIn, String fileOut, byte key[]) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { FileInputStream fis = new FileInputStream(fileIn); FileOutputStream fos = new FileOutputStream(fileOut); // Length is 32 bytes //byte key[] = "1010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100001111000011".getBytes("UTF-8"); MessageDigest sha = MessageDigest.getInstance("SHA-256"); key = sha.digest(key);/*from w w w . j av a 2 s .co m*/ SecretKeySpec sks = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, sks); CipherInputStream cis = new CipherInputStream(fis, cipher); int b; byte[] d = new byte[8]; while ((b = cis.read(d)) != -1) { fos.write(d, 0, b); } fos.flush(); fos.close(); cis.close(); }
From source file:MainClass.java
public static void blowfishEncrypt(String f1, String f2) throws Exception { SecretKey key = null;//from w w w.j a v a 2 s . com ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("BlowfishKey.ser")); key = (SecretKey) keyFile.readObject(); keyFile.close(); Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(f1)), cipher); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f2)); int i; do { i = in.read(); if (i != -1) out.write(i); } while (i > 0); in.close(); out.close(); }
From source file:MainClass.java
public static void desEncrypt(String f1, String f2) throws Exception { SecretKey key = null;/*from w ww .j a va2 s . c o m*/ ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser")); key = (SecretKey) keyFile.readObject(); keyFile.close(); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(f1)), cipher); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f2)); int i; do { i = in.read(); if (i != -1) out.write(i); } while (i > 0); in.close(); out.close(); }
From source file:Main.java
private static boolean handleFile(int mode, byte[] key, byte[] iv, String sourceFilePath, String destFilePath) { File sourceFile = new File(sourceFilePath); File destFile = new File(destFilePath); try {/* w w w .ja v a 2 s. co m*/ if (sourceFile.exists() && sourceFile.isFile()) { if (!destFile.getParentFile().exists()) destFile.getParentFile().mkdirs(); destFile.createNewFile(); InputStream in = new FileInputStream(sourceFile); OutputStream out = new FileOutputStream(destFile); Cipher cipher = initCipher(mode, key, iv, AES_CFB_NOPADDING); CipherInputStream cin = new CipherInputStream(in, cipher); byte[] b = new byte[1024]; int read = 0; while ((read = cin.read(b)) != -1) { out.write(b, 0, read); out.flush(); } cin.close(); in.close(); out.close(); return true; } } catch (Exception e) { e.printStackTrace(); } return false; }
From source file:org.panbox.core.crypto.CryptCore.java
public static SecretKey decryptShareKey(ShareKeyDBEntry entry, PublicKey pubKey, PrivateKey privKey) { SecretKey result = null;/*from w ww. j av a2s . c o m*/ if (entry != null) { byte[] encSK = entry.getEncryptedKey(pubKey); byte[] sk = new byte[KeyConstants.SYMMETRIC_BLOCK_SIZE]; try { ASYMM_CIPHER.init(Cipher.DECRYPT_MODE, privKey); ByteArrayInputStream bis = new ByteArrayInputStream(encSK); CipherInputStream cis = new CipherInputStream(bis, ASYMM_CIPHER); cis.read(sk); cis.close(); bis.close(); result = new SecretKeySpec(sk, entry.getAlgorithm()); } catch (InvalidKeyException e) { logger.warn("Exception caught in CryptCore.decryptShareKey", e); } catch (IOException e) { logger.warn("Exception caught in CryptCore.decryptShareKey", e); } } return result; }
From source file:com.dc.util.file.FileSupport.java
private static void copyEncryptedStream(InputStream input, OutputStream output, String key) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { Cipher dcipher = Cipher.getInstance("AES"); CipherInputStream cis = null; try {//from w ww. j av a 2 s. co m dcipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES")); cis = new CipherInputStream(input, dcipher); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = cis.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } output.flush(); } finally { if (cis != null) { cis.close(); } } }
From source file:org.openmrs.module.clinicalsummary.io.UploadSummariesTask.java
/** * Method that will be called to process the summary collection file. The upload process will unpack the zipped collection of summary files and then * decrypt them./*from w w w . j av a 2 s . com*/ * * @throws Exception */ protected void processSummaries() throws Exception { String zipFilename = StringUtils.join(Arrays.asList(filename, TaskConstants.FILE_TYPE_ZIP), "."); String encryptedFilename = StringUtils.join(Arrays.asList(filename, TaskConstants.FILE_TYPE_ENCRYPTED), "."); File file = new File(TaskUtils.getZippedOutputPath(), zipFilename); OutputStream outStream = new BufferedOutputStream(new FileOutputStream(file)); ZipFile encryptedFile = new ZipFile(new File(TaskUtils.getEncryptedOutputPath(), encryptedFilename)); Enumeration<? extends ZipEntry> entries = encryptedFile.entries(); while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); String zipEntryName = zipEntry.getName(); if (!zipEntryName.endsWith(TaskConstants.FILE_TYPE_SAMPLE) && !zipEntryName.endsWith(TaskConstants.FILE_TYPE_SECRET)) { int count; byte[] data = new byte[TaskConstants.BUFFER_SIZE]; CipherInputStream zipCipherInputStream = new CipherInputStream( encryptedFile.getInputStream(zipEntry), cipher); while ((count = zipCipherInputStream.read(data, 0, TaskConstants.BUFFER_SIZE)) != -1) { outStream.write(data, 0, count); } zipCipherInputStream.close(); } } outStream.close(); File outputPath = TaskUtils.getSummaryOutputPath(); ZipFile zipFile = new ZipFile(file); Enumeration<? extends ZipEntry> zipEntries = zipFile.entries(); while (zipEntries.hasMoreElements()) { ZipEntry zipEntry = zipEntries.nextElement(); processedFilename = zipEntry.getName(); File f = new File(outputPath, zipEntry.getName()); // ensure that the parent path exists File parent = f.getParentFile(); if (parent.exists() || parent.mkdirs()) { FileCopyUtils.copy(zipFile.getInputStream(zipEntry), new BufferedOutputStream(new FileOutputStream(f))); } } }
From source file:org.openmrs.module.clinicalsummary.io.DownloadSummariesTask.java
/** * Method that will be called to process the summary collection file. Download process will create one zipped and encrypted collection of summary * files.//from w ww. j av a 2s . c om * <p/> * this.passphrase = password; * * @throws Exception */ protected final void processSummaries() throws Exception { // TODO: The better approach would be to create zip file and then encrypt it. // And then Content of the zip file: // * Zipped file of summary files and sql file // * Sample file to be used for decryption testing String zipFilename = StringUtils.join(Arrays.asList(filename, TaskConstants.FILE_TYPE_ZIP), "."); File zipFile = new File(TaskUtils.getZippedOutputPath(), zipFilename); ZipOutputStream zipOutStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile))); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, -1); Date cutOffDate = null; if (BooleanUtils.isTrue(partial)) { cutOffDate = calendar.getTime(); } File inputPath = TaskUtils.getSummaryOutputPath(); File[] files = inputPath.listFiles(); if (files != null) { for (File file : files) { processStream(zipOutStream, inputPath.getAbsolutePath(), file, cutOffDate); } } zipOutStream.close(); String encryptedFilename = StringUtils.join(Arrays.asList(filename, TaskConstants.FILE_TYPE_ENCRYPTED), "."); File encryptedOutFile = new File(TaskUtils.getEncryptedOutputPath(), encryptedFilename); ZipOutputStream encryptedZipOutStream = new ZipOutputStream( new BufferedOutputStream(new FileOutputStream(encryptedOutFile))); int count; byte[] data; // add the 16 bytes init vector for the cipher into the output stream String secretFilename = StringUtils.join(Arrays.asList(filename, TaskConstants.FILE_TYPE_SECRET), "."); ZipEntry ivZipEntry = new ZipEntry(secretFilename); encryptedZipOutStream.putNextEntry(ivZipEntry); // write the 16 bytes init vector for the cipher into the output stream AlgorithmParameters params = cipher.getParameters(); byte[] initVector = params.getParameterSpec(IvParameterSpec.class).getIV(); encryptedZipOutStream.write(initVector); // add the sample file entry String sampleFilename = StringUtils.join(Arrays.asList(filename, TaskConstants.FILE_TYPE_SAMPLE), "."); ZipEntry sampleZipEntry = new ZipEntry(sampleFilename); encryptedZipOutStream.putNextEntry(sampleZipEntry); // write the sample file data = new byte[TaskConstants.BUFFER_SIZE]; String sampleText = "This is sample text inside encrypted document. " + "If you see this text, that means your decryption parameters is correct"; InputStream inStream = new ByteArrayInputStream(sampleText.getBytes()); CipherInputStream sampleCipherInStream = new CipherInputStream(inStream, cipher); while ((count = sampleCipherInStream.read(data, 0, TaskConstants.BUFFER_SIZE)) != -1) { encryptedZipOutStream.write(data, 0, count); } sampleCipherInStream.close(); // add the zipped summaries ZipEntry zipEntry = new ZipEntry(zipFile.getName()); encryptedZipOutStream.putNextEntry(zipEntry); // write the zipped summaries data = new byte[TaskConstants.BUFFER_SIZE]; InputStream zipInStream = new BufferedInputStream(new FileInputStream(zipFile)); CipherInputStream zipCipherInStream = new CipherInputStream(zipInStream, cipher); while ((count = zipCipherInStream.read(data, 0, TaskConstants.BUFFER_SIZE)) != -1) { encryptedZipOutStream.write(data, 0, count); } zipCipherInStream.close(); encryptedZipOutStream.close(); }
From source file:craterdog.security.MessageCryptex.java
@Override public void decryptStream(SecretKey sharedKey, InputStream input, OutputStream output) throws IOException { logger.entry();//from w w w . j a va 2 s . c o m CipherInputStream cipherInput = null; try { logger.debug("Creating a special input stream to do the work..."); cipherInput = decryptionInputStream(sharedKey, input); logger.debug("Reading bytes, decrypting them, and writing them out..."); IOUtils.copy(cipherInput, output); output.flush(); } finally { if (cipherInput != null) cipherInput.close(); } logger.exit(); }
From source file:com.cubusmail.server.mail.security.MailPasswordEncryptor.java
public String decryptPassword(byte[] encryptedPassword) { Cipher cipher;/*from ww w . ja va 2 s.com*/ try { log.debug("decrypt..."); cipher = Cipher.getInstance(this.algorithm); cipher.init(Cipher.DECRYPT_MODE, this.keyPair.getPrivate()); CipherInputStream cis = new CipherInputStream(new ByteArrayInputStream(encryptedPassword), cipher); ByteArrayOutputStream baosDecryptedData = new ByteArrayOutputStream(); byte[] buffer = new byte[8192]; int len = 0; while ((len = cis.read(buffer)) > 0) { baosDecryptedData.write(buffer, 0, len); } baosDecryptedData.flush(); cis.close(); log.debug("...finish"); return new String(baosDecryptedData.toByteArray()); } catch (Exception e) { log.error(e.getMessage(), e); throw new IllegalStateException(e.getMessage(), e); } }