Example usage for javax.crypto CipherInputStream read

List of usage examples for javax.crypto CipherInputStream read

Introduction

In this page you can find the example usage for javax.crypto CipherInputStream read.

Prototype

public int read(byte b[], int off, int len) throws IOException 

Source Link

Document

Reads up to len bytes of data from this input stream into an array of bytes.

Usage

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.// ww  w.  j  a v  a2 s  .  c  om
 *
 * @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  . ja va2s  .c  o  m
 * <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();
}