在单元测试中找不到任何支持AES / CBC / ISO7816-4Padding的提供商

时间:2018-04-03 13:06:33

标签: java android unit-testing security spongycastle

我使用SpongyCastle来支持转换 AES / CBC / ISO7816-4Padding 。我在Android项目的 build.gradle 文件中包含了以下依赖项:

compile "com.madgag.spongycastle:core:1.58.0.0"
compile "com.madgag.spongycastle:prov:1.58.0.0"
compile 'com.madgag.spongycastle:bcpkix-jdk15on:1.58.0.0'

我在Android手机上执行加密操作没有任何问题,但是我需要通过以下单元测试(这只是一个简单的类,我已经提取了实用程序类的主要功能I' ve必须加密和解密数据所以请忽略硬编码密钥和IV)

import static junit.framework.TestCase.assertEquals;

import java.security.GeneralSecurityException;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.junit.Test;
import org.spongycastle.jce.provider.BouncyCastleProvider;

public class SpongyTests {

    static {
        Security.insertProviderAt(new BouncyCastleProvider(), 1);
    }

    final static byte[] KEY = {
        0x2b, 0x7e, 0x15, 0x16, 0x28, (byte) 0xae, (byte) 0xd2, (byte) 0xa6,
        (byte) 0xab, (byte) 0xf7, 0x15, (byte) 0x88, 0x09, (byte) 0xcf, 0x4f, 0x3c
    };

    final static byte[] IV = {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };

    @Test
    public void testCryptoOperations() throws GeneralSecurityException {
        String plainData = "This is the confidential information";

        byte[] encryptedData = aesEncrypt(KEY, IV, plainData.getBytes());
        byte[] decryptedData = aesDecrypt(KEY, IV, encryptedData);

        assertEquals(plainData, new String(decryptedData));
    }

    public static byte[] aesEncrypt(byte[] key, byte[] iv, byte[] data) throws
        GeneralSecurityException {
        return symmetricCryptoOperation(Cipher.ENCRYPT_MODE, key, iv, data);
    }

    public static byte[] aesDecrypt(byte[] key, byte[] iv, byte[] data) throws GeneralSecurityException {
        return symmetricCryptoOperation(Cipher.DECRYPT_MODE, key, iv, data);
    }

    private static byte[] symmetricCryptoOperation(int mode, byte[] key, byte[] iv, byte[] data) throws GeneralSecurityException {
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/ISO7816-4Padding");
        cipher.init(mode, keySpec, new IvParameterSpec(iv));
        return cipher.doFinal(data);
    }
}

当我运行上述单元测试时,我收到以下异常:

java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/ISO7816-4Padding

    at javax.crypto.Cipher.getInstance(Cipher.java:540)
    at com.yoti.mobile.android.tags.common.SpongyTests.symmetricCryptoOperation(SpongyTests.java:49)
    at com.yoti.mobile.android.tags.common.SpongyTests.aesEncrypt(SpongyTests.java:40)
    at com.yoti.mobile.android.tags.common.SpongyTests.testCryptoOperations(SpongyTests.java:32)

正如您所看到的,我在第一个位置添加BouncyCastleProvider但它不起作用。关于如何让这个测试通过的任何想法? :)

2 个答案:

答案 0 :(得分:2)

尝试更改此

static {
    Security.insertProviderAt(new BouncyCastleProvider(), 1);
}

到这个

@Before
public void setUp() {
    Security.insertProviderAt(new BouncyCastleProvider(), 1);
}

答案 1 :(得分:0)

我找到了解决方案。 SpongyCastle提供商似乎不支持这种转变。我必须为测试版本添加以下依赖项:

testCompile 'org.bouncycastle:bcprov-jdk16:1.46'

之后测试工作了! :)