android 报错 java.security.NoSuchAlgorithmException: Provider BC does not provide SM4/CBC/PKCS5Padding

转: https://blog.csdn.net/nodonoidea/article/details/119727242

java后台写法

    Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
    Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
    cipher.init(mode, sm4Key, ivParameterSpec);
    return cipher;

android 正确的使用BouncyCastleProvider

Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_CBC_PADDING, new BouncyCastleProvider());
Key sm4Key = new SecretKeySpec(key, "SM4");
cipher.init(Cipher.ENCRYPT_MODE, sm4Key);
cipher.doFinal(plain);

如果安卓 使用BouncyCastleProvider.PROVIDER_NAME 就会报错。要使用new BouncyCastleProvider()

你可能感兴趣的:(android 报错 java.security.NoSuchAlgorithmException: Provider BC does not provide SM4/CBC/PKCS5Padding)