コード例 #1
0
  // TODO - zipEntry might use extended local header
  protected void add(ZipEntry zipEntry, ZipFileEntryInputStream zipData, String password)
      throws IOException, UnsupportedEncodingException {
    AESEncrypter aesEncrypter = new AESEncrypterBC(password.getBytes("iso-8859-1"));

    ExtZipEntry entry = new ExtZipEntry(zipEntry.getName());
    entry.setMethod(zipEntry.getMethod());
    entry.setSize(zipEntry.getSize());
    entry.setCompressedSize(zipEntry.getCompressedSize() + 28);
    entry.setTime(zipEntry.getTime());
    entry.initEncryptedEntry();

    zipOS.putNextEntry(entry);
    // ZIP-file data contains: 1. salt 2. pwVerification 3. encryptedContent 4. authenticationCode
    zipOS.writeBytes(aesEncrypter.getSalt());
    zipOS.writeBytes(aesEncrypter.getPwVerification());

    byte[] data = new byte[1024];
    int read = zipData.read(data);
    while (read != -1) {
      aesEncrypter.encrypt(data, read);
      zipOS.writeBytes(data, 0, read);
      read = zipData.read(data);
    }

    byte[] finalAuthentication = aesEncrypter.getFinalAuthentication();
    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine(
          "finalAuthentication="
              + Arrays.toString(finalAuthentication)
              + " at pos="
              + zipOS.getWritten());
    }

    zipOS.writeBytes(finalAuthentication);
  }
コード例 #2
0
  /**
   * Add un-encrypted + un-zipped InputStream contents as file "name" to encrypted zip file.
   *
   * @param name of the new zipEntry within the zip file
   * @param is provides the data to be added
   * @param password to be used for encryption
   */
  public void add(String name, InputStream is, String password)
      throws IOException, UnsupportedEncodingException {
    AESEncrypter aesEncrypter = new AESEncrypterBC(password.getBytes("iso-8859-1"));

    // Compress contents of inputStream and report on bytes read
    // we need to first compress to know details of entry
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(bos, new Deflater(9, true), 8 * 1024);
    int read = 0;
    long inputLen = 0;
    byte[] buf = new byte[8 * 1024];
    while ((read = is.read(buf)) > 0) {
      inputLen += read;
      dos.write(buf, 0, read);
    }
    dos.close();
    byte[] data = bos.toByteArray();

    ExtZipEntry entry = new ExtZipEntry(name);
    entry.setMethod(ZipEntry.DEFLATED);
    entry.setSize(inputLen);
    entry.setCompressedSize(data.length + 28);
    entry.setTime((new java.util.Date()).getTime());
    entry.initEncryptedEntry();

    zipOS.putNextEntry(entry);
    // ZIP-file data contains: 1. salt 2. pwVerification 3. encryptedContent 4. authenticationCode
    zipOS.writeBytes(aesEncrypter.getSalt());
    zipOS.writeBytes(aesEncrypter.getPwVerification());

    aesEncrypter.encrypt(data, data.length);
    zipOS.writeBytes(data, 0, data.length);

    byte[] finalAuthentication = aesEncrypter.getFinalAuthentication();
    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine(
          "finalAuthentication="
              + Arrays.toString(finalAuthentication)
              + " at pos="
              + zipOS.getWritten());
    }

    zipOS.writeBytes(finalAuthentication);
  }