Package javax.crypto

Examples of javax.crypto.CipherOutputStream


     
      try
      {
       
        java.util.zip.ZipOutputStream zos = null;
        CipherOutputStream cos = null;
       
        // Check whether we need to encrypt the file content:
        if (passCode != null && passCode.length() > 0)
        {
         
          // Write IV as a prefix:
          SecureRandom random = new SecureRandom();
          byte[] iv = new byte[IV_LENGTH];
          random.nextBytes(iv);
          os.write(iv);
          os.flush();
         
          Cipher cipher = null;
          try
          {
            cipher = getCipher(Cipher.ENCRYPT_MODE, passCode, iv);
          }
          catch (GeneralSecurityException gse)
          {
            throw new ManifoldCFException("Could not encrypt configuratiom file: " + gse.getMessage());
          }
         
          cos = new CipherOutputStream(os, cipher);
          zos = new java.util.zip.ZipOutputStream(cos);
        }
        else
          zos = new java.util.zip.ZipOutputStream(os);
        try
        {
          // Now, work within a transaction.
          database.beginTransaction();
          try
          {
            // At the outermost level, I've decided that the best structure is to have a zipentry for each
            // manager.  Each manager must manage its own data as a binary blob, including any format versioning information,
            // This guarantees flexibility for the future.

            // The zipentries must be written in an order that permits their proper restoration.  The "lowest level" is thus
            // written first, which yields the order: authority connections, repository connections, jobs
            java.util.zip.ZipEntry outputEntry = new java.util.zip.ZipEntry("outputs");
            zos.putNextEntry(outputEntry);
            outputManager.exportConfiguration(zos);
            zos.closeEntry();

            java.util.zip.ZipEntry mappingEntry = new java.util.zip.ZipEntry("mappings");
            zos.putNextEntry(mappingEntry);
            mappingManager.exportConfiguration(zos);
            zos.closeEntry();

            java.util.zip.ZipEntry authEntry = new java.util.zip.ZipEntry("authorities");
            zos.putNextEntry(authEntry);
            authManager.exportConfiguration(zos);
            zos.closeEntry();

            java.util.zip.ZipEntry connEntry = new java.util.zip.ZipEntry("connections");
            zos.putNextEntry(connEntry);
            connManager.exportConfiguration(zos);
            zos.closeEntry();

            java.util.zip.ZipEntry jobsEntry = new java.util.zip.ZipEntry("jobs");
            zos.putNextEntry(jobsEntry);
            jobManager.exportConfiguration(zos);
            zos.closeEntry();

            // All done
          }
          catch (ManifoldCFException e)
          {
            database.signalRollback();
            throw e;
          }
          catch (Error e)
          {
            database.signalRollback();
            throw e;
          }
          finally
          {
            database.endTransaction();
          }
        }
        finally
        {
          zos.close();
          if (cos != null) {
            cos.close();
          }
        }
      }
      finally
      {
View Full Code Here


      ByteArrayOutputStream outStream = new ByteArrayOutputStream(1024 + 32);

      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7");

      cipher.init(Cipher.ENCRYPT_MODE, key);
      CipherOutputStream out = new CipherOutputStream(outStream, cipher);

      byte[] b = new byte[2048];
      int n;
      while ((n = in.read(b)) != -1)
        if (n > 0)
          out.write(b, 0, n);

      out.close();

      byte[] ciphertxt = outStream.toByteArray();
      harness.check(Arrays.equals(ciphertxt, CIPHERTEXT), "Cipher text MUST match");
  }
View Full Code Here

      hOut.writeObject(cookieObj);

      hOut.close();

      TempOutputStream cipherOut = new TempOutputStream();
      CipherOutputStream cOut
        = new CipherOutputStream(cipherOut, cipher);

      tos.writeToStream(cOut);
      tos.destroy();

      cOut.close();

      byte []encryptedData = cipherOut.toByteArray();

      cipherOut.destroy();
View Full Code Here

        }
        return b;
    }

    protected void execute(String inputFile, String outputFile) throws Exception {
        OutputStream out = new CipherOutputStream(new FileOutputStream(outputFile),
                cipher);
        InputStream in = new FileInputStream(inputFile);
        int numRead = 0;
        byte[] buf = new byte[100];
        while ((numRead = in.read(buf)) > 0) {
            out.write(buf, 0, numRead);
        }
        out.flush();
        out.close();
    }
View Full Code Here

            // see also [MS-OFFCRYPT] - 2.3.4.15
            // The final data block MUST be padded to the next integral multiple of the
            // KeyData.blockSize value. Any padding bytes can be used. Note that the StreamSize
            // field of the EncryptedPackage field specifies the number of bytes of
            // unencrypted data as specified in section 2.3.4.4.
            CipherOutputStream cryptStream = new CipherOutputStream(rawStream, getCipher(getSecretKey(), "PKCS5Padding"));
           
            this.out = cryptStream;
        }
