Zip4J - > java.security.ProviderException:无法构造MacSpi实例

时间:2016-04-04 13:32:23

标签: java junit cryptography zip zip4j

我尝试使用lingala zip4j归档X509Certificate个文件。

但是,只有当我使用Junit进行单元测试时,我才会收到这个奇怪的异常。

如果我将我的应用程序作为产品(这是一个弹簧网络应用程序)运行 - 它可以正常工作,没有例外,我能够正确地存档和取消存档文件。

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

这是我的Utils代码,它存档了我使用过的证书:

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

我正在使用Java 1.6

我也试过使用1.8,但我收到同样的错误。

注意:这只发生在我使用Junit运行时...

2 个答案:

答案 0 :(得分:2)

我发现使用PowerMockRunner.class运行测试会导致此问题发生。

我不确定为什么会这样。我能够通过反射创建我的模拟来解决这个问题,而不必使用PowerMock

我已经解决了我所遇到的直接问题,但这是一个非常奇怪的问题,如果有人知道为什么会这样,我仍然想知道。

答案 1 :(得分:0)

添加此内容会有所帮助-@PowerMockIgnore({“ javax.crypto。*”})

请参阅此以获取更多信息- https://github.com/powermock/powermock/issues/294