I am trying to understand how java security providers work in android. I would like to force all calls to Cipher.getInstance() to return a cipher with spongy castle as the provider. I am having no luck.
The following code returns a cipher with provider being "AndroidKeyStoreBCWorkaround version 1.0", but I want the provider to be SpongyCastle.
The reason I want to do this, is that I have a library that calls into javax.crypto.Cipher.getInstance() multiple times. I want all those calls to go to spongy castle, without having to re-write the library to explicitly specify "SC" as the provider.
public class MainActivity extends Activity
{
static
{
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
Security.removeProvider("BC");
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
try
{
//this returns provider = "AndroidKeyStoreBCWorkaround version 1.0"
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES/CTR/NoPadding");
//this works
// cipher = javax.crypto.Cipher.getInstance("AES/CTR/NoPadding", "SC");
}
catch(Exception e)
{
}
}
}
You just need to write another class which calls javax.crypto.Cipher.getInstance and passes it the "SC" variable. You should be able to do so without making any changes to the original library, and can do so in the same file as the code you have provided. Though this answer seems overly simple so perhaps there are other aspects to your question? That's the answer, anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With