我怎么知道我是使用 jar 中的 Bouncy Castle 还是 Android 中实现的那个?

How I know if I using Bouncy Castle from jar or the one implemented in Android?

我怎么知道我是使用 jar 中的 Bouncy Castle 还是 Android 中实现的? 我已经从 https://www.bouncycastle.org/latest_releases.html 下载并将 jar 添加到我的 Android Studio 项目中。 如何检查我是否正在使用它(我下载的那个)?

Android 平台捆绑了 Bouncy Castle 的一个受限子集,如果您通过名称 "BC" 设置提供程序,则默认情况下将被选中。 (如果你按照 James 指示的 class 来做的话就不会)

Signature.getInstance("SHA1WithRSA","BC")

使用您自己的加密提供程序的替代方案:

1 包括您自己的 BouncyCastle 版本

包括 bouncycastle 的 jars,删除捆绑的提供程序并添加新的。

Security.removeProvider("BC");
BouncyCastleProvider bc = new BouncyCastleProvider();
Security.insertProviderAt(bc,1);

2 使用海绵堡:

SpongyCastle 是针对 Android 的 BouncyCastle 的官方重新打包。 https://rtyley.github.io/spongycastle/。包括依赖项并使用 "SC" 而不是 "BC"

 Signature.getInstance("SHA1WithRSA","SC")

Why?

The Android platform unfortunately ships with a cut-down version of Bouncy Castle - as well as being crippled, it also makes installing an updated version of the libraries difficult due to classloader conflicts.

Spongy Castle is the stock Bouncy Castle libraries with a couple of small changes to make it work on Android:

  • all package names have been moved from org.bouncycastle.* to org.spongycastle.*

  • to avoid classloader conflicts the Java Security API Provider name is now SC rather than BC

  • no class names change, so the BouncyCastleProvider class remains Bouncy, not Spongy, but moves to the org.spongycastle.jce.provider package.

至少在最近的几个版本中,Android 已通过将他们使用的 bouncycastle class 重命名为 com.android.** 来修复名称空间冲突。我不确定这是什么时候发生的,但是查看 https://android.googlesource.com/platform/external/bouncycastle/+refs 的源代码存储库表明更改是从冰淇淋三明治开始发生的 -- API 级别 14。

我不确定他们对 string-based 提供商查找做了什么,例如Cipher.getInstance("AES/GCM/PKCS5PADDING", "BC")。但是,JCE 中的每个 getInstance() 方法也有一个版本,您可以在其中显式指定 Provider class,例如Cipher.getInstance("AES/GCM/PKCS5PADDING", new org.bouncycastle.jce.provider.BouncyCastleProvider()) 这消除了任何歧义。