--- old/src/java.base/share/classes/java/util/zip/ZipEntry.java 2016-02-10 23:24:07.622787999 +0900 +++ new/src/java.base/share/classes/java/util/zip/ZipEntry.java 2016-02-10 23:24:07.376788726 +0900 @@ -57,6 +57,9 @@ int flag = 0; // general purpose flag byte[] extra; // optional extra field data for entry String comment; // optional comment string for entry + ZipCryption zipCryption = null; // encryption / decryption engine + byte[] encryptionHeader; // ZIP encryption header + boolean passphraseValidated = false; // passphrase validation is not yet. /** * Compression method for uncompressed entries. @@ -131,6 +134,8 @@ flag = e.flag; extra = e.extra; comment = e.comment; + zipCryption = e.zipCryption; + encryptionHeader = e.encryptionHeader; } /** @@ -673,4 +678,51 @@ throw new InternalError(e); } } + + /** + * Set passphrase for ZIP encryption and decryption. + * @param passphrase Passphrase for encryption and decryption. + */ + public void setPassphrase(String passphrase) { + zipCryption = new TraditionalZipCryption(passphrase); + } + + ZipCryption getZipCryption() { + return zipCryption; + } + + /** + * Get the state whether encrypted entry or not. + * @return true if passphrase is required. + */ + public boolean isPassphraseRequired() { + return (flag & 1) == 1; + } + + /** + * Validate passphrase for this entry. + * @return true if passphrase is valid, or this entry is not encrypted. + * @throws ZipException Passphrase is not set or Encryption header is not set. + */ + public boolean isValidPassphrase() throws ZipException{ + if (!isPassphraseRequired()) { + return true; + } + + if (zipCryption == null) { + throw new ZipException("Passphrase is not set."); + } + if (passphraseValidated) { + return true; + } + if (encryptionHeader == null) { + throw new ZipException("Encryption header is not set."); + } + + zipCryption.reset(); + byte[] tmp = encryptionHeader.clone(); + zipCryption.decryptBytes(tmp); + passphraseValidated = zipCryption.isValid(this, tmp); + return passphraseValidated; + } }