简体   繁体   中英

How to create a password protected Zip Stream using TrueVFS's (was TrueZip) ZipOutputStream without keymanager?

import net.java.truevfs.comp.zip.ZipOutputStream;

...


ZipOutputStream zos = new ZipOutputStream(outPipe);
zos.setCryptoParameters( ## How to create those Crypto Parameters ### );

The cryptoparameters have to be of the interface ZipCryptoParameters . The class KeyManagerZipCryptoParameters is implementing that - but I do not want a keymanager, I just want to simply hard-code a specific password.

How can I do that?

Edit

I tried

private static final class CustomWinZipAesParameters
        implements WinZipAesParameters {
    final byte[] password;

    CustomWinZipAesParameters(final byte[] password) {
        this.password = password.clone();
    }

    @Override
    public byte[] getWritePassword(String name)
            throws ZipKeyException {
        return password.clone();
    }

    @Override
    public byte[] getReadPassword(String name, boolean invalid)
            throws ZipKeyException {
        if (invalid)
            throw new ZipKeyException(name + " (invalid password)");
        return password.clone();
    }

    @Override
    public AesKeyStrength getKeyStrength(String arg0)
            throws ZipKeyException {
        return AesKeyStrength.BITS_128;
    }

    @Override
    public void setKeyStrength(String name, AesKeyStrength keyStrength)
            throws ZipKeyException {
        // We have been using only 128 bits to create archive entries.
        assert AesKeyStrength.BITS_128 == keyStrength;
    }
} // CustomWinZipAesParameters

and then

byte[] ba = {64, 64, 64, 64, 64};
zos.setCryptoParameters(new CustomWinZipAesParameters(ba));

But the resulting ZIP is simply not encrypted.

I simply that to set the encrypted flag per file. (in addition to my edit above). Then it works.

Beware: I can only extract the file with 7z - not with the unzip command on Linux (otherwise I get "unsupported compression method 99")

File file = new File(contentManager.getOsmAndFilePlain());

ZipEntry ze = new ZipEntry(ContentManager.PRO_APP_FILENAME);
ze.setEncrypted(true);   ## this line inserted
zos.putNextEntry(ze);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM