英文:
Cannot find any provider supporting RSA/None/OAEPWITHSHA-256ANDMGF1PADDING
问题
我在使用javax.crypto
中的RSA加密时遇到了一个奇怪的问题。
我正在使用以下方式的RSA/None/OAEPWITHSHA-256ANDMGF1PADDING
模式...
Cipher cipher = Cipher.getInstance("RSA/None/OAEPWITHSHA-256ANDMGF1PADDING");
...并且在我的Spring Boot项目中它正常工作。
但是,当我通过同一项目中的main(String[] args)
调用相同的函数时,我遇到了问题:
无法找到支持 RSA/None/OAEPWITHSHA-256ANDMGF1PADDING 的提供程序
同样:如果我使用RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING
,我可以得到正确的输出!(我不想使用ECB,因为我知道它在RSA算法中没有用途)。
另外,在我另一个Spring Boot项目中,我遇到了相同的上述问题(这次不仅在main(String[] args)
中出现,而且在Spring Boot项目自身中也出现了)。
有人能帮我解决这个问题吗?
英文:
I encountered a strange issue while encrypting with RSA in javax.crypto
.
I am using RSA/None/OAEPWITHSHA-256ANDMGF1PADDING
mode as follow...
Cipher cipher = Cipher.getInstance("RSA/None/OAEPWITHSHA-256ANDMGF1PADDING");
...and it is working correctly in my spring boot project.
But when I call the same function via main(String[] args)
in the same project, I am getting the issue:
Cannot find any provider supporting RSA/None/OAEPWITHSHA-256ANDMGF1PADDING
Again: if I use RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING
I get the correct output! (I don't want to use ECB as I know it has no use in the RSA algorithm).
Also, in my another spring boot project, I am getting the same above issue (this time not only main(String[] args)
but in the Spring Boot project itself as well).
Could anybody help me with this?
答案1
得分: 11
基本上,“"RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"”中的ECB,由“"SunJCE"”提供程序提供的方式是一个误称。它可能是从旧版Java中的分组密码模式复制而来。它不允许加密多个数据块,这是你从ECB模式中所期望的。换句话说,它与“"RSA/None/OAEPWITHSHA-256ANDMGF1PADDING"”完全相同,但通常不接受该算法名称作为Java SE。
它在你的Spring Boot项目中不会失败的原因是,该项目可能包含了Bouncy Castle提供程序(或另一个注册了带有“none”的算法名称的提供程序)。然而,Bouncy Castle只是一个纯软件提供程序,存在侧信道保护问题。通常你应该更倾向于使用“"SunJCE"”内部的实现。
在没有明确指定提供程序的情况下,只使用“"RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"”是最好的方法。
【编辑】我强烈建议使用OAEPParameterSpec
和MGF1ParameterSpec
明确设置要用于标签和MGF1的SHA-256哈希。不幸的是,Java可能默认使用SHA-1,而在所有地方使用相同的哈希函数是最兼容的选项。
英文:
Basically the ECB in "RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"
as provided by the "SunJCE"
provider is a misnomer. It probably has been copied from the block cipher modes in the old Java versions. It does not allow multiple blocks to be encrypted, which is what you would expect from ECB mode. In other words, it is completely identical to "RSA/None/OAEPWITHSHA-256ANDMGF1PADDING"
- but that algorithm name is generally not accepted with Java SE.
The reason why it doesn't fail in your Spring Boot project is that it likely includes the Bouncy Castle provider (or another provider that registers the algorithm name with none
inside of it). Bouncy Castle is however a software only provider, that has had side channel protection issues. Generally you should prefer the implementation within the "SunJCE"
.
Just using "RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"
without explicitly specifying the provider is the best way forward.
[EDIT] I strongly suggest to set the SHA-256 hash to be used for the label and MGF1 explicitly using OAEPParameterSpec
and MGF1ParameterSpec
. Unfortunately Java may default to SHA-1 and using identical hash functions all over is the most compatible option.
答案2
得分: 0
当我遇到这个问题时,我能够通过指定提供程序来解决它,就像这样:
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
所以看起来我们需要使用重载的构造函数:
Cipher cipher = Cipher.getInstance("RSA/None/OAEPWITHSHA-256ANDMGF1PADDING", provider);
英文:
When I faced this issue, I was able to resolve it by specifying the provider likeso
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider())
. So it seems that we need to use the overloaded consutructor
Cipher cipher = Cipher.getInstance("RSA/None/OAEPWITHSHA-256ANDMGF1PADDING", provider);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论