问题
So I am looking ways to use a security provider only in the scope of a function. I can already do this by adding these two lines: (assume BouncyCastle is the provider)
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
// do some stuff with converter
Security.removeProvider("BC");
So i just want to use BC for JcaPEMKeyConverter and then for the rest use default security provider.
Is there a better way to do this? More elegantly? How about a custom java annotation, is that a good way ?
Thx :)
回答1:
If you only want that the BouncyCastleProvider
are used in JcaPEMKeyConverter class use setProvider(java.security.Provider provider) instead of setProvider(java.lang.String providerName) as follows:
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(new BouncyCastleProvider());
Then there is no need to add and remove the provider on the security providers list, so you can avoid Security.addProvider
and Security.removeProvider
calls.
Hope it helps,
来源:https://stackoverflow.com/questions/37456230/using-a-specific-java-security-provider-only-in-the-scope-of-a-method