Using a specific Java security provider only in the scope of a method

懵懂的女人 提交于 2020-08-05 06:23:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!