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 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


                this.skeySpec = skeySpec;

                Cipher cipher = Cipher.getInstance(algorithm);
                cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

                this.cipherOut = new CipherOutputStream(out, cipher);
            } catch (GeneralSecurityException e) {
                throw (IOException) new IOException().initCause(e);
            }
        }
View Full Code Here

        HMACAccumulator hmac = getMessageAuthenticationCode(key);
        if (plaintextStream != null) {
            inlineInitVector(outputStream, iv);
            byte[] buffer = new byte[bufferSize];
            int read;
            CipherOutputStream cipherStream = null;
            try {
                cipherStream = new CipherOutputStream(outputStream, initializeCipher(ENCRYPT_MODE, key, iv));
                while ((read = plaintextStream.read(buffer)) > 0) {
                    cipherStream.write(buffer, 0, read);
                    cipherStream.flush();
                    hmac.encryptUpdate(buffer, read);
                }
                // only write if there is data to write (IBM JDK throws exception if no data)
                byte[] mac = hmac.getCalculatedMac();
                if (mac != null && mac.length > 0) {
                    cipherStream.write(mac);
                }
            } finally {
                IOHelper.close(cipherStream, "cipher", LOG);
                IOHelper.close(plaintextStream, "plaintext", LOG);
            }
View Full Code Here

       
        /**
         * {@inheritDoc}
         */
        public OutputStream applyFilter(OutputStream out) throws IOException {
            return new CipherOutputStream(out,
                    encryption.initCipher(number, generation));
        }
View Full Code Here

        }

        try {
            outStreamCipher.init(Cipher.ENCRYPT_MODE, key);

            return new CipherOutputStream(out, outStreamCipher);
        } catch (java.security.InvalidKeyException e) {
            throw Error.error(ErrorCode.X_S0531, e);
        }
    }
View Full Code Here

   *
   * @param  out  Description of the Parameter
   * @return      Description of the Return Value
   */
  public static OutputStream createEncryptedOutputStream(OutputStream out) {
    return new CipherOutputStream(out, createCipher(Cipher.ENCRYPT_MODE));
  }//}}}
View Full Code Here

      cipher = getCipher(1, "passwordfile");

      DataOutputStream dos;
      if (cipher != null) {
        dos = new DataOutputStream(new CipherOutputStream(new FileOutputStream(lastLogin), cipher));
      } else {
        dos = new DataOutputStream(new FileOutputStream(lastLogin, true));
      }
      dos.writeUTF(userName);
      dos.writeBoolean(false);
View Full Code Here

      Cipher cipher = getCipher(1, "passwordfile");

      DataOutputStream dos;
      if (cipher != null) {
        dos = new DataOutputStream(new CipherOutputStream(new FileOutputStream(lastLogin), cipher));
      } else {
        dos = new DataOutputStream(new FileOutputStream(lastLogin, true));
      }
      for (String user : usernames.keySet()) {
        dos.writeUTF(user);
View Full Code Here

     */
    public CipherOutputStream encrypt(OutputStream os) throws InvalidKeyException,
        InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException
    {
        Cipher cipher = initEncryptModeCipher();
        return new CipherOutputStream(os, cipher);
    }
View Full Code Here

     */
    public CipherOutputStream decrypt(OutputStream os) throws InvalidKeyException,
        InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException
    {
        Cipher cipher = initDecryptModeCipher();
        return new CipherOutputStream(os, cipher);
    }
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.