javajunitcryptographyzipzip4j

Zip4J -> java.security.ProviderException: Could not construct MacSpi instance


I'm trying to use lingala zip4j to archive X509Certificate files.

However, I am getting this strange exception only when I am unit testing with Junit.

If I run my application as a product (which is a spring web app) - it works fine with no exceptions and I am able to properly archive and un-archive files with no issues.

net.lingala.zip4j.exception.ZipException: java.security.ProviderException: Could not construct MacSpi instance

    at net.lingala.zip4j.crypto.AESEncrpyter.deriveKey(AESEncrpyter.java:116)
    at net.lingala.zip4j.crypto.AESEncrpyter.init(AESEncrpyter.java:89)
    at net.lingala.zip4j.crypto.AESEncrpyter.<init>(AESEncrpyter.java:69)
    at net.lingala.zip4j.io.CipherOutputStream.initEncrypter(CipherOutputStream.java:173)
    at net.lingala.zip4j.io.CipherOutputStream.putNextEntry(CipherOutputStream.java:133)
    at net.lingala.zip4j.io.DeflaterOutputStream.putNextEntry(DeflaterOutputStream.java:45)
    ...

Caused by: java.security.ProviderException: Could not construct MacSpi instance
    at javax.crypto.Mac.chooseFirstProvider(Mac.java:316)
    at javax.crypto.Mac.getMacLength(Mac.java:398)
    at net.lingala.zip4j.crypto.PBKDF2.MacBasedPRF.<init>(MacBasedPRF.java:45)
    at net.lingala.zip4j.crypto.PBKDF2.PBKDF2Engine.assertPRF(PBKDF2Engine.java:103)
    at net.lingala.zip4j.crypto.PBKDF2.PBKDF2Engine.deriveKey(PBKDF2Engine.java:66)
    at net.lingala.zip4j.crypto.AESEncrpyter.deriveKey(AESEncrpyter.java:113)
    ... 

Here is my Utils code which archives the certificates that I have used:

import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.io.ZipOutputStream;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.UUID;

public class ZipTestUtils {

    public static byte[] archive(List<X509Certificate> certificateList, String password)
            throws IOException, CertificateEncodingException, ZipException {

        byte[] bytes = null;

        // --------Encryption zipParameters (for password protection)--------
        ZipParameters zipParameters = getZipParameters(password);

        // -------------------- CREATE ZIP file --------------------
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream outputZipStream = new ZipOutputStream(outputStream);

        // Create ZIP file
        for (X509Certificate certificate : certificateList) {
            if (certificate == null) {
                // skip invalid entries.
                continue;
            }

            File file = File.createTempFile(UUID.randomUUID().toString(), ".cer");
            file.deleteOnExit();

            outputZipStream.putNextEntry(file, zipParameters);
            outputZipStream.write(CertificateTestUtils.encodeCertificate(certificate));
            outputZipStream.closeEntry();
        }

        //finish up
        outputZipStream.finish();

        bytes = outputStream.toByteArray();


        return bytes;
    }

    private static ZipParameters getZipParameters(String password) {
        // Create ZipParameters
        ZipParameters zipParameters = new ZipParameters();

        // Set how you want to encrypt files
        zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

        // Set encryption of files to true
        zipParameters.setEncryptFiles(true);

        // Set encryption method
        zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
        // Set key strength
        zipParameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);

        // Set password
        zipParameters.setPassword(password);
        return zipParameters;
    }
}

I'm using Java 1.6

I've also tried using 1.8 but I am getting the same error.

Note: This is only happening when I run with Junit...


Solution

  • I found out that running the tests with PowerMockRunner.class is causing this issue to occur.

    I am not sure why this is the case. I was able to overcome the problem by creating my mocks with reflection without having to use PowerMock.

    I have resolved the immediate issue that I had, but this is a very strange issue, and if anyone knows why this is happening I would still like to know.