View Full Code Here

      // rather than calling secure random directly.
      initVector = cipher.getIV();
      cryptoInitParams.put(CryptoInitProperty.INITIALIZATION_VECTOR, initVector);
    }
   
    CipherOutputStream cipherOutputStream = new CipherOutputStream(out, cipher);
    BufferedOutputStream bufferedCipherOutputStream = new BufferedOutputStream(cipherOutputStream);
   
    return bufferedCipherOutputStream;
  }
View Full Code Here

        log.error(e);
        throw new RuntimeException(e);
      }

      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      CipherOutputStream cipherStream = new CipherOutputStream(byteArrayOutputStream, cipher);
     
     
      if (Cipher.DECRYPT_MODE == encryptionMode) {
        cipherStream.write(context.getEncryptedSecretKey());
        cipherStream.flush();       
        byte[] plaintextSecretKey = byteArrayOutputStream.toByteArray();

        cipherStream.close();
       
        context.setPlaintextSecretKey(plaintextSecretKey);
      } else {
        cipherStream.write(context.getPlaintextSecretKey());
        cipherStream.flush();       
        byte[] encryptedSecretKey = byteArrayOutputStream.toByteArray();

        cipherStream.close();
       
        context.setEncryptedSecretKey(encryptedSecretKey);
        context.setOpaqueKeyEncryptionKeyID(pathToKeyName);
      }
     
View Full Code Here

                        "stream for cipher ", e, log);
            }
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CipherOutputStream out = new CipherOutputStream(baos, cipher);

        byte[] buffer = new byte[64];
        int length;
        try {
            while ((length = sourceStream.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
        } catch (IOException e) {
            throw new SecureVaultException("IOError when reading the input" +
                    " stream for cipher ", e, log);
        } finally {
            try {
                sourceStream.close();
                out.flush();
                out.close();
            } catch (IOException ignored) {
                // ignore exception
            }
        }
View Full Code Here

    output.write(SIGNATURE);
    output.write(salt.length);
    output.write(salt);
    output.write(rounds);
    ObjectOutputStream oout = new ObjectOutputStream(
        new CipherOutputStream(output, cipher));
    try {
      oout.writeObject(secrets);
    } finally {
      try {if (null != oout) oout.close();} catch (IOException ex) {}
    }
View Full Code Here

     
      try
      {
       
        java.util.zip.ZipOutputStream zos = null;
        CipherOutputStream cos = null;
       
        // Check whether we need to encrypt the file content:
        if (passCode != null && passCode.length() > 0)
        {
         
          // Write IV as a prefix:
          SecureRandom random = new SecureRandom();
          byte[] iv = new byte[IV_LENGTH];
          random.nextBytes(iv);
          os.write(iv);
          os.flush();
         
          Cipher cipher = null;
          try
          {
            cipher = getCipher(Cipher.ENCRYPT_MODE, passCode, iv);
          }
          catch (GeneralSecurityException gse)
          {
            throw new ManifoldCFException("Could not encrypt configuratiom file: " + gse.getMessage());
          }
         
          cos = new CipherOutputStream(os, cipher);
          zos = new java.util.zip.ZipOutputStream(cos);
        }
        else
          zos = new java.util.zip.ZipOutputStream(os);
        try
        {
          // Now, work within a transaction.
          database.beginTransaction();
          try
          {
            // At the outermost level, I've decided that the best structure is to have a zipentry for each
            // manager.  Each manager must manage its own data as a binary blob, including any format versioning information,
            // This guarantees flexibility for the future.

            // The zipentries must be written in an order that permits their proper restoration.  The "lowest level" is thus
            // written first, which yields the order: authority connections, repository connections, jobs
            java.util.zip.ZipEntry outputEntry = new java.util.zip.ZipEntry("outputs");
            zos.putNextEntry(outputEntry);
            outputManager.exportConfiguration(zos);
            zos.closeEntry();

            java.util.zip.ZipEntry authEntry = new java.util.zip.ZipEntry("authorities");
            zos.putNextEntry(authEntry);
            authManager.exportConfiguration(zos);
            zos.closeEntry();

            java.util.zip.ZipEntry connEntry = new java.util.zip.ZipEntry("connections");
            zos.putNextEntry(connEntry);
            connManager.exportConfiguration(zos);
            zos.closeEntry();

            java.util.zip.ZipEntry jobsEntry = new java.util.zip.ZipEntry("jobs");
            zos.putNextEntry(jobsEntry);
            jobManager.exportConfiguration(zos);
            zos.closeEntry();

            // All done
          }
          catch (ManifoldCFException e)
          {
            database.signalRollback();
            throw e;
          }
          catch (Error e)
          {
            database.signalRollback();
            throw e;
          }
          finally
          {
            database.endTransaction();
          }
        }
        finally
        {
          zos.close();
          if (cos != null) {
            cos.close();
          }
        }
      }
      finally
      {
View Full Code Here

TOP

Related Classes of javax.crypto.CipherOutputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.